Check a lowercase character

Check a lowercase character

EasyIf-Else Programs · 24 of 64

Write a Python program to check whether a character is lowercase.

Input
"c", "Q"
Output
c True
Q False

Approach

  1. Use islower(), or compare with the range “a” to “z”.

Solution

Python
for ch in ["c", "Q"]:    if "a" <= ch <= "z":        print(ch, True)    else:        print(ch, False)
Output
c True
Q False

Line by line

Line 2

Characters compare by their Unicode order, so a chained comparison checks the a-z range. ch.islower() works too and also handles non-English letters.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Larger of two numbers

Larger of two numbers

EasyIf-Else Programs · 22 of 64

Write a Python program to print the larger of two numbers.

Input
25, 63
Output
63

Approach

  1. Compare them with >; handle equal numbers too.

Solution

Python
a, b = 25, 63if a > b:    print(a)elif b > a:    print(b)else:    print("Both are equal:", a)
Output
63

Line by line

Lines 6-7

Equal numbers get their own message.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Positive or negative, even or odd

Positive or negative, even or odd

EasyIf-Else Programs · 21 of 64

Write a Python program to check whether a number is positive or negative and even or odd.

Input
26, -7, 0
Output
26 is positive and even
-7 is negative and odd
0 is zero and even

Approach

  1. Decide the sign with if-elif-else.
  2. Decide parity with a conditional expression.

Solution

Python
for n in [26, -7, 0]:    if n > 0:        sign = "positive"    elif n < 0:        sign = "negative"    else:        sign = "zero"    parity = "even" if n % 2 == 0 else "odd"    print(f"{n} is {sign} and {parity}")
Output
26 is positive and even
-7 is negative and odd
0 is zero and even

Line by line

Lines 2-7

Three branches cover positive, negative and zero.

Line 8

A one-line conditional expression picks “even” or “odd”.

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 negative

Check if a number is negative

EasyIf-Else Programs · 20 of 64

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

Input
-45, 12
Output
-45 True
12 False

Approach

  1. Compare with 0 using <.

Solution

Python
for n in [-45, 12]:    is_negative = n < 0    print(n, is_negative)
Output
-45 True
12 False

Line by line

Line 2

A comparison already produces a boolean, so storing it directly is the simplest form of this if-else.

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 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 a palindrome number

Check a palindrome number

EasyIf-Else Programs · 16 of 64

Write a Python program to check whether a number is a palindrome.

Input
121, 123
Output
121 is a palindrome
123 is not a palindrome

Approach

  1. Compare the number’s digits with their reverse.

Solution

Python
for n in [121, 123]:    if str(n) == str(n)[::-1]:        print(n, "is a palindrome")    else:        print(n, "is not a palindrome")
Output
121 is a palindrome
123 is not a palindrome

Line by line

Line 2

str(n)[::-1] reverses the digits as text.

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