Today we slow down and deeply understand everything that felt confusing. By the end of this lesson, the foundations will feel rock-solid.
The print() function has one job: show something in the terminal. But there are many different ways to use it. Let's learn every single one.
Create print_mastery.py and type every line. Run it and study the output carefully.
print("Age:", 20) → Age: 20print("Age: " + str(20)) → Age: 20str() to convert numbers). Gives you full control over formatting.
What does print("Result:", 3 + 4) output?
3 + 4 = 7. Then print shows both items separated by a space: Result: 7.We've used int, float, and str. There's a fourth essential type: bool (boolean). It can only be one of two values: True or False. This is the type Python uses every time it makes a decision in an if statement.
Create booleans.py:
Every if statement works by evaluating its condition to True or False. When you write if age >= 18:, Python computes age >= 18 which becomes either True or False. If True, the indented block runs. If False, it's skipped.
= Assignment — stores a value: x = 10 means "put 10 into x"== Comparison — checks if equal: x == 10 means "is x equal to 10?" and returns True or Falseif x = 5: (one equals) instead of if x == 5: (two equals), you'll get a SyntaxError. Single = is never allowed inside an if condition.
Create operators_all.py:
What is 17 % 5?
% gives the remainder. 17 ÷ 5 = 3 remainder 2. So 17 % 5 = 2.Let's be very clear about the syntax for strings, lists, and sets. Every bracket and quote matters.
'hello' and double quotes "hello" are exactly the same thing. Both create a string. Use whichever you like, but be consistent."it's sunny"'She said "hello"'[ ] and items are separated by commas:my_list = [item1, item2, item3]numbers = [10, 20, 30] — a list of integersnames = ["Alice", "Bob"] — a list of stringsempty_list = [] — a list with nothing in itmixed = [1, "hello", 3.14, True] — lists can mix typeslist("hello") → ['h', 'e', 'l', 'l', 'o']
my_set = {item1, item2, item3}colors = {"red", "green", "blue"}nums = {1, 2, 2, 3, 3, 3} → becomes {1, 2, 3} (duplicates removed!)empty_set = set() — NOT {} (that creates an empty dictionary, a different type)What does len({1, 2, 2, 3, 3, 3}) return?
{1, 2, 2, 3, 3, 3} becomes {1, 2, 3} — that's 3 unique items.Let's understand exactly what happens inside the computer when you create a variable, and the rules for naming them.
x = 10x = 10:10 in that spotx on that spot, so you can find it laterprint(x), Python looks up the label "x", goes to that RAM location, reads the value (10), and shows it on screen.
Create step_by_step.py. Before running, trace through it yourself line by line on paper:
name age student_name score1 _private myVar1name — cannot start with a numbermy name — cannot have spaces (use my_name instead)my-name — hyphens not allowed (use underscores)class for if print — these are Python keywords/built-ins, don't use them as variable namesName, name, and NAME are three completely different variables.
If you write list = [1, 2, 3], you've just replaced the built-in list() function with your variable. Now list() won't work anymore in your program. Same for str = "hello", print = 5, type = "admin", etc. Always use descriptive names like my_list, user_type, etc.
Which of these is a VALID variable name?
2nd_place starts with a number. my name has a space. for is a Python keyword. Only student_score follows all the rules.Let's break down the exact anatomy of a for loop and understand every single word in the syntax.
for i in range(1, 6):for i in range(1, 6):, here's exactly what happens:i gets the value 1 → the indented code runs with i=1i gets the value 2 → the indented code runs with i=2i gets the value 3 → the indented code runs with i=3i gets the value 4 → the indented code runs with i=4i gets the value 5 → the indented code runs with i=5i is automatically updated every time the loop goes around. You don't need to change it yourself — the for loop does that for you.
i Change Each Iterationif Condition?if condition can be anything that Python can evaluate to True or False:if True: — always runsif False: — never runsif x > 5: — runs if the comparison is Trueif name == "Pooja": — runs if name equals "Pooja"if x: — runs if x is "truthy" (non-zero number, non-empty string)if "hello": — runs! (non-empty string = True)if "": — does NOT run (empty string = False)if 0: — does NOT run (zero = False)if 42: — runs! (any non-zero number = True)if my_list: — runs if the list is not emptyFalse, 0, 0.0, "", [], NoneIn for num in [10, 20, 30]:, which is the keyword and which is the variable?
for and in are Python keywords (built into the language — you can't change them). num is a variable name that YOU chose. It gets a new value from the list on each iteration.Here is every common error you'll encounter, what it means, and exactly how to fix it. Create a file for each debug exercise, run it, and fix it.
Python's grammar rule was broken — it can't even begin to understand the code.
msg = "Hello World": at end of the if line. Fix: if 5 > 3:The code inside a block (if, for, etc.) isn't indented, or the indentation is inconsistent.
print(i)You used a variable name that Python doesn't recognize — it was never created, or it's misspelled.
User_name (capital U) doesn't exist — only user_name (lowercase u) does. Python is case-sensitive. Fix: print("Hello,", user_name)You tried to do an operation with incompatible types (like adding a string and a number).
+ string and int. Fix: print("I am " + str(age) + " years old") or use comma: print("I am", age, "years old")The type is right but the value is wrong — like trying to convert non-numeric text to a number.
int("hello") fails because "hello" isn't a number. int() only works on numeric strings like int("42"). Fix depends on intent — if you want 0, use a try/except or check the value first.You tried to access a list position that doesn't exist.
items[2] for last item, or items[-1].You tried to access a dictionary key that doesn't exist. (We haven't covered dictionaries yet, but you should know this error.)
"email" doesn't exist in the dictionary. Only "name" and "age" exist. Fix: use an existing key or add the key first: person["email"] = "pooja@mail.com"You tried to use a method that doesn't exist on that type.
.append() is a list method. x is an integer — integers don't have .append(). Fix: if you want a list, use x = [42] then x.append(5).The code runs fine but gives the WRONG answer. The hardest type to find.
10 + 20 + (30/3) = 40.0, not 20.0. Fix: avg = (10 + 20 + 30) / 3input() ALWAYS returns a string. If you do math on it without converting, you get wrong results or errors.
input() returns "5" (string). "5" * 2 = "55" (string repetition). Fix: num = int(input("Number: "))A while loop whose condition never becomes False. Press Ctrl+C to stop it.
x never changes, so x < 10 is always True. Fix: add x = x + 1 inside the loop.In Python 3, print is a function and MUST use parentheses.
print is a function — parentheses are required. Fix: print("Hello")Using a built-in name as a variable destroys the built-in functionality.
TypeError: 'list' object is not callable. Line 1 overwrote the built-in list() function with a variable. Now list(range(5)) tries to "call" your variable as a function. Fix: rename the variable: my_list = [1, 2, 3]Patterns teach you to see the math hidden inside repetition. This is how programmers think: find the pattern, express it as a formula, and let a loop do the work.
Let's print this pattern. First, do it with individual print statements:
This works, but imagine printing 100 rows. There must be a better way. Let's find the pattern.
Look at each row and count the stars:
"*" * 1"*" * 2"*" * 3"*" * 4"*" * 5i has exactly i stars. The formula is simply: "*" * ii goes from 1 to 5. That's a loop!
Just 2 lines! And you can change 6 to 101 to print 100 rows. The loop + formula approach is infinitely scalable.
Now let's do a harder pattern with spaces:
i (1 to 5):5 - i Stars = i" " * 4 + "*" * 1" " * 3 + "*" * 2" " * 2 + "*" * 3" " * 1 + "*" * 4" " * 0 + "*" * 5
Hints: For 5 rows, total width is 9 (2*5-1). For row i (1 to 5): Stars = 2*i - 1. Spaces = 5 - i. Use the same approach as the solved example above. Try for 3 minutes before revealing the answer!
n = 5for i in range(1, n + 1): spaces = " " * (n - i) stars = "*" * (2 * i - 1) print(spaces + stars)The process is always the same: see the pattern → find the formula → write the loop. This skill transfers to everything in programming — not just star patterns.
Every concept that felt confusing should now feel clear. You understand not just what to type, but why it works. This deep understanding is what separates someone who can code from someone who just copies code. You're becoming a real programmer.