Python Basics: Syntax & Variables
Learn the ABCs of Python coding – indentation, comments, variables, and naming rules!
1. Python Syntax: The Rules of the Game
Syntax means the set of rules that define how to write Python code. Think of it like grammar for programming!
Indentation: Spaces Matter!
In Python, indentation (spaces or tabs) is used to group code blocks, like loops or conditions.
age = 12
if age >= 10:
print("You can join the coding club!") # Indented with 4 spaces
else:
print("Keep practicing!")
✅ Correct: Lines under if and else are indented.
❌ Wrong: No indentation → Python throws an error!
Pro Tip: Use 4 spaces (not tabs) for indentation. Most editors do this automatically!
2. Comments: Notes for Humans
Comments are notes you add to explain your code. Python ignores them!
Single-Line Comments
Use # to write a comment:
# This program calculates your age
current_year = 2024
birth_year = 2012
age = current_year - birth_year
print(age) # Output: 12
Multi-Line Comments
Use triple quotes """ for longer notes:
"""
This program greets a user.
Author: Coding Champ
Date: 2024
"""
name = "Alex"
print("Hello, " + name)
3. Variables: Your Data Containers
A variable is like a labeled lunchbox that stores data (numbers, text, etc.).
Creating Variables
Assign values with =:
school = "Sunrise Middle School" # Stores text (string)
students = 250 # Stores number (integer)
is_fun = True # Stores True/False (boolean)
Example:
# Calculate total candies
candies_per_student = 5
total_students = 30
total_candies = candies_per_student * total_students
print("We need", total_candies, "candies!") # Output: We need 150 candies!
4. Naming Conventions: Rules for Variable Names
Python has rules for naming variables:
Rule | ✅ Valid | ❌ Invalid |
---|---|---|
Start with a letter or _ | score, _player | 3rd_place |
Use letters, numbers, or _ | game_level2 | total-score |
Case-sensitive | HighScore ≠ highscore | |
Use descriptive names | student_age | sa |
Examples:
# Good names:
player_health = 100
max_speed = 5.8
# Bad names:
a = 10 # What does 'a' mean?
xYz = "Hi" # Confusing uppercase/lowercase
Pro Tip: Use snake_case (all lowercase with underscores) for variable names, like favorite_color.
Fun Practice: Story Generator
Let’s use variables to make a silly story!
# Fill in the blanks
adjective = "spooky"
animal = "penguin"
verb = "dance"
story = f"Once, a {adjective} {animal} loved to {verb} in class!"
print(story)
Output:
Once, a spooky penguin loved to dance in class!
Troubleshooting Tips
- ❌ SyntaxError: invalid syntax: Check for missing : or wrong indentation.
- ❌ NameError: variable not defined: Did you spell the variable name correctly?
- ❌ TypeError: Mixing data types? E.g.,
print("Age: " + 12)
→ Fix:print("Age: " + str(12))
.
Key Takeaways
- ✅ Indentation groups code blocks (use 4 spaces!).
- ✅ Comments explain your code with # or """
- ✅ Variables store data (text, numbers, True/False).
- ✅ Naming rules: Start with letters/_, use snake_case, and be descriptive!
What’s Next?
Practice with this mini-quiz!
# Fix the errors in this code:
school_name = "Maple Middle School"
number_of_students = 300
print("Welcome to " + school_name)
print("We have " + number_of_students + " students!")
Hint: Look at the data types!
Pro Tip: Experiment by changing variable values and see what happens! Coding is all about trial and error. 🚀