7/8/24, 3:33 PM class 3
if Statement
The if statement evaluates a condition. If the condition is True , the block of code inside
the if statement is executed. If the condition is False , the block of code is skipped.
# Basic structure of an if statement if condition: # Block of code to execute if condition is True pass
In [1]: x = 10
if x > 5:
print("x is greater than 5")
x is greater than 5
In [2]: x = int(input("enter a number: "))
if x > 5:
print("x is greater than 5")
enter a number: 4
else Statement
The else statement follows an if statement and executes a block of code if the if
condition is False
# Basic structure of an if-else statement if condition: # Block of code to execute if condition is True pass else: #
Block of code to execute if condition is False pass
In [3]: x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
x is not greater than 5
In [4]: x = int(input("enter a number: "))
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
enter a number: 4
x is not greater than 5
elif Statement
The elif (short for "else if") statement allows you to check multiple conditions. It must
follow an if statement or another elif statement. If the if condition is False , the
program checks the elif condition. If the elif condition is True , its block of code is
executed. You can have multiple elif statements.
# Basic structure of an if-elif-else statement if condition1: # Block of code to execute if condition1 is True pass
elif condition2: # Block of code to execute if condition1 is False and condition2 is True pass else: # Block of
localhost:8888/nbconvert/html/Python/Python/class 3.ipynb?download=false 1/7
7/8/24, 3:33 PM class 3
code to execute if both condition1 and condition2 are False pass
In [5]: x = 7
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5 ")
else:
print("x is less than 5")
x is greater than 5
Nested if Statements
You can nest if , elif , and else statements inside one another to create more complex
conditions.
In [6]: x = int(input("enter a number: "))
if x > 10:
print("x is greater than 10")
if x > 20:
print("x is also greater than 20")
else:
print("x is not greater than 20")
else:
print("x is not greater than 10")
enter a number: 45
x is greater than 10
x is also greater than 20
Combining Conditions
You can combine conditions using logical operators such as and , or , and not .
In [7]: x = 8
y = 12
if x > 5 and y > 10:
print("Both conditions are True")
else:
print("At least one condition is False")
Both conditions are True
In [8]: x = 2
y = 12
if x > 5 or y > 10:
print("one condition is True")
else:
print("Both conditions are False")
one condition is True
Indentation
localhost:8888/nbconvert/html/Python/Python/class 3.ipynb?download=false 2/7
7/8/24, 3:33 PM class 3
Python relies on indentation (whitespace at the beginning of a line) to define scope in the
code. Other programming languages often use curly-brackets for this purpose.
In [9]: a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
Cell In[9], line 4
print("b is greater than a") # you will get an error
^
IndentationError: expected an indented block after 'if' statement on line 3
Python Loops
Python has two primitive loop commands:
while loops
for loops
1. while loop
A while loop in Python repeatedly executes a block of code as long as a specified
condition is True .
It is useful when the number of iterations is not known beforehand and depends on a
certain condition being met.
while condition: # Block of code to be executed repeatedly pass
In [10]: #simple example where we use a while loop to print numbers from 1 to 5:
count = 1
while count <= 5:
print(count)
count += 1
1
2
3
4
5
In [11]: print(count)
In [12]: # example where we use a while loop to calculate the sum of numbers from 1 to 10:
total = 0
number = 1
while number <= 5:
total += number
number += 1
print(f'The total sum is: {total}')
The total sum is: 15
break Statement
localhost:8888/nbconvert/html/Python/Python/class 3.ipynb?download=false 3/7
7/8/24, 3:33 PM class 3
With the break statement we can stop the loop even if the while condition is true
In [13]: i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
1
2
3
continue Statement
With the continue statement we can stop the current iteration, and continue with the
next
In [14]: i = 0
while i < 10:
i += 1
if i == 7:
continue
print(i)
1
2
3
4
5
6
8
9
10
In [15]: count = 0
while count < 10:
count += 1
if count % 2 == 0:
continue # Skip the rest of the loop for even numbers
if count > 7:
break # Exit the loop if count is greater than 7
print(count)
1
3
5
7
In [16]: i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
localhost:8888/nbconvert/html/Python/Python/class 3.ipynb?download=false 4/7
7/8/24, 3:33 PM class 3
1
2
3
4
5
i is no longer less than 6
2. for Loop
The for loop in Python is used to iterate over a sequence (like a list, tuple, dictionary, set,
or string).
```python for item in sequence: # Do something with item
In [17]: numbers = [1, 2, 3, 4, 5]
for n in numbers:
square = n ** 2
print(f"The square of {n} is {square}")
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
In [18]: for i in "Apple":
print(i)
A
p
p
l
e
enumerate Function
The enumerate function in Python adds a counter to an iterable and returns it as an
enumerate object. This enumerate object can then be used directly in for loops to get both
the index and the value of each item in the iterable.
In [19]: istr = "aoprpalnegse"
# Initialize empty strings for odd and even index characters
oic = ""
eic = ""
for i, char in enumerate(istr):
if i% 2 ==0:
eic+=char
else:
oic+=char
In [20]: eic
'apples'
Out[20]:
In [21]: oic
localhost:8888/nbconvert/html/Python/Python/class 3.ipynb?download=false 5/7
7/8/24, 3:33 PM class 3
'orange'
Out[21]:
Nested Loops
In [22]: for i in range(2):
for j in range(2):
print(f"i = {i}, j = {j}")
i = 0, j = 0
i = 0, j = 1
i = 1, j = 0
i = 1, j = 1
In [23]: for num in range(10):
if num == 5:
break
print(num)
0
1
2
3
4
In [24]: for num in range(10):
if num == 5:
print(num)
break
In [25]: for num in range(10):
print(num)
if num == 5:
break
0
1
2
3
4
5
In [26]: for num in range(10):
if num == 5:
break
print(num)
In [27]: for num in range(10):
if num == 6:
continue
print(num)
0
1
2
3
4
5
7
8
9
localhost:8888/nbconvert/html/Python/Python/class 3.ipynb?download=false 6/7
7/8/24, 3:33 PM class 3
In [28]: for num in range(10):
print(num)
if num == 6:
continue
0
1
2
3
4
5
6
7
8
9
In [29]: for num in range(10):
if num == 6:
print(num)
continue
In [30]: for num in range(10):
if num == 6:
continue
print(num)
In [31]: n = 5
for i in range(n):
for j in range(n - i - 1):
print(" ", end="")
for k in range(2 * i + 1):
print("*", end="")
print()
*
***
*****
*******
*********
In [32]: X="MBUAMNBDARIA"
In [33]: X[::2]
'MUMBAI'
Out[33]:
In [34]: X[1::2]
'BANDRA'
Out[34]:
localhost:8888/nbconvert/html/Python/Python/class 3.ipynb?download=false 7/7