Variables and Data Types in Programming

Supporting explainer · Computer Science · 1 min read

What variables and common data types are, with short Python examples.

In programming, a variable is a named place that stores a value. The value can change as the program runs.

Creating variables (Python)

age = 15
price = 4.99
name = "Sam"
is_student = True

Common data types

TypeMeaningExample
Integer (int)whole numbers7, -3
Float (float)numbers with decimals3.14
String (str)text"hello"
Boolean (bool)true or falseTrue

Operations depend on type

Integer and decimal division in Python

Naming

Use clear names, such as total_score, not x. In Python, names cannot start with a digit or contain spaces.

Assignment is not equality

In x = x + 1, the = means "store the result on the right in the variable on the left", not "x equals x plus one". After it runs, x is one larger.

Common mistakes

Quick recap

Variables store values. Each value has a type that determines what operations are allowed. Convert types when needed.

Key vocabulary

Keep learning

More: Computer Science hub · Computer Science practice questions