Tokens, Data Types and Mutability
This is a free course. Log in to run the code exercises and track your progress.
Tokens, Data Types and Mutability
The smallest meaningful units of a Python program are its tokens:
- Keywords: reserved words such as
if,while,def,True,None. - Identifiers: names you choose for variables and functions.
- Literals: fixed values such as
25,3.14,"CBSE",True. - Operators:
+,==,and,inand so on. - Punctuators:
( ) [ ] { } , : .and similar symbols.
Python's built-in data types: numbers (int, float, complex such as 2+3j), boolean (True/False), sequences (str, list, tuple), None (no value) and the mapping dict.
A variable is a name bound to an object. In x = 5, x is the l-value (it can appear on the left of =) and 5 the r-value. Objects are either mutable (can be changed in place: list, dict) or immutable (int, float, bool, str, tuple). Assigning a new value to an immutable variable makes it refer to a new object: id(x) changes.
a, b = 5, 9
a, b = b, a # swap with tuple assignment
print(a, b) # 9 5
print(type(2+3j)) # <class 'complex'>
Read two values from one line and swap them using tuple assignment (no third variable), then print them as a = ... b = ....
a, b = input().split()
# swap a and b in one statement
print("a =", a, "b =", b)Log in to edit and run this code.