Python Beginner Concepts Tutorial

# Types and Values

### Value¶

A value is an assignment your program works with such as a letter or number. Examples include:

• 5
• groceries
• 3.14

### Type¶

Each value belongs to a specific type.

In Python, we can check for the type of a value using a built-in function - meaning it's already available to you in the Python programming language - called type().

In [1]:
type(5)

Out[1]:
int

The type of the value 4 is int - referred to out loud as an integer.

#### Numeric types and examples¶

type name example
int 5
float 3.14
In [2]:
type(5)

Out[2]:
int
In [3]:
type(3.14)

Out[3]:
float

#### Sequence types and examples¶

type name example
str "groceries"
list [0, 1, 2, 3]
tuple ("pi", 3.14)
In [4]:
type("groceries")

Out[4]:
str

str is referred to out loud as a string.

In [5]:
type([0, 1, 2, 3])

Out[5]:
list
In [6]:
type(("pi", 3.14))

Out[6]:
tuple

#### Mapping type and example¶

type name example
dict {"joe": [6.8, 7.2], "matt": [8.1, 7.9]
In [7]:
type({"joe": [6.8, 7.2], "matt": [8.1, 7.9]})

Out[7]:
dict

dict is referred to out loud as a dictionary or hash map - a consistent term used to refer to this structure in other programming languages.

If you utilize inheritance, you may want to consider isinstance() rather than type(). The differences are explained in detail in this great StackOverflow post.