module 3
module 3
Selection Statements
1. if Statement
The if statement in Python is used to make decisions based on conditions. It allows you to
execute a block of code only if a specified condition is true.
Syntax:
if condition:
# Code block to execute if condition is true
Example:
temperature = 30
if temperature > 25:
print("It's a hot day.")
Output
2. if-else Statement
The if-else statement in Python is a fundamental control structure that allows you to execute
specific blocks of code based on whether a condition is true or false.
How It Works
1. Condition Evaluation: The condition inside the if statement is evaluated.
2. Execution:
o If the condition is True, the code block following the if statement is executed.
o If the condition is False, the code block following the else statement is
executed.
Syntax:
if condition:
# Code block if condition is true
else:
# Code block if condition is false
Example:
temperature = 20
if temperature > 25:
print("It's a hot day.")
else:
print("It's a pleasant day.")
Output
It's a pleasant day.
3. elif Statement
The elif statement is used when you want to check multiple conditions. It is possible to have
multiple elif statements following an if.
Syntax:
if condition1:
# Code block if condition1 is true
elif condition2:
# Code block if condition2 is true
else:
# Code block if all conditions are false
Example:
temperature = 15
if temperature > 25:
print("It's a hot day.")
elif temperature > 15:
print("It's a warm day.")
else:
print("It's a cool day.")
Output
It's a cool day.
4. Nested if Statements
It is possible to nest if statements within each other to create more complex conditions.
Syntax
if condition1:
# Code block if condition1 is true
if condition2:
# Code block if condition2 is true
else:
# Code block if condition2 is false
else:
# Code block if condition1 is false
Example:
num = 10
if num > 0:
print("Positive number")
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
else:
print("Negative number or zero")
Output
Positive number
Even number
Iteration Statements
1. for Loop
The for loop is used to iterate over a sequence (like a list, tuple, or string) or to execute a
block of code a specific number of times.
Syntax:
for variable in sequence:
# Code block to execute for each element
Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output
0
1
2
3
4
The range() function defaults to 0 as a starting value, however it is possible to specify the
starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not
including 6):
for x in range(2, 6):
print(x)
output
2
3
4
5
Output
b
a
n
a
n
a
3. With the break statement we can stop the loop before it has looped
through all the items:
4.Exit the loop when x is "banana", but this time the break comes before the
print:
5. With the continue statement we can stop the current iteration of the loop,
and continue with the next:
The else block will NOT be executed if the loop is stopped by a break statement.
Example
Break the loop when x is 3, and see what happens with the else block:
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
Output
0
1
2
for x in adj:
for y in fruits:
print(x, y)
Output
red apple
red banana
big apple
big banana
Example 2
for x in range(3):
for y in range(3):
print(x, y)
Output
00
01
02
10
11
12
20
21
22
Output
1
2
4
5
6
Output
1
2
3
4
5
i is no longer less than 6
Properties of Lists
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
Ordered
When we say that lists are ordered, it means that the items have a defined order, and
that order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
Changeable
The list is changeable, meaning that we can change, add, and remove items in a list
after it has been created.
Allow Duplicates
Since lists are indexed, lists can have items with the same value:
Example
Lists allow duplicate values:
List Length
To determine how many items a list has, use the len() function:
type()
From Python's perspective, lists are defined as objects with the data type 'list':
Access Items
List items are indexed and you can access them by referring to the index number:
Example : Print the second item of the list:
Note: The search will start at index 2 (included) and end at index 5 (not included).
Remember that the first item has index 0.
By leaving out the start value, the range will start at the first item:
Example
This example returns the items from the beginning to, but NOT including, "kiwi":
By leaving out the end value, the range will go on to the end of the list:
Example
This example returns the items from "cherry" to the end:
If you insert more items than you replace, the new items will be inserted where you
specified, and the remaining items will move accordingly:
Example
Change the second value by replacing it with two new values:
Note: The length of the list will change when the number of items inserted does not
match the number of items replaced.
If you insert less items than you replace, the new items will be inserted where you
specified, and the remaining items will move accordingly:
Example
Change the second and third value by replacing it with one value:
Insert Items
To insert a new list item, without replacing any of the existing values, we can use
the insert() method.
The insert() method inserts an item at the specified index:
Example
Insert "watermelon" as the third item:
Note: As a result of the example above, the list will now contain 4 items.
Note: As a result of the examples above, the lists will now contain 4 items.
Extend List
To append elements from another list to the current list, use the extend() method.
Example
Add the elements of tropical to thislist: