Today you'll teach your programs to make decisions, store collections of data, and repeat actions automatically. These three ideas power almost every program ever written.
Open VS Code → open H:\Pooja → create a new folder called day4. Open the integrated terminal (Ctrl + `) and navigate: cd H:\Pooja\day4. All files today go inside this folder.
Let's make sure yesterday's concepts are solid before building on them. Answer these without looking back.
What is the output of this code?x = 10x = x + 5print(x)
x starts as 10. Then x = x + 5 takes the current value (10), adds 5, and stores 15 back into x.What is type("42")?
"42" is text, 42 (no quotes) is an int.You run code and get: NameError: name 'scre' is not defined. What happened?
scre. You probably meant score. Always check spelling.What does "hello".upper() return?
.upper() converts every letter to uppercase: "HELLO".Think about any app you use. When you type a wrong password, it shows "Incorrect password." When you type the right one, it logs you in. Different things happen depending on a condition. That's what conditionals do.
You make conditional decisions every day without thinking about it:
IF it's raining → take an umbrella
ELSE IF it's sunny → wear sunglasses
ELSE → just go outside normally
You check a condition (is it raining?), and depending on the answer (yes or no), you do different things. That's all if/elif/else does in Python — it lets the program choose what to do based on a condition.
Without conditionals, a program would do the exact same thing every time — no matter what. Think of an ATM: it needs to check if you have enough balance before letting you withdraw money. A login page needs to check if your password is correct. A game needs to check if your health reached zero. Conditionals make programs smart — they can react differently to different situations.
Create if_basic.py:
age to 15, save and run again. Notice that "You are an adult" and "You can vote" don't appear, but "Program finished" still does.print lines that are indented (pushed to the right) are inside the if block. They only run when the condition is True. The last print is NOT indented, so it always runs.Notice how the two lines inside the if are pushed to the right by 4 spaces? That's called indentation. In Python, indentation is NOT optional — it's how Python knows which lines are "inside" the if block and which are outside.
Other programming languages use curly braces { } to group code. Python uses spaces. This forces your code to be visually clean and readable — you can always see the structure just by looking at the spacing.
Rule: Every line inside an if (or elif, else, loop, etc.) MUST be indented by the same amount (4 spaces is the standard). When you stop indenting, you're "outside" the block.
Create if_else.py:
python123 — you get the "Access granted" path.else catches everything that doesn't match the if condition. It's the "otherwise" branch.Create grade.py:
elif means "else if" — check this condition only if all the ones above were False.Create indent_error.py with this broken code:
IndentationError: expected an indented block after 'if' statement on line 3if on line 3, but the next line isn't indented, so I don't know what's supposed to be inside the if block."print on line 4 so it becomes: print("x is big"). In VS Code, you can also press Tab to indent.== equal to != not equal to > greater than < less than >= greater or equal <= less or equal= (one equals) is assignment (store a value). == (two equals) is comparison (check if equal).
What does this print if temp = 25?if temp > 30: print("Hot")elif temp > 20: print("Warm")else: print("Cold")
if is skipped. 25 IS > 20, so the elif matches. Python prints "Warm" and stops checking — it doesn't continue to else.So far, every variable has held one value. But what if you need to store 10 student names? Or 50 test scores? You could make 50 separate variables, but that would be a nightmare. That's why lists exist.
Think of a shopping list on paper. Instead of writing each item on a separate piece of paper (separate variables), you write them all on one list. You can add items, remove items, check how many items you have, and go through each item one by one. A Python list works exactly the same way.
Think of apps you use: a to-do app stores a list of tasks. Your music player has a list of songs. Instagram shows a list of posts. Google search returns a list of results. Whenever an app deals with multiple items of the same kind, there's a list behind the scenes.
Create list_why.py — see how painful life would be without lists:
Create list_basics.py:
The first item is at position [0], NOT [1]. This trips up beginners constantly. fruits[0] = "apple", fruits[1] = "banana", etc. This is called zero-based indexing and it's used in almost every programming language.
Create list_modify.py:
colors = ["red", "green", "blue"]
What is colors[1]?
[0]="red", [1]="green", [2]="blue".What if you want to print "Hello" 100 times? Or greet every student in a list? You could write 100 print statements, but that's insane. Loops let you repeat code automatically.
Imagine a teacher calling attendance: "Alice?" ... "Bob?" ... "Charlie?" ... They're doing the same action (calling a name) for each student in the list. That's a loop — repeating an action for every item in a collection, or repeating an action a certain number of times.
Create loop_why.py:
Both produce the same output. But if you had 1000 names, the loop version is still just 3 lines. The non-loop version would be 1000 lines.
Create loop_range.py:
range(1, 6) generates: 1, 2, 3, 4, 5 (starts at 1, stops before 6). range(5) generates: 0, 1, 2, 3, 4.
Create loop_while.py:
while loops keep repeating as long as the condition is True. Once command equals "quit", the condition becomes False and the loop stops.
Create combo.py:
Notice the indentation: the if/else is indented inside the for loop, and the print statements are indented inside the if/else. Two levels of indentation = 8 spaces.
How many times does this print?for i in range(3): print("Hi")
range(3) generates 0, 1, 2 — that's 3 values, so the loop runs 3 times.10 exercises: 5 programming + 5 debugging. Create a new file for each one.
Write a program that asks the user for a number and prints whether it's even or odd. (Hint: a number is even if number % 2 == 0. The % operator gives the remainder.)
number = int(input("Enter a number: "))if number % 2 == 0: print("Even")else: print("Odd")Write a program: ask for user's age. If age < 5 → "Free", age 5-17 → "Child ticket: $5", age 18-64 → "Adult ticket: $10", age 65+ → "Senior ticket: $7".
age = int(input("Enter age: "))if age < 5: print("Free")elif age <= 17: print("Child ticket: $5")elif age <= 64: print("Adult ticket: $10")else: print("Senior ticket: $7")Complete this so it prints "Positive", "Negative", or "Zero":
elif — second blank: elsex = 15if x > 20: print("A")elif x > 10: print("B")elif x > 5: print("C")
elif stops at the first match.Write a program that asks for username and password. If username is "admin" AND password is "1234" → "Login successful". Otherwise → "Invalid credentials". (Hint: use and to combine conditions: if a == "x" and b == "y":)
user = input("Username: ")pw = input("Password: ")if user == "admin" and pw == "1234": print("Login successful")else: print("Invalid credentials")print needs to be indented (4 spaces) to be inside the if block. Fix: print("It's hot!"): at the end of line 2. Every if, elif, else, for, while line MUST end with :. Fix: if age >= 18:= (assignment) instead of == (comparison). Fix: if color == "blue":Expected: score 75 should print "B", but it prints "C". Find the logic error.
>= 60 comes before >= 70. Since 75 >= 60 is True, Python prints "C" and stops, never reaching the 70 check. Fix: put higher thresholds first: swap the >= 60 and >= 70 lines.10 exercises: 5 programming + 5 debugging.
Create a list of 4 favorite foods. Print the entire list, print the first item, print the last item, and print how many items are in the list.
foods = ["pizza", "biryani", "pasta", "momos"]print(foods)print("First:", foods[0])print("Last:", foods[-1])print("Total:", len(foods))Start with items = ["milk", "bread"]. Add "eggs" to the list, then remove "bread", then change the first item to "almond milk". Print the list after each change.
items = ["milk", "bread"]items.append("eggs")print(items)items.remove("bread")print(items)items[0] = "almond milk"print(items)Complete this so it adds "grape" to the list and prints the total count:
append — Second blank: lennums = [10, 20, 30]nums.append(40)nums[0] = 99print(nums)
append(40) adds 40 at the end: [10,20,30,40]. Then [0] = 99 changes the first item: [99,20,30,40].Create a list numbers = [10, 20, 30, 40, 50]. Use a for loop to calculate the total sum. Print "Sum: 150".
numbers = [10, 20, 30, 40, 50]total = 0for n in numbers: total = total + nprint("Sum:", total)[3] doesn't exist. Fix: colors[2] for the last item, or colors[-1]..append(), not .add(). Fix: items.append("eraser").len(nums) returns an int (3), and you can't + a string with an int. Fix: print("Count: " + str(len(nums))) or simpler: print("Count:", len(nums)).[], Python creates a tuple (a different type), not a list. It still works with indexing, but you can't use .append() etc. Fix: animals = ["cat", "dog", "bird"].if "cherry" in fruits: then fruits.remove("cherry"), or remove an item that actually exists.10 exercises: 5 programming + 5 debugging.
Ask the user for a number. Print its multiplication table from 1 to 10. Example for 5: 5 x 1 = 5, 5 x 2 = 10, etc.
num = int(input("Enter a number: "))for i in range(1, 11): print(num, "x", i, "=", num * i)Given word = "programming", use a loop to count how many vowels (a, e, i, o, u) are in the word. Print the count.
word = "programming"count = 0for letter in word: if letter in "aeiou": count = count + 1print("Vowels:", count)Complete this so it prints numbers 1 to 5:
range(1, 6) — starts at 1, stops before 6, giving: 1, 2, 3, 4, 5.total = 0for n in [2, 4, 6]: total = total + nprint(total)
Given numbers = [34, 78, 12, 95, 56]. Use a loop to find and print the largest number. (Don't use the built-in max() function — do it manually with a loop and an if statement.)
numbers = [34, 78, 12, 95, 56]biggest = numbers[0]for n in numbers: if n > biggest: biggest = nprint("Largest:", biggest)print must be indented inside the for block. Fix: print(i)Warning: this will run forever. Press Ctrl + C to stop it!
count never changes — it's always 0, so count < 5 is always True. Fix: add count = count + 1 inside the loop (indented).Expected: print 1 to 10. Actual: prints 1 to 9.
range(1, 10) goes 1 through 9 (stops before 10). Fix: range(1, 11) to include 10.for name in names: (add : at the end).Expected: print the sum of all numbers. Actual: prints only the last number.
total = n replaces total with n each time instead of adding to it. Also, total needs to be initialized before the loop. Fix: add total = 0 before the loop, and change line 3 to total = total + n.Conditionals, lists, and loops are the backbone of nearly every program ever written. A game? Loops + conditionals. A social media feed? Lists + loops. A chatbot? Conditionals + loops. You now have the tools to build real things. Next time we'll learn about functions — reusable blocks of code — and start building more complex projects.