Python - Data Types

ʕ•ﻌ•ʔ
Python pixel-art

Data types determine the context of a value, variable, or memory bits.


Built-in Data Types

Text Sequence Type (str)

"Hi mom!"
'This world is an illusion, exile!'

Numeric Types (int, float & complex)

1337
3.14
7j

Boolean Type (True & False)

True
False

Sequence Types (list, tuple & range)

["Hi!", 33, True]
("Bye!", 66, False)
range(3, 15, 3)

Mapping Type (dict)

{ 
  "key": "value",
  "num": 1
}

Set Types (set, frozenset)

{ "Dog", "Cat", "Elephant" }
frozenset({})

The ‘range’ Type

The range type represents an immutable sequence of numbers and is commonly used for looping.

for n in range(10):
  print(n)
x = list(range(0, 100, 3))
print(x)

https://docs.python.org/3/library/functions.html#func-range


List vs Tuple

The main difference is that a list is mutable (can be changed), while a tuple is immutable (cannot be changed).

x = [1, 2, 3]
x.append(4)
print(x) # ok
x = (1, 2, 3)
x.append(4) # error

Set vs FrozenSet

The main difference is that a set is mutable (can be changed), while a frozenset is immutable (cannot be changed).

x = {1, 2, 3}
x.add(4)
print(x) # ok
x = frozenset({1, 2, 3})
x.add(4) # error
CC0 1.0 Universal; Felipe Izolan;