Check if a number is positive

Check if a number is positive

EasyIf-Else Programs · 19 of 64

Write a Python program to check whether a number is positive and print True or False.

Input
20, -5, 0
Output
20 True
-5 False
0 False

Approach

  1. A positive number is greater than 0; the comparison itself gives True or False.

Solution

Python
for n in [20, -5, 0]:    if n > 0:        print(n, True)    else:        print(n, False)
Output
20 True
-5 False
0 False

Line by line

Line 2

0 is neither positive nor negative, so it prints False.

Tip

print(n > 0) gives the same result without an if-else.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Pass or fail

Pass or fail

EasyIf-Else Programs · 18 of 64

Write a Python program that reads marks and prints Pass if they are greater than 35, otherwise Fail.

Input
45
Output
Enter marks: 45
Pass

Approach

  1. Read the marks with input() and convert to int.
  2. Compare with 35.

Solution

Python
marks = int(input("Enter marks: "))if marks > 35:    print("Pass")else:    print("Fail")
Outputtyped input: 45
Enter marks: 45
Pass

Line by line

Line 1

input() returns text; int() converts it.

Line 2

The task says greater than 35, so exactly 35 fails.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Check a palindrome string

Check a palindrome string

EasyIf-Else Programs · 17 of 64

Write a Python program to check whether a string is a palindrome, ignoring case.

Input
"jaj", "Madam", "python"
Output
jaj is a palindrome
Madam is a palindrome
python is not a palindrome

Approach

  1. Lower-case the text and compare it with its reverse.

Solution

Python
for word in ["jaj", "Madam", "python"]:    text = word.lower()    if text == text[::-1]:        print(word, "is a palindrome")    else:        print(word, "is not a palindrome")
Output
jaj is a palindrome
Madam is a palindrome
python is not a palindrome

Line by line

Line 2

Lower-casing makes “Madam” read the same backwards.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Check voting eligibility

Check voting eligibility

EasyIf-Else Programs · 15 of 64

Write a Python program to check whether a person is eligible to vote (age 18 or above).

Input
17, 18, 45
Output
17 -> not eligible, wait 1 year(s)
18 -> eligible to vote
45 -> eligible to vote

Approach

  1. Compare the age with 18 using >=.

Solution

Python
for age in [17, 18, 45]:    if age >= 18:        print(age, "-> eligible to vote")    else:        print(age, "-> not eligible, wait", 18 - age, "year(s)")
Output
17 -> not eligible, wait 1 year(s)
18 -> eligible to vote
45 -> eligible to vote

Line by line

Line 2

>= includes exactly 18, which is the legal voting age in India.

Line 5

The else branch also tells how long to wait.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Largest of three numbers

Largest of three numbers

EasyIf-Else Programs · 14 of 64

Write a Python program to find the largest of three numbers.

Input
a = 45, b = 89, c = 23
Output
Largest: 89

Approach

  1. Compare each number with the other two using >=.

Solution 1: if-elif

Python
a, b, c = 45, 89, 23 if a >= b and a >= c:    largest = aelif b >= a and b >= c:    largest = belse:    largest = cprint("Largest:", largest)
Output
Largest: 89

Line by line

Line 3

a is the largest if it is at least as big as both others. >= handles equal values.

Lines 7-8

If neither a nor b is the largest, it must be c.

Solution 2: max()

Python
print(max(45, 89, 23))
Output
89

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Check if a number is in a list

Check if a number is in a list

EasyIf-Else Programs · 13 of 64

Write a Python program to check whether a given number is present in a list of numbers.

Input
numbers = [12, 45, 7, 89, 33]; check 89 and 50
Output
89 found at index 3
50 is not in the list

Approach

  1. Use in; use index() to also report the position.

Solution

Python
numbers = [12, 45, 7, 89, 33] for n in [89, 50]:    if n in numbers:        print(n, "found at index", numbers.index(n))    else:        print(n, "is not in the list")
Output
89 found at index 3
50 is not in the list

Line by line

Line 5

index() is safe here because the if already confirmed the number is present.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Interview process with nested if-else

Interview process with nested if-else

EasyIf-Else Programs · 12 of 64

