Coding/Learn/python/variables
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnVariables
5 / 32 0%
Lesson 05 of 32
6 mins read

Variables

PreviousNext Lesson
In Plain English

Variables are labeled storage containers in your program memory. If you put sugar into a jar labeled "sugar", you can grab it whenever you need sweetness. In Python, you write user_name = "Alex", and Python holds onto it until you decide to update it.

The Mental Model

"Think of variables as named cardboard moving boxes. You write a name on the outside in Sharpie ("high_score"), toss a value inside (9500), and whenever you ask Python for "high_score", it opens the box and hands you the 9500."

Deep Dive: How It Works

Variables in Python are dynamically typed. That means you do not need to explicitly declare if a variable holds text or a number ahead of time. Python inspects the value you assign and assigns the type under the hood.

The single equal sign (=) is the assignment operator. It tells Python to evaluate whatever is on the right side first, and store the result into the label on the left side.

Variable names in Python follow snake_case convention: all lowercase letters with words joined by underscores (e.g. max_speed, user_email). Names must start with a letter or underscore, and cannot start with a number or contain spaces.

Syntax Blueprint

variable_name = value

The label is on the left, the single equal sign acts as the assignment arrow, and the value is on the right.

PointCore Rules to Remember

Point star
Assignment with =: The single equal sign stores the right-hand value into the left-hand name.
Point star
Naming Rules: Use lowercase and underscores (snake_case). Cannot start with a number.
Point star
Dynamic Reassignment: You can update what is inside a variable anytime by assigning a new value.

Common Beginner Traps & How to Fix Them

Mistake:Putting the value on the left side: 100 = score

Why it happens: In math, 100 = x is valid, but in programming the container name must always be on the left.

How to fix: Always write container_name = value (e.g. score = 100).

Mistake:Using reserved Python keywords as names

Why it happens: Naming a variable "print", "class", or "for" breaks Python syntax.

How to fix: Choose descriptive custom names like current_print_job or user_class.

Pro Tip: You can assign multiple variables in one line: x, y, z = 10, 20, 30.

Live Interactive Example

Hit Run Code to see it live
Storing and Updating Variable Values
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: Python keeps track of the current value inside the age box. When we assign 21, the old value 20 is replaced.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create and print a variable

  • PointCreate a variable called language and assign it the string value "Python".
  • PointThen print the language variable.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates