Variables in Python
7 minutes read
Beginner friendly
Includes practice
Meet Variables – Your Code’s Memory Boxes 📦.
Welcome! In this lesson we’ll uncover variables—the first building-block of every Python program.
Think of a variable as a labeled box where you can store a piece of information, then pull it out later when you need it. Let’s dive in step-by-step.
What Is a Variable?
- A name you choose …
- … that points to some data …
- … so you can reuse that data later.
Real-life analogy: write “Cookies” on a jar. Now whenever you say “Cookies”, everyone knows which jar you mean and what’s inside.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Quick Summary
Variables = labeled storage for data in memory
They let you reuse, update, and communicate information in code
A name (label) on the left, a value on the right, joined by
=
Creating a Variable in Python
Python uses the assignment operator = (read: “gets” or “becomes”).
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Notice:
• No var, int, or string keywords—Python figures out the type automatically.
• No semicolons.
Naming Rules & Good Style
Python follows the PEP 8 style guide.
Naming Checklist
- Use letters, numbers, and underscores
_only - Must start with a letter or underscore (never a digit)
- Case-sensitive:
score≠Score - Use
snake_casefor readability:max_speed, notmaxSpeed - Avoid Python keywords (
class,for,if, …)
Quick examples (all valid):
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Reassigning & Updating
The same name can hold a new value later—Python just points the label to a different object.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Shortcut for numeric updates:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Multiple Assignment & Unpacking
Python lets you assign several variables in one line.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Unpacking works with collections too:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Hands-on Code Practice
Create two variables, swap their values, and print the result.
Practice Challenge
Create two variables aand bwith initial values 10and 20,then swap their values so that abecomes 20and bbecomes 10,and finally print both variables to confirm the swap.
Hint: use a temporary variable or Python’s tuple swap: a, b = b, a
Runtic's Learning Tips
After this lesson, write a tiny diary script that stores your name, age, and favorite food, then prints a sentence
When you see an error, treat it like a puzzle clue—not a verdict on your talent
Quick Knowledge Check
Quick Practice
3 minutesVariable Basics Quiz
Which line increases `counter` by exactly 1?
How many of these names are valid? `2cool`, `_name`, `class`, `user_3`
- You can now create, update, and name variables following Python’s style rules
- You practiced swapping values and answering quiz questions for retrieval strength
- You learned tips to keep momentum and re-use variables in your own mini-projects
AI Tutor
Solve first — tutor's here to guide.
Runtric might be wrong. Think critically.