Hackforge Academy

Category: python

Introduction to if Statement

Published on 24 Jul 2026

Explanation

The if statement is the simplest decision-making statement in Python. It executes a block of code only when the specified

Code:

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

if age >= 18:
    print('You are eligible to vote.')

print('Program completed.')

Explanation

The if...else statement allows a program to execute one block of code when a condition is True and another block

Code:

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

if number % 2 == 0:
    print('The number is Even.')
else:
    print('The number is Odd.')

Explanation

The if...elif...else statement is used when multiple conditions need to be checked. Python evaluates the conditions from top to bottom

Code:

marks = int(input('Enter your marks: '))

if marks >= 90:
    print('Grade A')
elif marks >= 75:
    print('Grade B')
elif marks >= 50:
    print('Grade C')
else:
    print('Fail')

Explanation

A nested if statement is an if statement placed inside another if statement. Nested conditions are useful when a decision

Code:

age = int(input('Enter your age: '))
license = input('Do you have a driving license? (yes/no): ')

if age >= 18:
    if license.lower() == 'yes':
        print('You are eligible to drive.')
    else:
        print('You need a valid driving license.')
else:
    print('You are underage and cannot drive.')

Explanation

Conditional statements are widely used in real-world applications to make decisions based on user input or business rules. Examples include

Code:

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

if username == 'admin' and password == '12345':
    print('Login Successful')
else:
    print('Invalid Username or Password')

amount = float(input('Enter purchase amount: '))

if amount >= 5000:
    discount = amount * 0.10
    print('Discount:', discount)
    print('Final Amount:', amount - discount)
else:
    print('No discount available.')

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community