Python - Data Types
Data types are the kinds of data that you can store.
Built-in Data Types
- string - A string of characters;
'im globglogabgalab'
"i love books"
- int, float, complex - numbers, integers, float
(numbers with decimal-point) and complex;
77
3.14
1j
- boolean - true or false;
True
False
- list, tuple, range - list of values, tuple and
range (used in loops);
[1, 'hi!', false]
(1, 'hi!', false)
range(0,10,2)
- dict - key/value;
{((name = "The rock"), (strength = 999))}
- set, frozenset - list with unique values and
unordered values;
{("apple", "orange", "pineapple")}
- none - null, none, nothing, empty and etc...
None
List vs Tuple
The main difference is that lists can be changed (mutable), while tuples cannot (immutable).
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
my_list[1] = 4 # <- Ok!
my_tuple[1] = 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 = { "a", "b", "c" }
y = frozenset(x)
x.add("d") # <- Ok!
y.add("d") # <- Error!
What is the 'range' data type?
The range data type is simply used in for-loops.
for n in range(10):
print(n)
What is 'Iterables'?
any sequence that can be iterated over, such as:
- list
- tuple
- dictionary
- set