Skip to content
Codewilds

Lesson 2 of 34 · Fundamentals

Python Variables and Data Types

easy10 XP3 min readFree lesson

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: player2 works, 2player doesn't.
  • Names are case-sensitive: score and Score are two different variables.
  • Reserved keywords such as if, for, class or True can'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" + 5 raises TypeError: can only concatenate str (not "int") to str. Convert first: int("5") + 5.
  • Converting the wrong text: int("3.5") and int("ten") raise ValueError — int() only understands whole-number text.
  • Lowercase booleans: true is just an undefined name. Python's booleans are True and False.
  • 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:

  1. hero_name — the string "Ada"
  2. level — the integer 3
  3. health — the float 97.5
  4. has_sword — the boolean True
  5. gold — the text stored in gold_text (already defined as "250"), converted to an int, so gold becomes 250

Leave gold_text itself unchanged.

Variables and data types in other languages

Need a quick reminder? The Python cheat sheet has every topic of Learn Python on one page.

Lesson last updated on .

Write code ↓
Python+10 XP

Press Run to execute your code, or Submit to check it against the tests.