DEV Community

Kamalesh AR
Kamalesh AR

Posted on

If-else in python

Practiced questions: Easy level

1. Positive,negative,zero:

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

2. Odd or even: [TBD]

here i have doubt about 0 is even or odd

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

if num % 2 == 0:
    print("Even")
else:
    print("Odd")
Enter fullscreen mode Exit fullscreen mode

3. Largest of two numbers:

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a > b:
    print("Largest:", a)
elif b > a:
    print("Largest:", b)
else:
    print("Both are equal")
Enter fullscreen mode Exit fullscreen mode

4. Eligible to vote:

age = int(input("Enter your age: "))

if age >= 18:
    print("Eligible to Vote")
else:
    print("Not Eligible to Vote")
Enter fullscreen mode Exit fullscreen mode

5. Pass or Fail:

marks = int(input("Enter your marks: "))

if marks >= 35:
    print("Pass")
else:
    print("Fail")
Enter fullscreen mode Exit fullscreen mode

6.Divisible by 5:

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

if num % 5 == 0:
    print("Divisible by 5")
else:
    print("Not Divisible by 5")
Enter fullscreen mode Exit fullscreen mode

7. Leap Year: [TBD]
can i use | replace for 'or'?

year = int(input("Enter a year: "))

if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
    print("Leap Year")
else:
    print("Not a Leap Year")
Enter fullscreen mode Exit fullscreen mode

8. Character type:[TBD]

ch = input("Enter a character:")

if ch.isupper():
    print("Uppercase Letter")
elif ch.islower():
    print("Lowercase Letter")
elif ch.isdigit():
    print("Digit")
else:
    print("Special Character")
Enter fullscreen mode Exit fullscreen mode

9. Greatest of Three Numbers:

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:
    print("Greatest:", a)
elif b >= a and b >= c:
    print("Greatest:", b)
else:
    print("Greatest:", c)
Enter fullscreen mode Exit fullscreen mode

10. Temperature check:

temp = float(input("Enter temperature: "))

if temp > 35:
    print("Hot")
elif temp >= 20:
    print("Normal")
else:
    print("Cold")
Enter fullscreen mode Exit fullscreen mode

Intermediate level:

1. Grade Calculation:

marks = int(input("Enter your marks: "))

if marks >= 90:
    print("Grade A")
elif marks >= 80:
    print("Grade B")
elif marks >= 70:
    print("Grade C")
elif marks >= 60:
    print("Grade D")
else:
    print("Grade F")
Enter fullscreen mode Exit fullscreen mode

2. Simple calci:[TBD]

num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))

if operator == "+":
    print("Result:", num1 + num2)
elif operator == "-":
    print("Result:", num1 - num2)
elif operator == "*":
    print("Result:", num1 * num2)
elif operator == "/":
    if num2 != 0:
        print("Result:", num1 / num2)
    else:
        print("Cannot divide by zero")
else:
    print("Invalid operator")
Enter fullscreen mode Exit fullscreen mode

3. Find the Largest of Four Numbers:[TBD]

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
d = int(input("Enter fourth number: "))

largest = a

if b > largest:
    largest = b

if c > largest:
    largest = c

if d > largest:
    largest = d

print("Largest number is:", largest)
Enter fullscreen mode Exit fullscreen mode

4. ATM withdrawal:

balance = 10000

amount = int(input("Enter withdrawal amount: "))

if amount <= balance:
    balance = balance - amount
    print("Withdrawal Successful")
    print("Remaining Balance:", balance)
else:
    print("Insufficient Balance")
Enter fullscreen mode Exit fullscreen mode

5. Electricity Bill:

units = int(input("Enter electricity units: "))

if units <= 100:
    bill = units * 2
elif units <= 200:
    bill = (100 * 2) + ((units - 100) * 3)
else:
    bill = (100 * 2) + (100 * 3) + ((units - 200) * 5)

print("Electricity Bill = โ‚น", bill)
Enter fullscreen mode Exit fullscreen mode

Hard level:

1. Banking loan:[TBD]
For can use if-else step by step

age = int(input("Enter your age: "))
salary = int(input("Enter your monthly salary: "))
credit_score = int(input("Enter your credit score: "))

if age >= 21:
    if salary >= 25000:
        if credit_score >= 700:
            print("Loan Approved")
        else:
            print("Loan Rejected: Low Credit Score")
    else:
        print("Loan Rejected: Salary Too Low")
else:
    print("Loan Rejected: Age Below 21")
Enter fullscreen mode Exit fullscreen mode

2. Scholarship Eligibility:

marks = int(input("Enter marks: "))
income = int(input("Enter family income: "))

if marks >= 90 and income < 300000:
    print("Scholarship Approved")
else:
    print("Scholarship Not Approved")
Enter fullscreen mode Exit fullscreen mode

3. Student attendance eligibility:[TBD]

total = int(input("Total classes: "))
attended = int(input("Classes attended: "))

attendance = (attended / total) * 100

print("Attendance:", round(attendance, 2), "%")

if attendance >= 75:
    print("Eligible for Exam")
else:
    print("Not Eligible")
Enter fullscreen mode Exit fullscreen mode

4. Cricket match result:

team_a = int(input("Team A Score: "))
team_b = int(input("Team B Score: "))

if team_a > team_b:
    print("Team A Wins")
elif team_b > team_a:
    print("Team B Wins")
else:
    print("Match Tied")
Enter fullscreen mode Exit fullscreen mode

5. Movie age restriction:

age = int(input("Enter age: "))

if age < 13:
    print("Category: U")
elif age < 18:
    print("Category: U/A")
else:
    print("Category: A")
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
vidhya_murali_5aabe7784bd profile image
vidhya murali

Very useful . Thank you

Collapse
 
kamalesh_ar_6252544786997 profile image
Kamalesh AR

Thank you