The Cafeteria Logic Problem
Chaitanya walks into the computer lab holding a crumpled piece of paper with a flowchart drawn on it. He looks confused.
Chaitanya: Ma’am, I’m trying to design a system for the canteen. It’s simple: If a student has money, give them a burger. If they don’t, tell them to go away. But the computer just runs every line of code from top to bottom. It gives the burger and tells them to go away!
Aditi Ma’am: That’s because your program has no brain, Chaitanya. It’s just following a list. You need Flow Control.
Chaitanya: Flow control?
Aditi Ma’am: Yes. You need to teach the computer to make decisions based on the current situation. Imagine a security guard at the school gate.
- Guard: “Do you have an ID card?”
- If Yes: “Enter.”
- If No: “Go home.”
Aditi Ma’am: To do this in Python, we first need to understand how to ask “Yes or No” questions.
Boolean Values (The Truth)
Aditi Ma’am: In the School System, every check must have a clear answer. It cannot be “Maybe.” It is either True or False.
Chaitanya: Just those two?
Aditi Ma’am: Yes. These are called Boolean values.
Chaitanya: I’ll type true in the shell.
Python
>>> true
Traceback (most recent call last):
NameError: name 'true' is not defined
Aditi Ma’am: Ah, catch the error. Python is case-sensitive. It must be True (capital T) and False (capital F). If you use lowercase, Python thinks it’s a variable name.
Comparison Operators (The Inspectors)
Aditi Ma’am: Now, to get a True or False answer, we need to compare things. We use Comparison Operators.
Chaitanya: Like equals and not equals?
Aditi Ma’am: Exactly. Here is the list:
==(Equal to)!=(Not equal to)<(Less than)>(Greater than)<=(Less than or equal to)>=(Greater than or equal to)
Chaitanya: Wait, why two equals signs ==?
Aditi Ma’am: This is the most common mistake beginners make.
=(Single Equal) is a command. “Make this variable equal to 42.”==(Double Equal) is a question. “Is this variable equal to 42?”
Chaitanya: Let me test the difference.
Python
>>> grade = 10 # Assignment (Command)
>>> grade == 10 # Comparison (Question)
True
>>> grade == 5
False
Aditi Ma’am: Also, remember that text is strict. 'Teacher' is not equal to 'teacher'. The capital ‘T’ makes it a completely different string.
Boolean Operators (AND, OR, NOT)
Chaitanya: Real life is complicated, Ma’am. To go on the school trip, a student needs good grades AND they need to pay the fee.
Aditi Ma’am: For that, we use Boolean Operators: and, or, not.
1. The and Operator Aditi Ma’am: This is the strict teacher. Both sides must be True.
True and True→ TrueTrue and False→ False
2. The or Operator Aditi Ma’am: This is the lenient teacher. If either side is True, you pass.
True or False→ TrueFalse or False→ False
3. The not Operator Aditi Ma’am: This just flips the value.
not True→ False
Chaitanya: So if I write (4 < 5) and (5 < 6), Python checks both?
Aditi Ma’am: Yes. True and True becomes True.
Mixing It All Together
Aditi Ma’am: You can combine these into complex logic gates. Look at this:
Python
>>> (4 < 5) and ((5 < 6) or (1 == 99))
Chaitanya: That looks scary.
Aditi Ma’am: Break it down like math.
(1 == 99)is False.(5 < 6)is True.- So the bracket becomes
(True or False), which is True. - Now we have
(4 < 5) and True. - Since
4 < 5is True, the final result is True.
Flow Control Statements: The if Statement
Aditi Ma’am: Now let’s write the code for your canteen system. We use the if statement.
Chaitanya: How does it know which code to run?
Aditi Ma’am: Indentation. This is crucial in Python. The code that belongs to the “if” must be pushed to the right (usually 4 spaces).
Python
name = 'Chaitanya'
if name == 'Chaitanya':
print('Hello, Chaitanya.')
Aditi Ma’am: If the condition is True, the indented block runs. If not, it skips it.
else and elif (Plan B and Plan C)
Chaitanya: What if I want to say “Access Denied” for everyone else?
Aditi Ma’am: Use else. It catches everything that the if missed.
Python
password = '123'
if password == 'swordfish':
print('Access Granted.')
else:
print('Wrong Password.')
Chaitanya: What if there are three categories? Junior, Senior, and Staff?
Aditi Ma’am: Use elif (Else If).
Python
role = 'Senior'
if role == 'Junior':
print('Go to Room 101.')
elif role == 'Senior':
print('Go to Room 202.')
elif role == 'Staff':
print('Go to Staff Room.')
Aditi Ma’am: Warning: Order matters! Python checks from top to bottom. As soon as it finds one True condition, it runs that block and ignores the rest.
Loops: Repeating Tasks (The while Loop)
Aditi Ma’am: Imagine you have to print “I will not talk in class” 100 times.
Chaitanya: I’d use copy-paste.
Aditi Ma’am: Or, you could use a Loop. A while loop keeps running as long as the condition is True.
Python
spam = 0
while spam < 5:
print('Hello, World.')
spam = spam + 1
Chaitanya: What happens if I forget spam = spam + 1?
Aditi Ma’am: Then spam stays 0 forever. 0 < 5 is always True. You create an Infinite Loop. The program will never stop until you force-quit it (Ctrl+C).
Breaking the Loop (break and continue)
Chaitanya: Can I leave the loop early? Like if the bell rings?
Aditi Ma’am: Yes.
break: “I quit. Get me out of this loop immediately.”continue: “Skip the rest of this turn and go back to the start.”
Chaitanya: Let me try a login system that keeps asking until you get it right.
Python
while True:
print('Who are you?')
name = input()
if name != 'Chaitanya':
continue # Go back to start, ask again
print('Hello, Chaitanya. What is the password?')
password = input()
if password == 'swordfish':
break # Success! Leave the loop.
print('Access Granted.')
The for Loop and range()
Aditi Ma’am: while loops are good when you don’t know how long you’ll wait. But if you know you need to run exactly 10 times, use a for loop.
Python
print('My name is')
for i in range(5):
print('Chaitanya Five Times (' + str(i) + ')')
Chaitanya: It prints 0, 1, 2, 3, 4. Why did it stop before 5?
Aditi Ma’am: range(5) creates a sequence starting at 0 and stopping just before 5.
Aditi Ma’am: You can also get fancy: range(0, 10, 2) counts by twos: 0, 2, 4, 6, 8.
Importing Modules (The Library)
Aditi Ma’am: Python has a massive library of pre-written code called Modules. To use them, you must import them first.
Chaitanya: Like checking out a book?
Aditi Ma’am: Yes. Let’s import random to print random numbers.
Python
import random
for i in range(5):
print(random.randint(1, 10))
Aditi Ma’am: You can also import sys to exit the program early using sys.exit(). This is useful if a critical file is missing and the program simply cannot continue.
Aditi’s Final Lesson
Aditi Ma’am: That’s it for Flow Control. You now have:
- Booleans: The ability to see True/False.
- Comparisons: The ability to check values.
- If/Else: The ability to make decisions.
- Loops: The ability to repeat tasks.
Chaitanya: I feel like I can actually build something now.
Aditi Ma’am: You can. Tomorrow, we will learn how to write Functions so we don’t have to keep rewriting the same code over and over.