Three conditional structures, built up step by step through a login/authentication simulator โ from a single check to three levels of nesting.
This topic covers three related conditional structures in Python, building up in complexity:
if-else (or if-elif-else) block inside another if-else block, so a second condition is only checked after a first condition has already been satisfied (or failed).All three are demonstrated together using a login/authentication simulator example, showing how the logic evolves from a basic check to a more capable, multi-step validation flow.
The running example throughout is a login/authentication simulator:
campusx@gmail.com1234True, and a different block if it's False. Only two possible outcomes.elif; Python runs the block for the first condition that evaluates to True, and falls back to else only if none of them match.if, elif, or else block can contain another complete if-else (or if-elif-else) block indented inside it.and operator, conditions (nested or not) let you build increasingly specific checks (e.g., "email is correct and password is correct" vs. "email is correct but password is wrong").# correct email- campusx@gmail.com
# password - 1234
email = input("Apna email bata")
password = input("Apna passowrd bhi bata")
if email == "campusx@gmail.com" and password == "1234":
print("Welcome")
else:
print("Incorrect credentials")
How it works:
email and password as input from the user.and operator so both conditions must be true to print "Welcome".else and prints "Incorrect credentials" โ there's no way to tell the user which part was wrong.Example run:
Apna email bata: campusx@gmail.com
Apna passowrd bhi bata: 1234
Welcome
# correct email- campusx@gmail.com
# password - 1234
email = input("Apna email bata")
password = input("Apna passowrd bhi bata")
if email == "campusx@gmail.com" and password == "1234":
print("Welcome")
elif email == "campusx@gmail.com" and password != "1234":
print("Password Incorrect")
password = input("Password fir se bol")
if password == "1234":
print("Finally correct")
else:
print("Still incorrect")
else:
print("Incorrect credentials")
How it works โ step by step:
This version is an if-elif-else structure at the top level, with a small nested if-else tucked inside the elif branch.
if (first condition): email correct and password correct โ "Welcome".elif (second condition): email correct but password wrong โ enters this branch.
"Password Incorrect".input()."1234" โ "Finally correct"."Still incorrect".else (fallback): if the email itself was wrong (regardless of password) โ "Incorrect credentials".Important points:
elif branch โ i.e., only when the email was already correct. This is the core idea of nesting: an inner decision that only matters once an outer condition is met."Incorrect credentials".A further extended version (seen in the full code) wraps the entire if-elif-else from Version 2 inside an outer if-else that first validates the email format:
# correct email- campusx@gmail.com
# password - 1234
email = input("Apna email bata")
if '@' in email:
password = input("Apna passowrd bhi bata")
if email == "campusx@gmail.com" and password == "1234":
print("Welcome")
elif email == "campusx@gmail.com" and password != "1234":
print("Password Incorrect")
password = input("Password fir se bol")
if password == "1234":
print("Finally correct")
else:
print("Still incorrect")
else:
print("Incorrect credentials")
else:
print("Email galat hai sahi likho")
How this differs from Version 2:
if '@' in email: checks whether the email even looks valid (contains @) before asking for a password at all.if, so it only runs when the email format passes.'@' is not in the email, the outer else triggers and it skips straight to print("Email galat hai sahi likho") ("Email is wrong, type it correctly") โ the password is never even asked for.This demonstrates three levels of nesting stacked together:
elif): retry the password check.| Email entered | Password entered | Output |
|---|---|---|
campusx@gmail.com | 1234 | Welcome |
campusx@gmail.com | 12535 (then re-entered 1234) | Password Incorrect โ Finally correct |
campusx@gmail.com | 315236 (then re-entered rjyyjr) | Password Incorrect โ Still incorrect |
Apna email bata: campusx@gmail.com
Apna passowrd bhi bata: 12535
Password Incorrect
Password fir se bol: 1234
Finally correct
Email matched โ entered elif branch (password didn't match 1234 the first time). Prompted again, user typed 1234 correctly this time โ nested if triggered โ "Finally correct".
Apna email bata: campusx@gmail.com
Apna passowrd bhi bata: 315236
Password Incorrect
Password fir se bol: rjyyjr
Still incorrect
Same path, but the second password attempt was also wrong โ nested else triggered โ "Still incorrect".
True; else only runs if none matched.and to combine multiple conditions in a single if/elif check (e.g., email and password both correct).In the login example, all three come together: the outer if-else validates the email format ('@' in email); nested inside it, an if-elif-else validates the email+password combination; and nested inside the elif (correct email, wrong password), a third-level if-else gives the user a second chance to type the password correctly.