Types and Values¶
Date published: 2018-02-14
Category: Python
Subcategory: Beginner Concepts
Tags: lists, tuples, dictionaries
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()
.
type(5)
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 |
type(5)
int
type(3.14)
float
Sequence types and examples¶
type name | example |
---|---|
str | "groceries" |
list | [0, 1, 2, 3] |
tuple | ("pi", 3.14) |
type("groceries")
str
str
is referred to out loud as a string.
type([0, 1, 2, 3])
list
type(("pi", 3.14))
tuple
Mapping type and example¶
type name | example |
---|---|
dict | {"joe": [6.8, 7.2], "matt": [8.1, 7.9] |
type({"joe": [6.8, 7.2], "matt": [8.1, 7.9]})
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.
Learn More¶
The above types are not completely comprehensive, but are the most common ones I utilize. You can learn more about Python's built-in types on Python's official documentation page.
If you utilize inheritance, you may want to consider isinstance()
rather than type()
. The differences are explained in detail in this great StackOverflow post.