Making Python Think with You
Programming isnβt just about printing and storing dataβitβs about making decisions, repeating tasks, and reacting based on user input or conditions. In this chapter, weβll dive into how Python handles logic and decisions with if
, elif
, else
, and more.
1. π Conditional Statements: if
, elif
, else
#
Python uses indentation to define blocks of code. Conditional statements allow your code to execute only when certain conditions are true.
β Syntax: #
if condition:
# Do this
elif another_condition:
# Do something else
else:
# Do this if nothing above is True
π Example: #
age = int(input("Enter your age: "))
if age >= 18:
print("You're an adult.")
elif age > 12:
print("You're a teenager.")
else:
print("You're a child.")
2. π§± Nested Conditions #
You can place one condition inside another. This helps in complex decision making.
π Example: #
age = int(input("Enter your age: "))
has_id = input("Do you have an ID? (yes/no): ")
if age >= 18:
if has_id == "yes":
print("Access granted.")
else:
print("ID required for verification.")
else:
print("Access denied. Underage.")
π‘ Tip: Avoid too many nested blocks by combining conditions using logical operators.
3. β Logical Operators in Decision Making #
We often need to evaluate multiple conditions together. Python gives us:
Operator | Description | Example |
---|---|---|
and | All must be True | x > 5 and x < 10 |
or | At least one True | x > 5 or x < 3 |
not | Reverses condition | not x > 5 |
π Example: #
username = "admin"
password = "1234"
input_user = input("Username: ")
input_pass = input("Password: ")
if input_user == username and input_pass == password:
print("Login successful!")
else:
print("Invalid credentials.")
π¦ Real-World Use Case: Simple Banking Login System #
Letβs build a basic bank login system with user authentication and conditional access to features like checking balance or depositing funds.
β Step-by-Step Code: #
# Predefined user data
bank_user = "hannan"
bank_pass = "pass123"
balance = 5000
print("====== Welcome to Python Bank ======")
# Login Section
username = input("Enter your username: ")
password = input("Enter your password: ")
if username == bank_user and password == bank_pass:
print("\nβ
Login Successful!\n")
print("1. Check Balance")
print("2. Deposit Amount")
print("3. Withdraw Amount")
choice = input("Choose an option (1/2/3): ")
if choice == "1":
print(f"\nπ° Your current balance is: Rs. {balance}")
elif choice == "2":
deposit = float(input("Enter amount to deposit: "))
balance += deposit
print(f"β
Deposit successful! New balance: Rs. {balance}")
elif choice == "3":
withdraw = float(input("Enter amount to withdraw: "))
if withdraw <= balance:
balance -= withdraw
print(f"β
Withdrawal successful! New balance: Rs. {balance}")
else:
print("β Insufficient balance!")
else:
print("β Invalid choice.")
else:
print("β Login failed! Please check your credentials.")
π What This Covers: #
- Authentication using
if
- Nested conditionals
- Logical operators for verifying credentials
- Simple banking choices (menu logic)
- User input & type conversion
π§ Challenge Ideas: #
- β Add a retry mechanism for login (3 attempts).
- β Allow multiple users using a dictionary.
- β Store and display transaction history.
- β
Encrypt the password using hashing (with
hashlib
).
π Wrap Up #
This section focused on:
if
,elif
,else
statements- Nesting logic with multiple layers
- Logical operators (
and
,or
,not
) - Real-life application: Bank Login System
Mastering decision-making logic is key to writing smart and flexible code.
Let me know when you’re ready for the next section:
π βLoops in Python (for, while, break, continue) + Real-World Use Case: Task Tracker or Countdown Timerβ π
Want this turned into a PDF or website post format? Happy to help!