Unit2 Python
Unit2 Python
x = 10
if x > 5:
print("x is greater than 5")
x = 10
if x > 15:
print("x is greater than 15")
elif x == 10:
print("x is equal to 10")
else:
print("x is less than 10")
x = 10
y = 20
if x > 5:
if y > 15:
print("x is greater than 5 and y is greater than 15")
x = 10
y = 20
Syntex:-
if condition1:
if condition2:
# Code block to execute if both condition1 and condition2 are True
else:
# Code block to execute if condition1 is True but condition2 is False
else:
# Code block to execute if condition1 is False
Example1:-
x = 10
y = 20
if x > 5:
print("x is greater than 5")
if y > 15:
print("y is greater than 15")
else:
print("y is not greater than 15")
else:
print("x is not greater than 5")
elif Statement in Python:-
The elif statement is short for "else if." It allows you to check multiple
conditions sequentially and execute a block of code when the first matching
condition is found. The elif statement must always follow an if, and you can
have multiple elif blocks.
Syntax:-
if condition1:
# Code block to execute if condition1 is True
elif condition2:
# Code block to execute if condition1 is False and condition2 is True
elif condition3:
# Code block to execute if condition1 and condition2 are False and
condition3 is True
else:
# Code block to execute if all conditions are False
Example1 :-
marks = 85
if marks >= 90:
print("Grade: A+")
elif marks >= 80:
print("Grade: A")
elif marks >= 70:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
else:
print("Grade: F")
Difference Between elif and Nested if:-
result = 10 + 5 * 2
print(result) # Output: 20
Example:-
x = 0.1 + 0.2
print(x) # Output: 0.30000000000000004
y = 5.7
print(int(y)) # Output: 5
Purpose Loops:-
1. for loop
2. while loop
For Loops:-
A for loop is used to iterate over a sequence (like a list, tuple, string, or
range) and execute a block of code for each element in that sequence
Used when the number of iterations is known or you are iterating over a
sequence.
Syntax:-
for variable in sequence:
# Code block to execute
Example
numbers = [1, 2, 3, 4, 5]
Example 2
Output :
*
**
***
****
*****
Example 3
Question N.1:-
*
***
*****
*******
*********
The Number of rows would be entered by the user.
Programs:-
for i in range(num):
for j in range(0, (num-i-1)):
print(' ' , end="")
for k in range(0, 2*i+1):
print('*', end="")
print()
Question N.2:-
for i in range(num):
for j in range(0, (num-i-1)):
print(' ' , end="")
for k in range(0, 2*i+1):
print(i, end="")
print()
Question N.3:-
Program:-
for i in range(num):
for j in range(0, (num-i-1)):
print(' ' , end="")
for k in range(0, 2*i+1):
print(i+1, end="")
print()
Question N.4:-
Program:-
while Loop:-
while condition:
# Code block to execute
Example
count = 1
while count <= 5:
print(count)
count += 1
Output:-
1
2
3
4
5
Example:-
Question1. Ask the user to enter a number and calculate its factorial numbers.
Program:-
num = int(input("Enter the number whose factorial is required"))
fact = 1
i =1
while i <= num:
fact = fact *i
i =i +1
print('\ factorial of '+str(num)+'is '+str(fact))
Output:-
Example3:-
Question1. Ask the user to enter two number ‘a’ and ‘b’ and calculate ‘a’ to the
power of ‘b’.
Program:-
a = int(input("Enter the first number"))
b = int(input("Enter the Second number"))
power =1
i =1
while i<=b:
power = power *a
i = i+1
else:
print(str(a)+' to the power of '+str(b)+' is '+str(power))
Output:-
Program:-
number = int (input ("Enter the value for x (where x > 2) ?"))
x1=0
x2 = 1
count = 2
if number <= 0 :
print("Please enter positive integer")
elif number == 1 :
print("Fibonacci sequence is :")
print(x1)
else :
print("Fibonacci sequence is :")
print(x1," ", x2)
while count < number :
xth = x1 + x2
print (xth)
x1 = x2
x2 = xth
count += 1
Output:
Enter the value for x (where x > 2) ?10
Fibonacci sequence is :
0
1
1
2
3
5
8
13
21
34
Controlling Loops:-
break: Exits the loop immediately, even if the condition has not yet been
met.
continue: Skips the current iteration and moves to the next iteration of the
loop.
pass: Does nothing; used as a placeholder for future code.
Example of break:
for i in range(10):
if i == 5:
break
print(i)
Example of continue:
for i in range(5):
if i == 2:
continue
print(i)
Output:-
0
1
3
4
Infinite Loop:-
If the condition in a while loop is always True, it creates an infinite loop, which
continues running forever (unless interrupted).
Example of an Infinite Loop:-
while True:
print("This loop will run forever")
Output:-
1. Use for loop: When you know in advance how many iterations you need,
or when iterating over a sequence like a list or a string.
2. Use while loop: When the number of iterations is not known in
advance and depends on some condition being True or False.
A nested loop is when one loop is placed inside another loop. In Python, this
allows for the execution of repeated actions within each iteration of an outer
loop. Each time the outer loop runs, the inner loop runs completely, creating a
loop within a loop.
1. Nested for loops: When a for loop is inside another for loop.
2. Nested while loops: When a while loop is inside another while loop.
3. Mixed loops: When a for loop is inside a while loop or vice versa.
Nested for loops are useful when you need to work with multi-dimensional data
like matrices, grids, or lists of lists.
Output:-
Output:-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Nested while loops can be used when both loops depend on certain conditions,
and both conditions must be met for the inner loop to execute.
i=1
while i <= 3: # Outer loop
j=1
while j <= 3: # Inner loop
print(f"i = {i}, j = {j}")
j += 1 # Increment inner loop counter
i += 1 # Increment outer loop counter
Output:-
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3
You can mix for and while loops, depending on the problem's requirements.
i=1
while i <= 3: # Outer `while` loop
for j in range(1, 4): # Inner `for` loop
print(f"i = {i}, j = {j}")
i += 1 # Increment outer loop counter
Output:-
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3