The cursor blinked again on the dark screen.
_
Pran rested his hands on the keyboard.
The robot stood quietly nearby.
[^_^]
/| |\
/ \
A new system message appeared.
PYTHON MODULE PROGRESS: 30%
NEXT MODULE: DECISION SYSTEM
Pran nodded.
"Decision system… that sounds important."
The computer displayed another message.
PROGRAMS MUST SOMETIMES MAKE CHOICES
That made sense.
Real programs often need to decide things like:
Is the password correct?
Is the player old enough?
Did the user guess the right number?
Computers cannot think for themselves.
But they can follow logical rules.
In Python, those rules are written using if statements.
The system displayed a simple example.
age = 20
if age >= 18:
print("You are an adult")
Pran studied the code.
The important part was this line:
if age >= 18
This means:
If the value of age is greater than or equal to 18, run the code below.
The computer then prints:
You are an adult
Pran decided to test it.
He ran the program.
The output appeared.
You are an adult
The robot nodded.
Then the computer displayed another version.
age = int(input("Enter your age: "))
if age >= 18:
print("Access granted")
Pran ran the program.
The screen asked:
Enter your age:
He typed:
15
Nothing happened.
Pran frowned.
"Wait… it didn't say anything."
That's because the condition was false.
The rule only prints something when the condition is true.
So if age is less than 18, the program does nothing.
But sometimes we want the program to do something else when the condition is false.
For that, Python uses another keyword.
else
The system displayed the improved program.
age = int(input("Enter your age: "))
if age >= 18:
print("Access granted")
else:
print("Access denied")
Pran ran the program again.
Enter your age:
He typed:
15
The computer responded.
Access denied
Pran nodded.
"That makes more sense."
The robot clapped its tiny ASCII hands.
[^o^]
/| |\
/ \
The computer displayed another system message.
DECISION SYSTEM VERIFIED
Then it showed a few important comparison operators.
These operators help programs compare values.
== equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
Pran leaned forward.
"So the computer checks conditions using these symbols."
Exactly.
For example:
score = 90
if score >= 50:
print("You passed")
If the score is 50 or higher, the student passes.
If not, the student fails.
Programs use these rules everywhere.
Games.
Websites.
Apps.
Security systems.
Even robots.
The computer suddenly displayed another message.
SYSTEM TEST REQUIRED
Then a challenge appeared.
PASSWORD VERIFICATION REQUIRED
The system showed the code.
password = input("Enter password: ")
if password == "python":
print("Access granted")
else:
print("Access denied")
Pran typed the program and ran it.
The screen asked:
Enter password:
He typed:
hello
The program responded.
Access denied
He ran it again.
Enter password:
This time he typed:
python
The computer responded.
Access granted
The screen flashed brightly.
Then the robot jumped slightly.
[^O^]
/| |\
/ \
Another system message appeared.
SECURITY MODULE ACTIVATED
Then another.
PYTHON MODULE PROGRESS: 40%
Pran leaned back in his chair.
"Forty percent already."
The computer displayed another message.
NEXT MODULE: REPETITION SYSTEM
Pran smiled.
"That sounds like loops."
Programs sometimes need to repeat instructions many times.
For example:
asking the user again if the answer is wrong
counting numbers
repeating actions in games
To do that, Python uses loops.
The cursor blinked again.
_
Waiting.
Ready for the next lesson.
Pran placed his fingers on the keyboard again.
"Alright Python," he said.
"Let's start repeating things."
Next Chapter
Chapter 15 — The Repeating Machine
You will learn:
while loops
repeating programs
simple guessing systems
