Wednesday, 6 September 2023

Top 10 Python if .. else programs

Python if else statement is one of the popular decision making statement. Refer following top 10 python programs based on if..else statement.

Top 10 Python if else Programs

1. Age Classifier: Classify a person's age into categories like child, teenager, or adult.

age = int(input("Enter your age: "))
if age < 18:
    print("Child/Teenager")
else:
    print("Adult")

Output:
Enter your age: 16 Child/Teenager

2. Positive, Negative, or Zero: Check if a number is positive, negative, or zero.

num = float(input("Enter a number: "))
if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")

Output:
Enter a number: 5 Positive

3. Even or Odd Checker: Check if a number is even or odd.

num = int(input("Enter a number: "))
if num % 2 == 0:
    print("It's Even")
else:
    print("It's Odd")

Output:
Enter a number: 6 It's Even

4. Grade Calculator: Calculate and display the grade based on a student's score.

score = int(input("Enter your score: "))
if score >= 90:
    print("A Grade")
elif score >= 80:
    print("B Grade")
elif score >= 70:
    print("C Grade")
else:
    print("F Grade")

Output:
Enter your score: 85 B Grade

5. Largest Number Finder: Find the largest among three numbers.

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
if num1 >= num2 and num1 >= num3:
    print("Largest number is", num1)
elif num2 >= num1 and num2 >= num3:
    print("Largest number is", num2)
else:
    print("Largest number is", num3)

Output:
Enter the first number: 10 Enter the second number: 11 Enter the third number: 12.5 Largest number is 12.5

6. Vowel or Consonant: Determine if a character is a vowel or consonant.

char = input("Enter a character: ")
if char in 'aeiouAEIOU':
    print("It's Vowel")
else:
    print("It's Consonant")

Output:
Enter a character: a It's Vowel

7. Positive or Negative Number List: Separate positive and negative numbers in a list.

numbers = [1, -2, 3, -4, 5, -6]
positive = []
negative = []
for num in numbers:
    if num >= 0:
        positive.append(num)
    else:
        negative.append(num)
print("Positive numbers:", positive)
print("Negative numbers:", negative)

Output:
Positive numbers: [1, 3, 5] Negative numbers: [-2, -4, -6]

8. User Authentication: Authenticate a user based on a username and password.

username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin" and password == "password":
    print("Login successful")
else:
    print("Login failed")

Output:
Enter username: admin Enter password: password Login successful

9. Triangle Type Checker: Determine the type of triangle based on its sides.

a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))

if a == b == c:
    print("Equilateral triangle")
elif a == b or b == c or a == c:
    print("Isosceles triangle")
else:
    print("Scalene triangle")

Output:
Enter side a: 6 Enter side b: 6 Enter side c: 6 Equilateral triangle

10. Check if a string is a palindrome.

string = input("Enter a string: ")
reversed_string = string[::-1]
if string == reversed_string:
  print("The string is a palindrome.")
else:
  print("The string is not a palindrome.")

Output:
Enter a string: ABCBA The string is a palindrome.



No comments:

Post a Comment