Every value in Python — a marks score, a name, a list of friends, a True/False answer — has a data type. The data type decides what you can do with that value: you can add two numbers, but you cannot divide a name by 2! In this blog you will master all of Python's built-in data types, mutable vs immutable types, type checking, type conversion and the classic traps interviewers ask about.
Watch the video first, then use this blog as your notes. Every section has runnable examples, and at the end you will find a "Predict the Output" quiz, practice challenges, a self-check list, interview questions and a quick summary.
How to use this blog: Open the Python shell (type python in your terminal) or any online Python editor and type every example yourself. Change the values, break the code and see what happens — that is how concepts really stick.
A data type is a category of values that tells Python how to store the value and which operations are allowed on it. Python is dynamically typed: you never declare the type; Python figures it out from the value.
x = 10 # int
x = "ten" # now the same variable holds a str - totally fine in Python
print(type(x)) # <class 'str'>Key idea: In Python, everything is an object, and every object has a type. Use type(value) to see it.
| Category | Type | Example | Mutable? |
|---|---|---|---|
| Numeric | int | 42, -7, 0 | No |
| float | 3.14, -0.5, 2e3 | No | |
| complex | 3 + 4j | No | |
| Boolean | bool | True, False | No |
| Text | str | "Python" | No |
| Sequence | list | [1, 2, 3] | Yes |
| tuple | (1, 2, 3) | No | |
| range | range(5) | No | |
| Set | set | {1, 2, 3} | Yes |
| frozenset | frozenset({1, 2}) | No | |
| Mapping | dict | {"name": "Riya"} | Yes |
| None | NoneType | None | No |
| Binary | bytes, bytearray | b"hi" | bytes: No, bytearray: Yes |
print(type(3), type(3.0), type("3"), type(True), type(None), type(3 + 4j))
# <class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'NoneType'> <class 'complex'>a = 25
big = 2 ** 100
print(big) # 1267650600228229401496703205376
print(1_000_000) # 1000000 (underscores for readability)
print(0b101, 0o17, 0xff) # 5 15 255 (binary, octal, hexadecimal)Unlike C or Java, Python integers never overflow — they grow as big as your memory allows.
pi = 3.14159
speed = 2.5e3 # 2500.0 (scientific notation)
print(0.1 + 0.2) # 0.30000000000000004 (!)
print(0.1 + 0.2 == 0.3) # False
print(round(0.1 + 0.2, 2) == 0.3) # TrueWhy 0.1 + 0.2 is not 0.3: Computers store floats in binary, and 0.1 cannot be represented exactly in binary (just like 1/3 = 0.333... in decimal). For money, use the decimal module.
z = 3 + 4j
print(z.real, z.imag) # 3.0 4.0
print(abs(z)) # 5.0| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Add | 7 + 2 | 9 |
| - | Subtract | 7 - 2 | 5 |
| * | Multiply | 7 * 2 | 14 |
| / | Divide (always float) | 7 / 2 | 3.5 |
| // | Floor division | 7 // 2 | 3 |
| % | Remainder (modulus) | 7 % 2 | 1 |
| ** | Power | 7 ** 2 | 49 |
is_passed = True
print(5 > 3) # True
print(type(False)) # <class 'bool'>
print(True + True) # 2 (bool is a subclass of int!)
print(isinstance(True, int)) # TrueEvery value can be tested as True or False. These are falsy; everything else is truthy:
| Falsy values |
|---|
| False, None, 0, 0.0, "" (empty string), [], (), {}, set() |
print(bool(0), bool(""), bool([])) # False False False
print(bool(" "), bool("False"), bool(-1)) # True True True (!)s = "Python"
print(len(s)) # 6
print(s[0], s[-1]) # P n (indexing)
print(s[1:4]) # yth (slicing)
print(s[::-1]) # nohtyP (reverse)
print(s.upper(), s.lower()) # PYTHON python
print("th" in s) # True
print(s + " 3") # Python 3Strings are immutable: s[0] = "J" raises TypeError. Create a new string instead: "J" + s[1:].
fruits = ["apple", "mango", "banana"] # list - ordered, mutable
fruits.append("kiwi")
fruits[0] = "grapes"
print(fruits) # ['grapes', 'mango', 'banana', 'kiwi']
point = (10, 20) # tuple - ordered, immutable
print(point[0]) # 10
# point[0] = 5 -> TypeError: 'tuple' object does not support item assignment
r = range(1, 6) # range - sequence of numbers
print(list(r)) # [1, 2, 3, 4, 5]Single-item tuple trap: (5) is just the int 5 in brackets. A one-item tuple needs a comma: (5,).
nums = {1, 2, 2, 3, 3, 3}
print(nums) # {1, 2, 3} duplicates removed
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # {1, 2, 3, 4, 5} union
print(a & b) # {3} intersection
print(a - b) # {1, 2} difference
empty = set() # NOT {} - that creates an empty dict!student = {"name": "Riya", "age": 17, "marks": [92, 88]}
print(student["name"]) # Riya
student["city"] = "Lucknow" # add a new key
print(student.get("phone", "N/A")) # N/A (safe access)
print(student.keys())
print(len(student)) # 4Keys must be immutable (str, int, tuple). Since Python 3.7, dictionaries remember insertion order.
result = None
print(result is None) # True (use "is", not "==", to check None)
def greet():
print("Hi")
print(greet()) # Hi, then None - functions without return give None| Immutable (cannot change in place) | Mutable (can change in place) |
|---|---|
| int, float, complex, bool, str, tuple, frozenset, bytes | list, dict, set, bytearray |
x = [1, 2, 3]
y = x # y points to the SAME list
y.append(4)
print(x) # [1, 2, 3, 4] <-- x changed too!
a = [1, 2, 3]
b = a[:] # or a.copy() - creates a NEW list
b.append(9)
print(a) # [1, 2, 3]Interview favourite: Variables in Python are names pointing to objects. Assigning y = x does not copy a list — both names refer to the same object. Use id(x) to see an object's identity.
x = 42
print(type(x) == int) # True
print(isinstance(x, (int, float))) # True (preferred way)
print(int("25"), int(3.99)) # 25 3 (int() truncates, does not round)
print(float(5), float("2.5")) # 5.0 2.5
print(str(100) + "5") # 1005
print(list("abc")) # ['a', 'b', 'c']
print(tuple([1, 2])) # (1, 2)
print(set([1, 1, 2])) # {1, 2}
print(int("101", 2)) # 5 (binary string to int)| Conversion | Example | Result |
|---|---|---|
| Implicit (automatic) | 3 + 2.5 | 5.5 (int promoted to float) |
| Explicit (type casting) | int("7") + 3 | 10 |
| Not allowed | "7" + 3 | TypeError |
Don't run the code yet! Write your answers in a notebook, then check the Answers section at the end of this blog.
Q1. What is the output?
print(type(7 / 7))Q2. What is the output?
print(7 // 2, 7 % 2)Q3. What is the output?
print(bool("False"))Q4. What is the output?
print(type((5)), type((5,)))Q5. What is the output?
print(len({1, 1, 2, 2, 3}))Q6. What is the output?
a = [1, 2]
b = a
b.append(3)
print(a)Q7. What is the output?
print(True + 5)Q8. What is the type of {} ?
print(type({}))Solve these from easy to tricky:
Solutions for challenges 2, 3 and 4 are in the Answers section.
Tick off each skill once you are confident:
Q: What are mutable and immutable data types?
Mutable objects (list, dict, set) can be changed after creation. Immutable objects (int, float, str, tuple) cannot — any "change" creates a new object.
Q: Difference between list and tuple?
Lists are mutable and use []; tuples are immutable, use (), are slightly faster and can be used as dictionary keys.
Q: Why can't a list be a dictionary key?
Dictionary keys must be hashable, and mutable objects like lists are not hashable.
Q: What is the difference between == and is?
== compares values; is checks whether two names refer to the same object.
Q: Is Python statically or dynamically typed?
Dynamically typed (types are checked at runtime) and strongly typed (it won't silently add a str to an int).
| Concept | Syntax / Key point |
|---|---|
| Integer | x = 10 |
| Float | x = 3.14 |
| String | x = "hi" |
| Boolean | x = True |
| List | x = [1, 2] |
| Tuple | x = (1, 2) |
| Set | x = {1, 2} |
| Dict | x = {"a": 1} |
| Check type | type(x), isinstance(x, int) |
# Challenge 2
a = "15"
b = "27"
print(int(a) + int(b)) # 42
# Challenge 3
nums = [4, 2, 4, 1, 2, 9]
print(sorted(set(nums))) # [1, 2, 4, 9]
# Challenge 4
book = {"title": "Python Basics", "author": "Coding Battle", "price": 299}
for value in book.values():
print(value)You now understand every built-in Python data type and the difference between mutable and immutable objects — a concept that will save you from many bugs. Next, take a deep dive into the most used data structure: Python Lists.
If this blog helped you, watch the full video above, subscribe to the channel and share it with a friend who is learning Python. Happy coding!