Nested if else in python exercises pdf

Debbie 30 Published: 07/24/2024

Nested if else in python exercises pdf

I'll respond in English as per your request.

Nested If Else Statements in Python Exercises PDF:

Exercise 1:

Write a program that takes an integer as input and prints the number of days it corresponds to (assuming one month has 30 days).

def num_days(n):

if n <= 0:

print("Invalid input")

elif n % 12 == 0 or (n % 4 == 0 and n % 100 != 0):

print(f"{n} is a leap year with {29*n//28} days")

else:

print(f"{n} has {30*(n-1)//12 + 1} days")

num_days(int(input("Enter the number of years: ")))

Exercise 2:

Write a program that takes three numbers as input and prints whether they form an isosceles triangle or not.

def check_triangle(a, b, c):

if a + b > c and a + c > b and b + c > a:

if a == b or b == c or a == c:

print("The given numbers form an isosceles triangle")

else:

print("The given numbers do not form an isosceles triangle")

else:

print("The given numbers do not form a triangle")

a, b, c = map(int, input("Enter the three sides of the triangle: ").split())

check_triangle(a, b, c)

Exercise 3:

Write a program that takes an integer as input and prints whether it is prime or not.

def check_prime(n):

if n <= 1:

print(f"{n} is not a prime number")

elif n <= 3:

print(f"{n} is a prime number")

else:

for i in range(2, int(n**0.5) + 1):

if n % i == 0:

print(f"{n} is not a prime number")

return

print(f"{n} is a prime number")

num = int(input("Enter the number: "))

check_prime(num)

Exercise 4:

Write a program that takes an integer as input and prints the factorial of it.

def calculate_factorial(n):

if n < 0:

print("Factorials are not defined for negative numbers")

elif n == 0 or n == 1:

print(f"The factorial of {n} is 1")

else:

result = 1

for i in range(2, n + 1):

result *= i

print(f"The factorial of {n} is {result}")

num = int(input("Enter the number: "))

calculate_factorial(num)

Exercise 5:

Write a program that takes an integer as input and prints whether it is odd or even.

def check_odd_even(n):

if n % 2 == 0:

print(f"{n} is an even number")

else:

print(f"{n} is an odd number")

num = int(input("Enter the number: "))

check_odd_even(num)

Exercise 6:

Write a program that takes three strings as input and prints whether they are anagrams or not.

def check_anagram(s1, s2, s3):

if set(s1.lower()) != set(s2.lower()) or set(s1.lower()) != set(s3.lower()):

print("The given strings are not anagrams")

else:

print("The given strings are anagrams")

s1, s2, s3 = input("Enter the three strings: ").split()

check_anagram(s1, s2, s3)

These exercises cover various scenarios where nested if-else statements can be used in Python. They involve logical conditions based on mathematical operations, string manipulations, and more.

What is the difference between elif and if in python using

I'll respond in English for you!

Python has two control structures that are often confused: if and elif. Both statements are used to make decisions based on conditions, but they serve different purposes.

The if statement

An if statement is used when you want to execute a block of code if a specific condition is met. The basic syntax is:

if condition:
code to be executed if condition is True

For example:

x = 5

if x > 10:

print("x is greater than 10")

In this case, the code inside the if block will only run if the condition x > 10 is True. If the condition is False, the code will skip and move on to the next statement.

The elif statement

An elif (short for "else if") statement is used when you want to check another condition if the initial condition is not met. The basic syntax is:

if condition:
code to be executed if condition is True

elif other_condition:

code to be executed if first condition is False and second condition is True

For example:

x = 5

if x > 10:

print("x is greater than 10")

elif x == 5:

print("x is equal to 5")

else:

print("x is less than 5")

In this case, the code inside the if block will run if x > 10. If not, the code inside the elif block will run if x == 5. Finally, if neither condition is met, the code inside the else block will run.

Key differences between if and elif

Purpose: if statements are used to execute code when a specific condition is met, while elif statements are used to check another condition if the initial condition fails. ** Syntax**: The main difference in syntax is that elif is used to chain multiple conditions together, whereas an if statement only checks one condition at a time.

To summarize:

Use if when you want to execute code based on a single condition. Use elif when you want to check another condition if the initial condition fails.

In Python programming, both statements are essential for making decisions and controlling the flow of your program. By understanding their differences and when to use each, you can write more efficient and effective code!