Back to Home

Conditionals

Conditionals

By default, statements in Python script are executed sequentially from top to bottom. If the processing logic require so, the sequential flow of execution can be altered in two way:

If Condition

In python and other programming languages the key word if is used to check if a condition is true and to execute the block code. Remember the indentation after the colon.

# syntax
if condition:
    this part of code runs for truthy conditions

Example: 1

a = 3
if a > 0:
    print('A is a positive number')
# A is a positive number

As you can see in the example above, 3 is greater than 0. The condition was true and the block code was executed. However, if the condition is false, we do not see the result. In order to see the result of the falsy condition, we should have another block, which is going to be else.

If Else

If condition is true the first block will be executed, if not the else condition will run.

# syntax
if condition:
    this part of code runs for truthy conditions
else:
     this part of code runs for false conditions

Example:

a = 3
if a < 0:
    print('A is a negative number')
else:
    print('A is a positive number')

The condition above proves false, therefore the else block was executed. How about if our condition is more than two? We could use elif.

If Elif Else

In our daily life, we make decisions on daily basis. We make decisions not by checking one or two conditions but multiple conditions. As similar to life, programming is also full of conditions. We use elif when we have multiple conditions.

# syntax
if condition:
    code
elif condition:
    code
else:
    code

Example:

a = 0
if a > 0:
    print('A is a positive number')
elif a < 0:
    print('A is a negative number')
else:
    print('A is zero')

Short Hand

# syntax
code if condition else code

Example:

a = 3
print('A is positive') if a > 0 else print('A is negative') # first condition met, 'A is positive' will be printed

Nested Conditions

Conditions can be nested

# syntax
if condition:
    code
    if condition:
    code

Example:

a = 0
if a > 0:
    if a % 2 == 0:
        print('A is a positive and even integer')
    else:
        print('A is a positive number')
elif a == 0:
    print('A is zero')
else:
    print('A is a negative number')

We can avoid writing nested condition by using logical operator and.

If Condition and Logical Operators

# syntax
if condition and condition:
    code

Example:

a = 0
if a > 0 and a % 2 == 0:
        print('A is an even and positive integer')
elif a > 0 and a % 2 !=  0:
     print('A is a positive integer')
elif a == 0:
    print('A is zero')
else:
    print('A is negative')

If and Or Logical Operators

# syntax
if condition or condition:
    code

Example:

user = 'James'
access_level = 3
if user == 'admin' or access_level >= 4:
        print('Access granted!')
else:
    print('Access denied!')

🌕 Now do some exercises for your brain and muscles.

đź’» Exercises:

  1. Write a Python program that takes an integer as input and prints whether it is even or odd.

  2. Write a program to check if a given number is positive, negative, or zero.

  3. Ask the user for their age.

    If the age is less than 13 → print “Child”.

    If between 13 and 19 → print “Teenager”.

    Otherwise → print “Adult”

  4. Write a program that takes two numbers and prints the larger one.

  5. Take a single character as input. If it’s a vowel (a, e, i, o, u), print “Vowel”, else print “Consonant”.

  6. Write a program to check if a given year is a leap year or not. (Hint: divisible by 4, but not by 100 unless also divisible by 400.)

  7. Take a score (0–100) as input and print the grade:

    90–100: A

    80–89: B

    70–79: C

    60–69: D

    Below 60: F

  8. Ask the user for a password.

    If it’s less than 6 characters → “Weak password”.

    If between 6 and 10 → “Moderate password”.

    Otherwise → “Strong password”.

  9. Given three sides of a triangle, determine whether the triangle is valid or not. (Hint: Sum of any two sides must be greater than the third.)

  10. Simulate a simple ATM condition:

    Ask for a PIN.

    If the PIN is correct, ask for the withdrawal amount.

    If the amount ≤ balance, print “Transaction Successful”, else print “Insufficient Balance.”

    If the PIN is incorrect, print “Invalid PIN.”