Statements:
- In Python, statements are individual instructions or commands that perform specific actions or operations.
- A statement is a single line of code or a group of lines that Python executes sequentially.
Different Types of Statements:
# Assignment Statements: Assign values to variables. For example:
x = 35
name = "Rana"
#Expression Statements: Evaluate an expression and may produce a result. For example:
y = x + 5
print(y)
#Conditional Statements: Control the flow of execution based on conditions. For example:
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
#Loop Statements: Repeat a block of code multiple times. For example:
for i in range(5):
print(i)
#Function and Method Calls: Invoke functions or methods to perform specific tasks. For example:
result = sum([1, 2, 3, 4, 5])
string_length = len("data 4 fashion")
#Import Statements: Import modules or packages to access their functionalities. For example:
import math
from datetime import datetime
#Exception Handling Statements: Catch and handle exceptions to prevent program crashes. For example:
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero is not allowed")