Introduction to Python

Variables Basics

Lesson 4: Variables and Data Types

What is a Variable?

A variable is like a labeled box where you can store a value. The label is the variable's name, and the value inside can change as the program runs — that's why it's called a variable!

Three illustrated boxes labeled name, age, and score with the values Tobi, 10, and 95 written inside each box respectively
In Python, you create a variable by writing its name, followed by = and the value you want to store. This is called variable assignment.

Creating Variables in Python

# Creating variables name = "Tobi" age = 10 height = 1.2 is_student = True # Printing variables print(name) print(age)

Variable Naming Rules

Python Variable Naming Rules

RuleValid ExampleInvalid Example
Must start with a letter or underscorename, _count1name, #score
Can contain letters, numbers, underscoresscore_1, player2score-1, player 2
Cannot be a Python keywordmy_list, totalfor, if, while
Case-sensitiveScore and score are different
List of variable names with green checkmarks next to valid names like player_score and red crosses next to invalid names like 1score and my-variable

Updating Variables

Variables can be updated at any point in your program. The old value is replaced with the new one.

score = 0 print(score) # Output: 0 score = 10 print(score) # Output: 10 score = score + 5 print(score) # Output: 15

Diagram of a box labeled score showing its value changing from 0 to 10 to 15 with arrows and assignment statements between each step
In Python, you do not need to declare the type of a variable. Python automatically figures out the type based on the value you assign. This is called dynamic typing.

Subscribe to our newsletter.

Get updates to news and events.