Total Pageviews

Wednesday, September 9, 2026

Conditional Statement

Conditional Statements in Python

Complete Tutorial with Syntax, Examples & Output

1. Introduction

A conditional statement is used to make decisions in a Python program. It allows the program to execute different statements depending on whether a given condition is True or False.

For example, a program can check whether a student has passed an examination, whether a person is eligible to vote, or whether a number is positive or negative.

Condition → True → Execute one block
Condition → False → Execute another block

2. Types of Conditional Statements

Python provides several ways to perform conditional execution:

Statement Purpose
if Executes code when a condition is True
if-else Chooses between two alternatives
if-elif-else Chooses among multiple alternatives
Nested if Places one conditional statement inside another
Conditional Expression Provides a compact one-line condition

3. if Statement

The if statement executes a block of code only when its condition evaluates to True.

Syntax

if condition:
    statement

Example

age = 20

if age >= 18:
    print("Eligible to vote")

Output

Eligible to vote

4. Boolean Conditions

A condition generally produces one of two Boolean values:

  • True
  • False

Example

age = 20

print(age >= 18)

Output

True

5. if-else Statement

The if-else statement is used when there are two possible execution paths.

Syntax

if condition:
    statement1
else:
    statement2

Example

number = 7

if number % 2 == 0:
    print("Even")
else:
    print("Odd")

Output

Odd

6. Practical Example – Pass or Fail

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

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

Sample Output

Enter marks: 65
Pass

7. if-elif-else Statement

The if-elif-else structure is used when multiple conditions need to be checked.

Syntax

if condition1:
    statement1
elif condition2:
    statement2
elif condition3:
    statement3
else:
    statement4

Example

marks = 82

if marks >= 90:
    print("Grade A+")
elif marks >= 80:
    print("Grade A")
elif marks >= 70:
    print("Grade B")
else:
    print("Grade C")

Output

Grade A

8. Practical Example – Student Grade

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

if marks >= 90:
    grade = "A+"
elif marks >= 80:
    grade = "A"
elif marks >= 70:
    grade = "B"
elif marks >= 60:
    grade = "C"
elif marks >= 40:
    grade = "D"
else:
    grade = "F"

print("Grade:", grade)

Sample Output

Enter marks: 76
Grade: B

9. Nested Conditional Statement

A nested conditional statement is a conditional statement placed inside another conditional statement.

Example

age = 22
has_id = True

if age >= 18:
    if has_id:
        print("Entry allowed")
    else:
        print("ID required")
else:
    print("Entry denied")

Output

Entry allowed

10. Comparison Operators in Conditions

Operator Meaning Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal a >= b
<= Less than or equal a <= b

11. Logical Operators in Conditions

Logical operators are used to combine multiple conditions.

Operator Meaning
and True only when both conditions are True
or True when at least one condition is True
not Reverses the Boolean result

Example using and

age = 21
marks = 65

if age >= 18 and marks >= 40:
    print("Eligible")
else:
    print("Not eligible")

Output

Eligible

12. Using the or Operator

day = "Sunday"

if day == "Saturday" or day == "Sunday":
    print("Holiday")
else:
    print("Working day")

Output

Holiday

13. Using the not Operator

logged_in = False

if not logged_in:
    print("Please login")

Output

Please login

14. Practical Example – 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")

Sample Output

Enter first number: 25
Enter second number: 40
Largest = 40

15. Practical Example – Largest 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("Largest =", a)
elif b >= a and b >= c:
    print("Largest =", b)
else:
    print("Largest =", c)

Sample Output

Enter first number: 25
Enter second number: 70
Enter third number: 45
Largest = 70

16. Practical Example – Positive, Negative or Zero

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

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

Sample Output

Enter a number: -15
Negative

17. Practical Example – Leap Year

A year is a leap year if it is divisible by 400, or if it is divisible by 4 but not divisible by 100.

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

if year % 400 == 0:
    print("Leap year")
elif year % 100 == 0:
    print("Not a leap year")
