Python Data Types Cheat Sheet

Updated: March 4, 2023 By: Wolf Post a comment

A succinct, comprehensive cheat sheet for Python data types. You can use it for quick lookup during your work.

TypeDescriptionExample
intinteger values123, -120, 0
floatfloating-point values1.21, 3.141, -9.8
boolBoolean valuesTrue, False
strstrings of charactersSling Academy, hello, hell
listordered collections of values[1, 2, 3], [‘s’, ‘l’, ‘i’, ‘n’, ‘g’]
tupleordered, immutable collections of values(1, 2, 3), (‘a’, ‘b’, ‘c’)
setunordered collections of unique values{1, 2, 3, 4}, {‘d’, ‘e’, ‘f’}
dictunordered collections of key-value pairs{‘name’: ‘Wolf’, ‘age’: 90}

Below are some more details about each data type (with concrete examples).

int

An int is a whole number without a decimal point. Integers in Python can be positive or negative. They can be created using the int() constructor or simply by typing a number without a decimal point.

Example:

x = 32  # positive integer
y = -10  # negative integer
z = 0  # zero

print(x, y, z)

Output:

32 -10 0

float

A float is a decimal number. Like integers, floating-point numbers in Python can be either positive or negative. They can be created using the float() constructor or just by typing a number with a decimal point.

Example:

x = 0.15  # positive float
y = -2.5  # negative float
z = 0.0  # zero as a float

print(x, y, z)

Output:

0.15 -2.5 0.0

bool

A bool is a boolean value, which can either be True or False. Booleans are typically used in conditional statements and comparisons.

Example:

x = True
y = False

if x:
    print("x is true")
    
if not y:
    print("y is false")

Output:

x is true
y is false

str

A str is a string of characters. Strings in Python can be enclosed in single quotes or double quotes and can contain letters, numbers, and special characters.

Example:

x = 'sling'  # string with single quotes
y = "academy"  # string with double quotes
z = "123"  # string containing numbers

print(x, y, z)

Output:

sling academy 123

list

A list is an ordered collection of values. Lists in Python can contain values of different data types and can be modified after they are initialized.

Example:

x = [1, 2, 3]  # list of integers
y = ['a', 'b', 'c']  # list of strings
z = [1, 'a', True]  # list of mixed data types

print(x, y, z)

Output:

[1, 2, 3] ['a', 'b', 'c'] [1, 'a', True]

See also: 3 Ways to Select Random Elements from a List in Python

tuple

A tuple is an ordered, immutable collection of values. Tuples in Python are similar to lists, but they cannot be modified after they are created. In other words, tuples are immutable.

Example:

x = (1, 2, 3)  # tuple of integers
y = ('a', 'b', 'c')  # tuple of strings
z = (1, 'a', True)  # tuple of mixed data types

print(x, y, z)

Output:

(1, 2, 3) ('a', 'b', 'c') (1, 'a', True)

set

A set is an unordered collection of unique values. Sets in Python can contain values of different data types, but each value can only appear once in the set.

Example:

x = {1, 2, 3}  # set of integers
y = {'a', 'b', 'c'}  # set of strings
z = {1, 'a', True}  # set of mixed data types

print(x, y, z)

Output:

{1, 2, 3} {'b', 'a', 'c'} {1, 'a'}

dict

A dict is an unordered collection of key-value pairs. Dictionaries in Python can contain values of different data types and are often used to store and manipulate data in a structured way.

Example:

x = {'name': 'Wolf', 'age': 90}  
# dictionary with string keys and mixed data types for values

y = {1: 'one', 2: 'two'}  
# dictionary with integer keys and string values

print(x, y)

Output:

{'name': 'Wolf', 'age': 90} {1: 'one', 2: 'two'}

Afterword

Above is the most concise and concise information about data types in Python. It is very useful in reviewing and looking up, as well as helping developers have an overview of data in Python.

If you want to learn about each specific problem with clear examples and explanations, check out the other Python articles on Sling Academy.