Statements and Control Structures in Python
1. Assignment Statement
Definition: Assigns a value to a variable.
Syntax:
variable_name = valueExample:
x = 10
name = "Alice"
pi = 3.14162. Import Statement
Definition: Used to include external modules or libraries in the program for additional functionalities.
Syntax:
import module_nameExample:
import math
print(math.sqrt(16)) # Output: 4.0Import specific functions:
from math import sqrt
print(sqrt(25)) # Output: 5.03. Print Statement
Definition: Outputs information or variables to the console.
Syntax:
print(value1, value2, ..., sep='separator', end='end')Examples:
print("Hello, World!")
print("Python", "is", "fun", sep="-") # Output: Python-is-fun
print("Hi", end="!") # Output: Hi!
print(10, 20, 30)4. if: elif: else: Statement
Definition: Used for decision making; executes code based on conditions.
Syntax:
if condition1:
# block 1
elif condition2:
# block 2
else:
# block 3Example:
score = 85
if score >= 90:
print("Grade A")
elif score >= 80:
print("Grade B")
else:
print("Grade C")5. for Statement and while Statement
for Loop: Repeatedly executes a block for each item in a sequence.
Syntax:
for variable in iterable:
# blockExample:
for i in range(5):
print(i) # Outputs 0 to 4while Loop: Repeats as long as a condition is true.
Syntax:
while condition:
# blockExample:
count = 0
while count < 5:
print(count)
count += 16. continue and break Statements
continue: Skips the current iteration and moves to the next loop iteration.
Example:
for i in range(5):
if i == 2:
continue
print(i)
# Output: 0 1 3 4break: Stops the loop entirely when called.
Example:
for i in range(5):
if i == 3:
break
print(i)
# Output: 0 1 27. try: except Statement
Definition:
Used for exception handling; code in try block runs, exceptions caught in except.
Syntax:
try:
# code that might cause exception
except ExceptionType:
# code to handle exceptionExample:
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")8. raise Statement and with Statement
raise: Manually triggers an exception.
Syntax:
raise ExceptionType("Error message")Example:
def check_age(age):
if age < 0:
raise ValueError("Age cannot be negative")with: Simplifies exception handling with resources (like files), ensuring cleanup.
Syntax:
with open("file.txt", "r") as file:
content = file.read()9. del and case Statement
del: Deletes variables, list items, dictionary entries, or object attributes.
Syntax:
del variable
del list[index]
del dict[key]Example:
x = 10
del x
my_list = [1, 2, 3]
del my_list[^1]case Statement:
Python doesn’t have a built-in case statement like some languages, but Python 3.10+ has match-case for pattern matching.
Syntax:
match variable:
case pattern1:
# block
case pattern2:
# block
case _:
# default blockExample:
def number_to_string(num):
match num:
case 1:
return "One"
case 2:
return "Two"
case _:
return "Other"
print(number_to_string(2)) # Output: TwoThis completes the detailed, syntax-rich notes for the “Statements and Control Structures” unit with definitions, usage, and examples.
Say “next” to continue to the next unit: list/tuple/range.
Footnotes
-
https://www.codingal.com/coding-for-kids/blog/control-structures-in-python/ ↩
-
https://www.geeksforgeeks.org/dsa/loops-and-control-statements-continue-break-and-pass-in-python/ ↩
-
https://www.kdnuggets.com/python-basics-syntax-data-types-and-control-structures ↩
-
https://www.geeksforgeeks.org/python/conditional-statements-in-python/ ↩