elif year % 4 == 0:
    print("Leap year")
else:
    print("Not a leap year")

Sample Output

Enter year: 2024
Leap year

18. Practical Example – Voting Eligibility

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

if age >= 18:
    print("Eligible to vote")
else:
    print("Not eligible to vote")

Sample Output

Enter your age: 19
Eligible to vote

19. Practical Example – Login Validation

username = input("Enter username: ")
password = input("Enter password: ")

if username == "admin" and password == "1234":
    print("Login successful")
else:
    print("Invalid username or password")

Sample Output

Enter username: admin
Enter password: 1234
Login successful

20. Practical Example – Calculator Using Conditions

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /): ")

if op == "+":
    print("Result =", a + b)
elif op == "-":
    print("Result =", a - b)
elif op == "*":
    print("Result =", a * b)
elif op == "/":
    if b != 0:
        print("Result =", a / b)
    else:
        print("Cannot divide by zero")
else:
    print("Invalid operator")

Sample Output

Enter first number: 20
Enter second number: 5
Enter operator (+, -, *, /): *
Result = 100.0

21. Conditional Expression

A conditional expression is a compact way to write an if-else decision in a single line.

Syntax

value_if_true if condition else value_if_false

Example

age = 20

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

print(status)

Output

Adult

22. Membership Operators in Conditions

The operators in and not in can be used to test whether a value exists in a sequence.

Example

subjects = ["C", "Python", "Java"]

subject = "Python"

if subject in subjects:
    print("Subject found")
else:
    print("Subject not found")

Output

Subject found

23. Identity Operators in Conditions

Python provides is and is not for identity testing. They check whether two references refer to the same object.

Example

a = None

if a is None:
    print("No value assigned")

Output

No value assigned

24. Truth Value in Conditions

Python allows many objects to be used directly as conditions. For example, an empty list is considered false, while a non-empty list is considered true.

Example

numbers = [10, 20, 30]

if numbers:
    print("List contains elements")
else:
    print("List is empty")

Output

List contains elements

25. Importance of Indentation

Python uses indentation to identify the statements belonging to a conditional block.

Correct Example

age = 20

if age >= 18:
    print("Adult")
    print("Eligible")

Both print() statements belong to the if block because they have the same indentation.


26. Flow of Conditional Statement

START

Evaluate Condition

Is Condition True?

↙          ↘

YES          NO

↓                ↓

Execute True Block    Execute False/Next Block

↘          ↙

Continue Program

END


27. Common Errors

  • Forgetting the colon:
    if age >= 18:
  • Incorrect indentation: Statements inside the condition must be indented.
  • Using = instead of ==:
    = performs assignment, while == checks equality.
  • Incorrect condition order: In an if-elif structure, conditions should be arranged carefully.
  • Using too many nested conditions: Complex nesting can make programs difficult to understand and maintain.

28. if vs if-else vs if-elif-else

Structure Use
if One condition
if-else Two alternatives
if-elif-else Multiple alternatives
Nested if Decision inside another decision
Conditional expression Compact one-line decision

29. Real-Life Applications

  • Student result and grading systems
  • Login and authentication systems
  • Banking applications
  • Online shopping and discount calculation
  • Voting eligibility checking
  • ATM transaction validation
  • Weather-based decisions
  • Traffic signal control
  • Game decision-making
  • Machine Learning classification logic

30. Summary

Conditional statements are fundamental to Python programming. They allow a program to make decisions and execute different blocks of code depending on conditions.

  • if executes code when a condition is True.
  • if-else provides two alternative paths.
  • if-elif-else handles multiple conditions.
  • Nested conditions allow decisions within decisions.
  • Comparison operators are commonly used to construct conditions.
  • Logical operators combine multiple conditions.
  • Conditional expressions provide a compact one-line alternative.
  • Proper indentation is essential in Python.

Created by Bijan Krishna Paul

Computer Science • Python Programming • Data Science

No comments:

Post a Comment