Python Foundations

Booleans, Conditions and Operators

Learn how Python thinks using True, False, relational operators, logical operators, if else, elif, nested if, and ternary expressions.

1. Overview

In the first stage of Python, we learn printing, input, variables, datatypes, and calculations. Now we make Python take decisions.

A program becomes useful when it can ask a question and choose what to do next.

Booleans

Booleans represent answers like True or False.

Comparisons

Relational operators compare values and return a Boolean result.

Conditions

If else statements allow the program to choose one path from many paths.

2. Booleans in Python

A Boolean has only two possible values: True or False.

is_raining = True
is_sunny = False

print(is_raining)
print(is_sunny)
Output:
True
False
Important: In Python, write True and False with capital first letters. Do not write true or false.

Boolean From a Comparison

age = 18

print(age >= 18)
print(age < 18)
Output:
True
False

3. Relational Operators

Relational operators compare two values. Their answer is always either True or False.

Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 8 > 3 True
< Less than 2 < 7 True
>= Greater than or equal to 18 >= 18 True
<= Less than or equal to 10 <= 5 False

Example

a = 10
b = 20

print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= 10)
print(b <= 20)
Output:
False
True
False
True
True
True
Checkpoint: = means assignment. == means comparison.
x = 5      # assignment
x == 5     # comparison

4. Logical Operators

Logical operators combine multiple conditions.

Operator Meaning Example
and True only when both conditions are True age >= 18 and has_id
or True when at least one condition is True cash > 0 or has_card
not Reverses the Boolean value not is_raining

Using and

age = 20
has_id = True

print(age >= 18 and has_id)
Output:
True

Using or

cash = 0
has_card = True

print(cash > 0 or has_card)
Output:
True

Using not

is_raining = False

print(not is_raining)
Output:
True

5. Truth Tables

Truth tables show how logical operators behave for every possible combination of values.

and

A B A and B
True True True
True False False
False True False
False False False

or

A B A or B
True True True
True False True
False True True
False False False

not

A not A
True False
False True

6. The if Statement

The if statement runs a block of code only when a condition is true.

marks = 70

if marks >= 33:
    print("Pass")
Output:
Pass
Python uses indentation to decide which statements belong inside the if block.

7. The if else Statement

if else is used when there are two possible paths.

marks = 25

if marks >= 33:
    print("Pass")
else:
    print("Fail")
Output:
Fail

Since marks are less than 33, the else block runs.

8. The if elif else Statement

elif means else if. It is used when there are many possible conditions.

marks = 82

if marks >= 90:
    print("Grade A+")
elif marks >= 75:
    print("Grade A")
elif marks >= 60:
    print("Grade B")
elif marks >= 33:
    print("Grade C")
else:
    print("Fail")
Output:
Grade A

Python checks conditions from top to bottom. Once one condition becomes true, that block runs and the remaining conditions are skipped.

9. Nested if

An if inside another if is called a nested if.

age = 21
has_id = True

if age >= 18:
    if has_id:
        print("Entry allowed")
    else:
        print("ID required")
else:
    print("Entry not allowed")
Output:
Entry allowed
Nested conditions are useful, but too many nested levels can make code difficult to read.

10. Ternary Operator

A ternary operator is a short form of if else.

Normal if else

age = 20

if age >= 18:
    status = "Adult"
else:
    status = "Minor"

print(status)

Ternary form

age = 20

status = "Adult" if age >= 18 else "Minor"

print(status)
Output:
Adult

The structure is:

value_if_true if condition else value_if_false

More Examples

number = 10

result = "Even" if number % 2 == 0 else "Odd"

print(result)
Output:
Even
marks = 45

result = "Pass" if marks >= 33 else "Fail"

print(result)
Output:
Pass
Checkpoint: Use ternary operators for simple decisions. For complex decisions, use normal if else.

11. Truthy and Falsy Values

Some values behave like False in Python. These are called falsy values.

Value Boolean Meaning
0 False
"" empty string False
[] empty list False
{} empty dictionary False
None False

Non-zero and non-empty values usually behave like True.

print(bool(0))
print(bool(1))
print(bool(""))
print(bool("Python"))
print(bool([]))
print(bool([10, 20]))
Output:
False
True
False
True
False
True

12. Mini Projects

Number Checker

Check whether a number is positive, negative, or zero.

Eligibility Checker

Use age and marks to decide whether a person is eligible.

Greatest Number

Find the greater value using both if else and ternary operator.

Mini Project 1: Number Checker

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

if num > 0:
    print("Positive number")
elif num < 0:
    print("Negative number")
else:
    print("Zero")

Mini Project 2: Simple Eligibility Checker

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

if age >= 18 and marks >= 50:
    print("You are eligible")
else:
    print("You are not eligible")

Mini Project 3: Greatest of Two Numbers

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

if a > b:
    print("Greater number is", a)
else:
    print("Greater number is", b)

Same Project Using Ternary Operator

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

greater = a if a > b else b

print("Greater number is", greater)

13. Common Mistakes

Mistake 1: Using = instead of ==

# Wrong
if x = 5:
    print("Yes")

# Correct
if x == 5:
    print("Yes")

Mistake 2: Forgetting the colon

# Wrong
if age >= 18
    print("Adult")

# Correct
if age >= 18:
    print("Adult")

Mistake 3: Wrong indentation

# Wrong
if age >= 18:
print("Adult")

# Correct
if age >= 18:
    print("Adult")

Mistake 4: Writing true instead of True

# Wrong
is_valid = true

# Correct
is_valid = True

14. Practice Questions

  1. Take age as input and print whether the person is a child, teenager, adult, or senior citizen.
  2. Take marks as input and print pass or fail.
  3. Take a number and check whether it is even or odd.
  4. Take two numbers and print the greater number.
  5. Take three numbers and print the greatest number.
  6. Take username and password and check login.
  7. Take temperature and print whether the person has fever or not.
  8. Use a ternary operator to check whether a number is positive or negative.
  9. Use and to check if a student passed in both maths and science.
  10. Use or to check if a person can pay by cash or card.

15. Embedded Python Editor

Practice the examples directly in the Python editor.

16. Final Summary

Topic Meaning
Boolean A value that is either True or False.
Relational Operators Operators that compare two values.
Logical Operators Operators that combine or reverse conditions.
if Runs code when a condition is true.
else Runs code when the condition is false.
elif Checks another condition when the earlier condition is false.
Ternary Operator A short one-line form of if else.
Final idea: Conditions are the decision-making system of Python. They allow your program to think, choose, and react.