Lesson 2 of 34 · Fundamentals
Python Variables and Data Types
Programs need to remember things: a player's name, a score, whether a door is locked. In Python you store values in variables — names that refer to data so you can use it again later.
In this lesson you'll create Python variables, meet the core data types every program is built from, and convert values from one type to another.
Creating variables
A variable comes to life the moment you assign a value to it with =:
hero = "Ada"
level = 3
print(hero, "is level", level) # Ada is level 3
Read = as "now refers to", not as "equals". You can point a name at a new value at any time, even one computed from its old value:
level = 3
level = level + 1 # take the current value, add 1, store the result
print(level) # 4
There is no let, var or type declaration in Python. The interpreter works out the type from the value itself — this is called dynamic typing.
Naming rules
- Names may contain letters, digits and underscores, but can't start with a digit:
player2works,2playerdoesn't. - Names are case-sensitive:
scoreandScoreare two different variables. - Reserved keywords such as
if,for,classorTruecan't be used as names. - By convention Python uses snake_case: lowercase words joined by underscores, like
max_health.
Python's core data types
| Type | Examples | Holds |
|---|---|---|
int |
42, -7, 1_000_000 |
whole numbers of any size |
float |
3.14, -0.5, 2.0 |
numbers with a decimal point |
str |
"Ada", 'hi' |
text (a string of characters) |
bool |
True, False |
yes/no values |
NoneType |
None |
"no value" |
The built-in type() function tells you the type of any value:
print(type(42)) # <class 'int'>
print(type(42.0)) # <class 'float'>
print(type("42")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>
42, 42.0 and "42" are three different things: an integer, a float, and a piece of text that happens to contain digits.
Converting between types
Data often arrives as text — from a file, a web form or the keyboard. Convert it by calling the type's name like a function:
age = int("36") # 36 (int)
price = float("4.99") # 4.99 (float)
label = str(100) # "100" (str)
print(age + 1) # 37
A few rules worth knowing: int() on a float simply drops the decimal part (int(3.9) is 3), and bool() turns "empty" values such as 0, "" and None into False and almost everything else into True.
You can also assign several variables in one line:
x, y = 10, 20
Common mistakes
- Mixing text and numbers:
"5" + 5raisesTypeError: can only concatenate str (not "int") to str. Convert first:int("5") + 5. - Converting the wrong text:
int("3.5")andint("ten")raiseValueError—int()only understands whole-number text. - Lowercase booleans:
trueis just an undefined name. Python's booleans areTrueandFalse. - Using a variable before creating it stops the program with
NameError: name '...' is not defined.
Assignment
You're creating a hero for Serpent Hollow. Set these variables to the exact values and types below:
hero_name— the string"Ada"level— the integer3health— the float97.5has_sword— the booleanTruegold— the text stored ingold_text(already defined as"250"), converted to an int, sogoldbecomes250
Leave gold_text itself unchanged.
Variables and data types in other languages
- JavaScript Variables: let, const and Data Types · JavaScript
- Go Variables: var, := and Multiple Assignment · Go
- Java Variables and Primitive Data Types · Java
- C# Variables and Data Types · C#
Need a quick reminder? The Python cheat sheet has every topic of Learn Python on one page.
Lesson last updated on .