Python Basics for Young Explorers
Welcome, junior coders! 🎉 In this adventure we’ll discover Python, a friendly computer language that lets you give instructions to a machine. You’ll learn how to store information, make choices, and repeat actions—just like a magician’s tricks, but with code!
1. What Is Python?
Python is a Programming Language (a set of rules for telling computers what to do).
- Syntax – the special spelling and punctuation that Python expects. Think of it as the grammar of a new language.
- Interpreter – the tool that reads your Python code line‑by‑line and makes the computer follow the instructions.
Did You Know? The name “Python” comes from a comedy TV show called Monty Python, not from the snake!
Example
print("Hello, world!")
When you run this, the computer shows the words Hello, world! on the screen. The print command tells the computer to Output (display) something.
2. Variables – Storing Treasure
A Variable is like a labeled box where you keep a piece of information. You can change what’s inside whenever you like.
| Variable | Meaning | Example |
|---|---|---|
age | a person’s age (a number) | age = 9 |
name | a name (text) | name = "Lila" |
score | points in a game | score = 0 |
Cause and Effect
- Cause: You assign a value to a variable (
score = 5). - Effect: Later, when you use
score, the computer remembers the number 5.
Mini Experiment
- Open the free online editor Repl.it (or any Python playground).
- Type:
favorite_color = "blue" print(favorite_color) - Change
"blue"to your own favorite color and run it again. See how the output changes!
3. Making Decisions – The if Spell
Sometimes a program must choose between two paths, just like you decide whether to wear a coat if it’s cold.
temperature = 12
if temperature < 15:
print("Wear a jacket!")
else:
print("No jacket needed.")
- The word
Ifstarts a condition (a test). - The Colon (
:) tells Python that the next indented lines belong to that condition. Elsecatches everything that didn’t satisfy theif.
Cause and Effect
- Cause:
temperatureis 12, which is less than 15. - Effect: The program prints “Wear a jacket!”.
4. Loops – Repeating Actions
A Loop makes the computer do the same thing many times without writing the code again. This is called Iteration.
for Loop (counting)
for i in range(1, 6):
print("Step", i)
Output:
Step 1
Step 2
Step 3
Step 4
Step 5
while Loop (until a Condition Is False)
count = 3
while count > 0:
print("Blast off in", count)
count = count - 1 # subtract 1 each time
Cause and Effect
- Cause: The
whilecondition (count > 0) is true. - Effect: The block runs, prints a line, then reduces
count. Whencountbecomes 0, the condition is false and the loop stops