2.1 Conditional Blocks Using if
Conditional blocks in Python allow the execution of specific code based on whether a condition is True or False. The if statement is used to evaluate a condition.
Syntax:
python
if condition:
# Code to execute if the condition is True
Example:
python
age = 18
if age >= 18:
print("You are an adult.")
In this example, if age is 18 or more, the message "You are an adult." will be printed.
2.2 else and elif
The else statement is used to execute code if the if condition is False. The elif (else if) statement checks multiple conditions in a sequence.
Syntax:
python
if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition2 is True
else:
# Code to execute if both conditions are False
Example:
python
age = 16
if age >= 18:
print("You are an adult.")
elif age > 12:
print("You are a teenager.")
else:
print("You are a child.")
In this example, if age is between 13 and 17, the message "You are a teenager." is printed; otherwise, other statements execute based on the condition.
2.3 for Loops and Iterations
A for loop in Python is used to iterate over a sequence (like a list, tuple, or string) and perform a set of actions.
Syntax:
python
for item in sequence:
# Code to execute for each item in the sequence
Example:
python
for num in range(1, 5):
print(num)
This code prints numbers from 1 to 4. The range() function generates the numbers in the sequence.
Iterating Over a List:
python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
This will print each fruit in the list.
2.4 while Loops
A while loop repeatedly executes a block of code as long as the given condition is True.
Syntax:
python
while condition:
# Code to execute while the condition is True
Example:
python
count = 0
while count < 5:
print(count)
count += 1
This loop prints the values from 0 to 4 and stops when count becomes 5.
2.5 Loop Manipulation Using continue, break, and pass
break: Terminates the loop prematurely.Example:
pythonfor num in range(10): if num == 5: break print(num)This loop will stop when
numequals 5.continue: Skips the current iteration and moves to the next iteration.Example:
pythonfor num in range(5): if num == 3: continue print(num)This will print 0, 1, 2, 4, skipping 3.
pass: Acts as a placeholder when a statement is required syntactically but no action needs to be performed.Example:
pythonfor num in range(5): if num == 3: pass print(num)This does nothing when
numis 3 but continues the loop.
2.6 Programming Using Conditional and Loop Blocks
By combining conditionals (if, else, elif) and loops (for, while), more complex and dynamic programs can be written.
Example: Find the sum of all even numbers from 1 to 10.
python
sum_of_evens = 0
for num in range(1, 11):
if num % 2 == 0:
sum_of_evens += num
print("Sum of even numbers from 1 to 10:", sum_of_evens)
In this program:
- The loop iterates over the numbers from 1 to 10.
- The
ifstatement checks whether the number is even. - If the number is even, it's added to
sum_of_evens.
These control structures (conditionals and loops) form the backbone of decision-making and iteration in Python programming. Understanding them is crucial for implementing logic and handling repetitive tasks efficiently.
0 Comments