Data types and operators
Six built-in types, which of them can change, and the difference between equal and identical.
- Name Python's core types and say which are mutable
- Use arithmetic, comparison, logical, identity and membership operators
- Explain why is and == can disagree
0_basic_data_str.py introduces the six types almost every program is built from:
my_int, my_float, my_complex = 42, 3.14, 1 + 2j
my_string = "Hello, World!" # immutable
my_list = [1, 2, 3, "Python", True] # mutable, ordered
my_tuple = (1, 2, 3, "Tuple", False) # immutable, ordered
my_dict = {"name": "Alice", "age": 30} # mutable, key -> value
my_set = {1, 2, 3, 3, 4} # mutable, unique -> {1, 2, 3, 4}
The split that matters most is mutable versus immutable. A list can be changed in place; a string or tuple cannot — “changing” one builds a new object. Dictionary keys and set elements must be immutable (strictly, hashable), which is why a tuple can be a key and a list cannot.
One correction to the script’s comments: dictionaries are no longer unordered. Since Python 3.7 they keep insertion order, and iteration follows it.
Operators
a_operators_and_conditions.py walks through each family. Two are easy to mix up:
x / y # 10 / 3 -> 3.333... true division, always a float
x // y # 10 // 3 -> 3 floor division
x % y # 10 % 3 -> 1 remainder
Floor division rounds towards minus infinity, so -7 // 2 is -4, and the remainder takes the sign of the divisor: -7 % 2 is 1.
Equal is not identical
a = [1, 2, 3]
b = a
c = [1, 2, 3]
a is b # True - two names, one object
a is not c # True - equal contents, different objects
a == c # True
== compares values; is compares identity — whether two names refer to the very same object. That distinction is the key to lesson 4, where changing “a copy” changes the original as well.
The script ends with a chained conditional, if / elif / else, where Python checks each condition in order and runs only the first branch that is true.
Try it yourself. Predict, then check: 0.1 + 0.2 == 0.3, (1, [2]) in {} (why does it raise?), -7 // 2 and -7 % 2, and whether "a" * 3 is "aaa" (and why you should never rely on the answer).