Python · basics

Data Types

Every value has a type: whole numbers, decimals, text, truth values and "nothing".

Five basic types

int

Whole numbers: 25, -10, 1000000

float

Numbers with a decimal point: 19.99, -5.5

str

Text in quotes: "Hello", 'Alice'

bool

Truth values: True, False

NoneType

The absence of a value: None

In the panels that follow, each variable box shows its type underneath.

Integer type


          # integer
          age = 25
          type(age)
          # ans: <class 'int'>
          count = -10
          # ans: negative integer
          big_num = 1000000
          # ans: large integer
        

Float type


          # float
          price = 19.99
          type(price)
          # ans: <class 'float'>
          pi = 3.14159
          # ans: float value
          negative = -5.5
          # ans: negative float
        

String type


          # string
          message = "Hello"
          type(message)
          # ans: <class 'str'>
          name = 'Alice'
          # ans: single quotes
          text = """multi
          line"""
          # ans: multiline string
        

Single, double or triple quotes all make a str; triple quotes can span lines (stored as \n).

Boolean type


          # boolean
          is_valid = True
          type(is_valid)
          # ans: <class 'bool'>
          is_empty = False
          # ans: False value
        

NoneType


          # None type
          x = None
          type(x)
          # ans: <class 'NoneType'>
          # ans: represents absence of value
          result = None
          # ans: no value yet
        

Checking types


          # type() function
          x = 5
          type(x)
          # ans: <class 'int'>
          y = "hello"
          type(y)
          # ans: <class 'str'>
          z = 3.14
          type(z)
          # ans: <class 'float'>
        

Exercises

Name the type, then press → to run the line and check the box.

Exercises · part 1


          # what type is this?
          x = 5.0
          # ans: <class 'float'>
          # what type is this?
          y = "123"
          # ans: <class 'str'>
          # what type results?
          result = 10 / 2
          # ans: <class 'float'>
          # what type is this?
          z = True + False
          # ans: <class 'int'> (True=1, False=0)
        

Exercises · part 2


          # what type?
          a = 5
          b = 2
          c = a / b
          # ans: <class 'float'>
          # what type?
          x = 10
          y = 3
          z = x // y
          # ans: <class 'int'>
        

Exercises · part 3


          # tricky: what type?
          value = "5" + "3"
          # ans: <class 'str'> (result is "53")
          # what is the type?
          x = None
          # ans: <class 'NoneType'>
          # what type is this expression?
          result = 5 > 3
          # ans: <class 'bool'>
        

Exercises · part 4


          # what happens?
          x = int(True)
          # ans: 1, type is <class 'int'>
          # mixing types?
          x = 5 + 2.5
          # ans: 7.5, type is <class 'float'>
          # edge case?
          x = bool([])
          # ans: False, type is <class 'bool'>
        

Exercises · part 5


          # what type?
          x = 0
          # ans: <class 'int'>
          # empty string type?
          s = ""
          # ans: <class 'str'>
          # what type?
          x = float(5)
          # ans: 5.0, <class 'float'>
        

Remember

/ makes a float

Even 10 / 2 is 5.0; use // for a whole-number result.

Quotes make a string

"123" is text, and "5" + "3" joins text into "53".

bool is a kind of int

True counts as 1 and False as 0 in arithmetic.

Ask Python

type(value) tells you the type of anything.