Python Language Tutorial
Python is one of the most widely used programming languages in the world. Python was created in the late 1980s by Guido van Rossum and first released in 1991. Now, Python is among the most popular programming languages in the world, and has been since way back then.
1. Features of Python.
a. Easy to Learn and Read
Python was designed for simple syntax and is easy to learn and read code. What I like about code blocks being separated by indentation, too, is that it improves the readability of the code, as it helps the reader understand (immediately) the structure of the program.
b. Interpreted Language
Instead, Python is an interpreted language, which, in other words, doesn't require a compilation step. The Python interpreter reads and executes the code, line by line, rather than compiling it from scratch like other compiled languages, resulting in a shorter development cycle and a more interactive experience. Instead, Python is an interpreted language, which, in other words, doesn't require a compilation step. The Python interpreter reads and executes the code, line by line, rather than compiling it from scratch like other compiled languages, resulting in a shorter development cycle and a more interactive experience.
c. Dynamically typed
Python is dynamically typed: the type of a variable is determined at runtime. Dynamically typed languages dare to be different. Unlike statically typed languages, you don't have to declare what type of data a variable can contain.
d. Object-Oriented
Python is a fully object-oriented programming language. It allows developers to create and work with classes and objects, promoting code reusability and modularity.
e. Extensive Standard Library
Python has a rich standard library containing many modules and functions for different tasks, including file handling, networking, and web development.
f. Cross-Platform
Python is a platform-independent programming language, so that any code written on any Operating System will run on any other Operating System without any changes.
2. Installing Python
Python can work on various platforms, including Windows, macOS, and Linux. Setting up a Python environment is the first step to start programming in Python. A Python environment includes the Python interpreter, libraries, and tools that allow you to write, execute, and manage Python code efficiently.
Python provides two types of versions in use: Python 2 and Python 3. Python 2 reached its end of life on January 1, 2020, and Python 3 is used current and recommended version. In this guide, we'll focus on Python 3. It is the version actively developed and supported. I am using Windows, so I suggest the to Install. To install Python on your computer, follow these steps:
- Visit the official Python website (https://www.python.org) and go to the Downloads section.
- Click on the latest stable release of Python 3 (e.g., Python 3.8.x).
- Scroll down and download the Windows installer (e.g., python-3.8.x-amd64.exe) suitable for your system architecture (32-bit or 64-bit).
- Run the installer and check the option to add Python to the system's PATH during installation.
- Click "Install Now" to begin the installation process.
- After installation, open the Command Prompt or PowerShell and type python to verify that Python is installed correctly.
3. Python Integrated Development Environments (IDEs)
You can write Python code in any text editor, but using an Integrated Development Environment (IDE) can significantly enhance your coding productivity. IDEs provide more features like code completion, syntax highlighting, debugging tools, and integration with version control systems. Here are some popular Python IDEs. But I am using Pycharm
PyCharm is a powerful and more feature-loaded IDE developed by JetBrains. It is available in both free (Community Edition) and paid (Professional Edition) versions. PyCharm offers advanced code analysis, debugging, and integration with popular web frameworks that help the coder to make the coding experience better..We will download PyCharm Community Edition, which is free to use. Go to YouTube and search how to install PyCharm, you will get steps to download the PyCharm IDE.
Your First Python Program
Let's write a simple "Hello, World!" program in Python to get started in PyCharm.
Python Code
# Hello, World! program
print("Hello, World!")
4. Python syntax and comments
A set of rules that control how you write code in the language is called Syntax. Following these rules ensures that your code is valid and can be executed without errors. There are some key points about Python syntax.
Indentation: Indentation is used to define blocks of code instead of using curly braces as in many other programming languages. Indentation is very important for structuring your code and identifying loops, conditionals, and function definitions.
For example :
Python Code
# Correct indentation
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
# Incorrect indentation (will raise an IndentationError)
if x > 5:
print("x is greater than 5")
For Example :
Python Code
# This is a single-line comment
"""
This is a
multi-line
comment
"""
# Code with comments
x = 10
# The following line adds 5 to the value of x
x += 5 # Now x is 15
For example :
Python Code
result = 10 + 20 + \
30 + 40 + \
50
For example :
Python Code
print("Hello"); print("World")
For example :
Python Code
# Both are valid
x=5
y = 10
5. Basic Input/Output Functions
print() function and input() function
In Python, the print() and input() functions are essential for input/output operations. The print() function is used to display output to the console. It takes one or more arguments (separated by commas) and prints them to the screen.
Python Code
print("Hello, world!")
Python Code
name = "John"
age = 30
print("My name is", name, "and I am", age, "years old.")
The input() function is used to get input from the user. When called, it displays a prompt to the user and waits for them to enter some text. The user's input is treated as a string and can be stored in a variable. Here's an example:
Python Code
name = input("Enter your name: ")
print("Hello, " + name + "!")
You can also convert the input to other data types. For example, if you expect the user to enter an integer, you can do this:
Python Code
age = int(input("Enter your age: "))
PYTHON BASICS: VARIABLES AND DATA TYPES
In Python, variables store data values. They are the containers for holding various types of data, such as numbers, text, or collections of values. In Python, understanding variables and data types is fundamental to programming, which allows us to work with different types of data efficiently
1. Declaring Variables :
In Python, variables are declared using (equal =) sign to its a value. Unlike other programming languages, Python is dynamically typed, meaning the type of a variable is determined at runtime based on the value assigned to it.
Python Code
# Declaring variables
age = 25
name = "John Doe"
is_student = True
2. Python Data Types
Python supports several built-in data types, which are the fundamental building blocks for constructing more complex data structures. The most common data types include:
a. Numeric Types
Python has several numeric data types:
- Integers: Whole numbers without a fractional part, such as 1, 10, or -5.
- Floating-Point Numbers: Numbers with a fractional part, such as 3.14 or -2.71.
- Complex Numbers: Numbers in the form of a + bj, where a and b are real numbers and j represents the square root of -1.
Python Code
# Numeric data types
age = 25
height = 5.11
complex_num = 3 + 2j
b. Strings
Strings are sequences of characters and are used to represent text data. They can be enclosed in single quotes (') or double quotes (").
Python Code
# String data type
name = "John Doe"
message = 'Hello, World!'
c. Boolean
Boolean data type represents the truth values True or False. Booleans are often used in conditional statements.
Python Code
# Boolean data type
is_student = True
is_registered = False