Write a Python program that describes a two-round interview: a candidate who passes round 1 can take round 2, and passing both means placed. Use nested if-else.

Input
("passed", "passed"), ("passed", "failed"), ("failed", "-")
Output
--- round1=passed, round2=passed
Round 1 cleared
Round 2 cleared: you are placed
--- round1=passed, round2=failed
Round 1 cleared
Failed in round 2, please try next time
--- round1=failed, round2=-
Failed in round 1, please try next time

Approach

  1. The outer if checks round 1.
  2. Only inside it does an inner if check round 2.

Solution

Python
def interview(round1, round2):    if round1 == "passed":        print("Round 1 cleared")        if round2 == "passed":            print("Round 2 cleared: you are placed")        else:            print("Failed in round 2, please try next time")    else:        print("Failed in round 1, please try next time") for r1, r2 in [("passed", "passed"), ("passed", "failed"), ("failed", "-")]:    print(f"--- round1={r1}, round2={r2}")    interview(r1, r2)
Output
--- round1=passed, round2=passed
Round 1 cleared
Round 2 cleared: you are placed
--- round1=passed, round2=failed
Round 1 cleared
Failed in round 2, please try next time
--- round1=failed, round2=-
Failed in round 1, please try next time

Line by line

Line 2

The outer condition decides whether round 2 matters at all.

Lines 4-7

The inner if-else only runs for candidates who passed round 1.

Lines 8-9

A failed round 1 skips round 2 entirely.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Square or cube depending on divisibility

Square or cube depending on divisibility

EasyIf-Else Programs · 11 of 64

Write a Python program that prints the square of a number if it is divisible by 2, or its cube if it is divisible by 3.

Input
4, 9, 6, 7
Output
4 square: 16
9 cube: 729
6 square: 36
7 is divisible by neither 2 nor 3

Approach

  1. Check divisibility by 2 first, then by 3.

Solution

Python
for n in [4, 9, 6, 7]:    if n % 2 == 0:        print(n, "square:", n ** 2)    elif n % 3 == 0:        print(n, "cube:", n ** 3)    else:        print(n, "is divisible by neither 2 nor 3")
Output
4 square: 16
9 cube: 729
6 square: 36
7 is divisible by neither 2 nor 3

Line by line

Lines 2-5

6 is divisible by both; because the % 2 test comes first, it prints the square.

Tip

To print both for numbers like 6, use two separate if statements instead of if / elif.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Validate a user id in a list

Validate a user id in a list

EasyIf-Else Programs · 10 of 64

Write a Python program to check whether a user id is present in a list of user ids.

Input
ids = [101, 102, 205, 309]; check 205 and 404
Output
205 is a valid user id
404 is not a valid user id

Approach

  1. Use the in operator to test membership.

Solution

Python
user_ids = [101, 102, 205, 309] for uid in [205, 404]:    if uid in user_ids:        print(uid, "is a valid user id")    else:        print(uid, "is not a valid user id")
Output
205 is a valid user id
404 is not a valid user id

Line by line

Line 4

in checks every item of the list and returns True or False.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Check username and password

Check username and password

EasyIf-Else Programs · 9 of 64

Write a Python program to check login with a given username and password.

Input
("admin", "Admin@123"), ("admin", "wrong"), ("guest", "x")
Output
admin -> Login successful
admin -> Wrong password
guest -> Unknown user

Approach

  1. Compare both values; give a different message for a wrong user and a wrong password.

Solution

Python
USERNAME = "admin"PASSWORD = "Admin@123" def login(user, pwd):    if user == USERNAME and pwd == PASSWORD:        return "Login successful"    elif user == USERNAME:        return "Wrong password"    else:        return "Unknown user" for u, p in [("admin", "Admin@123"), ("admin", "wrong"), ("guest", "x")]:    print(u, "->", login(u, p))
Output
admin -> Login successful
admin -> Wrong password
guest -> Unknown user

Line by line

Lines 1-2

The expected credentials are constants (upper-case names).

Line 5

Both must match, so the checks are joined with and.

Line 7

Reaching this line means the password was wrong for a known user.

Watch out

Real applications never store plain-text passwords; they store a salted hash and compare hashes. This program only practises if-else logic.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs