|

Python Conditional Statements – Making Decisions in Programs

Introduction

Programs become powerful when they can make decisions.

Example:

  • If student passes → show “Congratulations”
  • If password is correct → login

This is done using conditional statements.


The if Statement

age = 18if age >= 18:
print("You can vote")

Indentation is important.


if-else Statement

age = 16if age >= 18:
print("You can vote")
else:
print("You cannot vote")

if-elif-else

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

Nested Conditions

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

Real Example: Login System

username = input("Enter username: ")
password = input("Enter password: ")if username == "admin" and password == "1234":
print("Login Successful")
else:
print("Invalid Credentials")

Checking Multiple Conditions

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

Common Mistakes

  • Missing colon :
  • Wrong indentation
  • Using = instead of ==

Conclusion

You now know:

  • if
  • if-else
  • elif
  • Nested conditions

Next, we will learn loops.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *