Solutions to If Else Problems


In Python, if-else statements are used for conditional execution of code. They allow you to execute a block of code if a certain condition is met, and another block of code if the condition is not met.

The basic syntax of the if-else statement in Python is as follows:



if condition:
    # code to execute if condition is True
else:
    # code to execute if condition is False



Here, condition is the expression that is evaluated to a Boolean value (True or False). If condition is True, the code in the first block is executed, and if condition is False, the code in the second block is executed.

It is also possible to chain multiple if-else statements together to create more complex logic. This is known as an if-elif-else ladder, and the syntax is as follows:




if condition1:
    # code to execute if condition1 is True
elif condition2:
    # code to execute if condition1 is False and condition2 is True
else:
    # code to execute if both condition1 and condition2 are False

Here, condition is the expression that is evaluated to a Boolean value (True or False). If condition is True, the code in the first block is executed, and if condition is False, the code in the second block is executed.

It is also possible to chain multiple if-else statements together to create more complex logic. This is known as an if-elif-else ladder, and the syntax is as follows:


if condition1:
    # code to execute if condition1 is True
elif condition2:
    # code to execute if condition1 is False and condition2 is True
else:
    # code to execute if both condition1 and condition2 are False


In this case, if condition1 is True, the code in the first block is executed, and if condition1 is False and condition2 is True, the code in the second block is executed. If both condition1 and condition2 are False, the code in the third block is executed.


Overall, if-else statements are a fundamental construct in Python programming that allow you to control the flow of your code based on certain conditions. With an understanding of how they work, you can write more sophisticated and flexible programs.


Solutions to If Else Problems

  1. Find the max of 3 numbers
  2. Check for a leap year
  3. Check for the type of a triangle
  4. Given the month and year find the days in the month
  5. Find the weekday given the week day no
  6. Solve a quadratic equation
  7. Find the division given marks in 3 subjects
  8. Check for odd and even
  9. Find the middle of 3 numbers
  10. Find the max of 4 numbers

# Find the maximum of 3 numbers
a, b, c = 2, 1, 3

# Using if else ladder
if a >= b and a >= c:
highest = a
elif b >= c:
highest = b
else:
highest = c

# Print the highest number
print('Highest is', highest)

# Find the maximum of 3 numbers using nested if statements
if a >= b:
if a >= c:
highest = a
else:
highest = c
else:
if b >= c:
highest = b
else:
highest = c

# Print the highest number
print('Highest is', highest)

# Check if a year is a leap year
year = 2023
if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
print(year, "is a Leap Year")
else:
print(year, "is not a Leap Year")

# Check the type of a triangle
a, b, c = 3, 3, 2
count = 0

# Count the number of equal sides
if a == b:
count += 1
if a == c:
count += 1
if b == c:
count += 1

# Check the type of triangle based on the number of equal sides
if count == 3:
print("Equilateral")
elif count == 1:
print("Isosceles")
else:
print("Scalene")

# Given a month and year, find the number of days in the month
month = 2
year = 2020
if month in (1, 3, 5, 7, 8, 10, 12):
print(31)
elif month in (4, 6, 9, 11):
print(30)
elif month == 2:
if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
print(29)
else:
print(28)
else:
print("Invalid Month")



Try the rest.

Contact us for software training, education or development










 

Post a Comment

0 Comments

Me