đ Python Basics: Welcome to Pythonâs Playground!
Imagine youâre the boss of a team of robot friends, and you want to tell them what to do. But robots donât understand English, Igbo, Yoruba or Hausaâthey understand a special kind of language. That language is Python!
Let me take you through 4 magical tools every junior Python coder needs: Variables, Expressions and Statements, Comments, and Indentation.
đŚ 1. Variables â Labeled Boxes for Your Stuff
Letâs pretend you have a big shelf of boxes at home. You can write labels on the boxes like âSnacksâ, âToysâ, or âSchool Stuffâ. In Python, we call those variables.
A variable is just a name for a box where we keep something like a word or a number.
đš Letâs try this:
name = "Roger"
age = 8
Now the computer has a box labeled name
and itâs holding "Roger"
inside. It also has another box labeled age
with 8
inside. Cool, right?
â Important Rule: You canât start your box name with a number.
123name
wonât work. Butmy_name
orname123
will.
đ§Ž 2. Expressions and Statements â Answers vs Actions
Letâs say you have a small calculator. You press 2 + 3
, and it shows you 5
. Thatâs an expression â because it gives you an answer.
In Python:
2 + 3 # This is an expression
But when you tell the computer what to do, like âShow me whatâs in the name boxâ, you write:
print(name) # This is a statement (an action)
Think of it like this:
- Expressions = Answers
- Statements = Instructions
Hereâs another one:
favorite_color = "blue" # Statement (telling the computer to remember this)
đ 3. Comments â Secret Notes for You, Not the Robot
Letâs say youâre writing down your plan to build a LEGO house. You might scribble on the side:
âThis wall needs to be tall.â
Thatâs a comment. In Python, you write it with a #
mark. Like this:
# This program says hello
print("Hello, friend!")
The robot (computer) will ignore the comment. Itâs like a secret note just for you and your fellow humans.
đ§ 4. Indentation â Grouping Code Neatly
Now imagine building a story where everything under a chapter is slightly pushed in. That way, you know it belongs to that chapter. Python does the same thing using indentation.
Weâll see this more when we do âif this happens, do thatâ kinds of coding, but hereâs a simple peek:
if age > 7:
print("You're old enough to start coding!")
That space before print
? Thatâs indentation. Python uses it to know that print("...")
is part of the âifâ rule above it.
If you donât indent when youâre supposed to, Python throws a mini-tantrum:
IndentationError: expected an indented block
So keep your code neat and lined up like a marching band. đĽđş
đ Summary: You Now Know...
Concept | What It Means | Example |
---|---|---|
Variable | A box with a name that holds data | name = "Roger" |
Expression | Something that gives an answer | 2 + 2 â 4 |
Statement | An instruction for the computer | print(name) |
Comment | A note the computer ignores | # This is a comment |
Indentation | Tells Python which instructions go where | if age > 7: (then indent) |
â Practice Time! Can You Try These?
- Write a Python statement that stores the number
10
in a variable calledscore
. - Create a variable called
food
and store"rice"
in it. - Write a
print
statement to show whatâs inside thefood
box. - Add a comment above your print statement saying âThis shows my favorite foodâ.
- Which of these will cause an error?
A.
my_name = "Ada"
B.123score = 100
C.color_of_car = "red"