Python Syntax – Tutorial with Examples

Python syntax refers to the set of rules that determine how the Python programming language is written and organized. The syntax is straightforward, easy to learn, and highly readable, making it a popular choice for beginners and experienced programmers alike.

Indentation

One of the unique features of Python’s syntax is its use of indentation to delimit blocks of code. Unlike other programming languages that use curly braces to define blocks of code, Python uses indentation to define blocks. The number of spaces used for indentation must be consistent within a block of code, but it does not matter how many spaces are used as long as it is consistent.

if True:
    print("True")
    print("Inside the block")
print("Outside the block")

In the example above, the two print statements inside the if block are indented, which indicates that they belong to the block of code that is executed when the condition is true. The last print statement is not indented and is outside of the block, so it is executed regardless of the result of the if statement.

Variables and Data Types

In Python, variables are used to store values. Unlike other programming languages, Python variables do not need to be declared with a specific data type. The data type of a variable is determined automatically based on the value assigned to it.

x = 10
y = "Hello"
z = [1, 2, 3]

In the example above, the variable x is assigned the value 10, which is an integer. The variable y is assigned the value “Hello”, which is a string. The variable z is assigned the value [1, 2, 3], which is a list.

Operators and Expressions

Python provides a variety of operators for performing arithmetic, comparison, and logical operations. These operators can be used to create expressions that are evaluated to produce a value.

x = 10
y = 5
#Arithmetic operators
print(x + y) # 15
print(x - y) # 5
print(x * y) # 50
print(x / y) # 2.0

#Comparison operators
print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False

#Logical operators
print(True and False) # False
print(True or False) # True
print(not True) # False

In the example above, the arithmetic, comparison, and logical operators are used to evaluate expressions and produce values. The result of each expression is printed to the console.

Control Flow

Control flow statements in Python allow you to execute code conditionally and repeatedly. The most commonly used control flow statements in Python are if, for, and while.

# if statement
x = 10
if x > 5:
    print("x is greater than 5")
else:
    print("x is less than or equal to 5")

# for statement
for i in range(5):
    print(i)

# while statement
i = 0
while i < 5:
    print(i)
    i += 1

In the example above, the if statement is used to execute a block of code conditionally based on a boolean expression. The for statement is used to execute a block of code repeatedly for each item in a sequence. The while statement is used to execute a block of code repeatedly while a boolean expression is true.

Functions

Functions in Python are blocks of reusable code that can accept inputs, perform a specific task, and return an output. Functions help to break down large and complex programs into smaller and more manageable components. Functions can be defined using the def keyword, followed by the function name and a set of parentheses for accepting arguments.

def greet(name):
    print("Hello " + name)
greet("John") # Hello John

In the example above, a function named greet is defined to accept a single argument named name. The function then prints a greeting message using the name argument. The function is called by passing the argument “John”, which results in the message “Hello John” being printed to the console.

Modules and Packages

In Python, modules are individual files that contain Python code. Modules can be imported into other Python files to make their code available for reuse. A package is a collection of modules that are grouped together. Packages can be installed using the pip package manager, which makes it easy to add new functionality to your Python environment.

# Importing a module
import math
print(math.pi) # 3.141592653589793
#Installing a package
!pip install numpy
import numpy as np
a = np.array([1, 2, 3])
print(a) # [1 2 3]

In the example above, the math module is imported and used to print the value of pi. The numpy package is installed and imported, and its array function is used to create a NumPy array. The resulting array is then printed to the console.

This is a brief overview of the Python syntax. It is recommended that you read more about the language and practice writing code to become proficient in using it.

Leave a Reply

Your email address will not be published. Required fields are marked *