runtric-logo
Python Basic 1 - Getting StartedVariables in Python (2/4)
Chapter 2
Python Basic 1 - Getting Started

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?

  1. A name you choose …
  2. … that points to some data …
  3. … 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.

Example Code

Interactive Code Editor
python

This 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”).

Example Code

Interactive Code Editor
python

This 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: scoreScore
  • Use snake_case for readability: max_speed, not maxSpeed
  • Avoid Python keywords (class, for, if, …)

Quick examples (all valid):

Example Code

Interactive Code Editor
python

This 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.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Shortcut for numeric updates:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Multiple Assignment & Unpacking

Python lets you assign several variables in one line.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Unpacking works with collections too:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Hands-on Code Practice

Create two variables, swap their values, and print the result.

Practice Challenge

Interactive Code Editor
📝 Your Task:

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.

python

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 minutes

Variable Basics Quiz

Which line increases `counter` by exactly 1?

How many of these names are valid? `2cool`, `_name`, `class`, `user_3`

Lesson Complete!
100%
Progress
What You've Learned:
  • 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

Sign in to track your progress

Chapter Progress

0 secs

AI Tutor

Powered by Runtric

Solve first — tutor's here to guide.

Runtric might be wrong. Think critically.