type()A data type tells Python what kind of value a variable holds — a number, text, a list, etc. Each type has its own set of allowed operations.
| Family | Members |
|---|---|
| Numeric | int, float, complex |
| Boolean | bool — only True / False |
| Sequence | str, list, tuple |
| Mapping | dict — key/value pairs |
| None | NoneType — the value None |
type() Functiontype(x) tells you the type of any value or variable.
print(type(10)) # <class 'int'> print(type(3.14)) # <class 'float'> print(type("hi")) # <class 'str'> print(type([1, 2])) # <class 'list'> print(type(True)) # <class 'bool'> print(type((1, 2))) # <class 'tuple'> print(type({"a": 1})) # <class 'dict'> print(type(None)) # <class 'NoneType'>
Whole numbers, positive or negative, with no decimal point. Python integers can be as big as memory allows — no overflow.
x = 25 · y = -100 · z = 0 · big = 10 ** 50 (very large allowed)Numbers with a decimal point. Also accepts scientific notation using e.
pi = 3.14 · temp = -2.5 · big = 1.5e6 (means 1.5 × 106 = 1500000.0) · tiny = 2e-3 (= 0.002)Has a real and imaginary part. Written as a + bj. The letter j is used (not i).
c = 2 + 3j print(c.real, c.imag) # 2.0 3.0 print(type(c)) # <class 'complex'>
| Property | int | float | complex |
|---|---|---|---|
| Decimal point? | No | Yes | n/a |
| Example | 10 | 3.14 | 2+3j |
10/3 | 3.333... (always float) | — | |
10//3 | 3 (int if both int) | — | |
10 / 5 returns 2.0 (float), not 2 (int). The / operator always produces a float in Python 3. Use // for integer division.int(x), float(x), complex(x) let you convert. int(3.9) is 3 — the decimal part is truncated, not rounded. Use round() if you actually want rounding.
print(int(7.9)) # 7 (truncated) print(round(7.9)) # 8 (rounded) print(float(5)) # 5.0 print(int("42")) # 42 (string -> int)
NoneHas only two values: True and False. The first letters must be capital. Booleans are the natural result of comparisons and logical operations.
print(5 > 3) # True print(2 == 4) # False print(True and False) # False print(not 0) # True
True behaves like 1 and False like 0 in arithmetic.
True + True gives 2 · False * 99 gives 0 · True + 4.5 gives 5.5.When any value is used in an if, Python first converts it to bool. The values that count as False:
0, 0.0, 0j""[], tuple (), dict {}None and False itselfEverything else is True — including non-zero numbers and any non-empty container.
None valueNone means "no value" or "nothing here yet". It is different from 0 and from the empty string.
result = None print(type(result)) # NoneType print(result == 0) # False print(result is None) # TrueFunctions that don't
return anything implicitly return None.is None — not == None. x is None reads more clearly and is the recommended Python style.A sequence stores items in a fixed order. Each item has an index (position number). Indexing in Python starts at 0. Negative indices count from the right (-1 = last).
An ordered collection of characters in single or double quotes. Strings are immutable — once created they cannot be changed.
name = "Python" print(name[0]) # P print(name[-1]) # n print(name[0:3]) # Pyt (slicing) print(name[::-1]) # nohtyP (reversed) print(len(name)) # 6
Concatenate with +, repeat with *: "ab" * 3 gives "ababab". Membership: "th" in "Python" is True.
Items in square brackets, separated by commas. A list can hold values of different types. Lists are mutable — items can be changed, added or removed.
marks = [88, 76, 92, 55] marks[2] = 95 # change index 2 marks.append(80) # add at end marks.pop(0) # remove first print(marks) # [76, 95, 55, 80] print(len(marks), sum(marks)) # 4 206
Common methods: append, insert, pop, remove, sort, reverse, count, index.
len(), +, * and in just like strings — plus the mutating methods above.Same as a list but unchangeable after creation. Written in round brackets.
point = (10, 20) print(point[0]) # 10 print(point + (30,)) # (10, 20, 30) # point[0] = 5 -> TypeError (immutable) single = (5,) # note the comma empty = ()Use a tuple when the data should not change — coordinates, days of the week, RGB colours.
(5,). Without the comma, (5) is just the integer 5 in brackets.| Property | str | list | tuple |
|---|---|---|---|
| Brackets | " " | [ ] | ( ) |
| Mutable? | No | Yes | No |
| Mixed types? | chars only | any | any |
| Indexing? | Yes | Yes | Yes |
A dictionary stores key:value pairs in curly braces. You access values by their key, not by an index.
student = {"name": "Vivek",
"class": 11,
"marks": 92}
print(student["name"]) # Vivek
student["marks"] = 95 # update
student["section"] = "A" # add new key
print(len(student)) # 4
Keys must be unique and immutable (string, int, tuple). Values can be anything.Common methods: keys(), values(), items(), get(), pop().
This is one of the most-asked exam topics. The classification tells whether a value can change in place.
| Mutable (changeable) | Immutable (fixed) |
|---|---|
list | int, float, complex |
dict | bool |
| — | str |
| — | tuple |
| — | NoneType |
a = [1, 2, 3] a[0] = 99 print(a) # [99, 2, 3] same list, content modified
s = "hello" # s[0] = "H" -> TypeError s = "Hello" # this creates a NEW string and rebinds s
tuple itself is immutable, but if a tuple contains a list, that inner list can still be modified. The tuple's structure cannot change, but its mutable items still can.type(3.0) is:
10 / 5 in Python 3 is:
bool("") is:
(5,) the trailing comma is needed because:
int(7.9) is:
print(type(True + 5))Output:
a = [1, 2, 3]
b = a
b.append(4)
print(a)
Output: s = "Python" print(s[1:4]) print(s[::-1])Output:
t = (10, 20, 30) print(t + (40,)) print(len(t))Output:
L = [5, 1, 9, 3] L.sort() print(L) print(sum(L))Output:
d = {"a": 1, "b": 2}
d["c"] = 3
print(len(d), d["b"])
Output: print(bool(0), bool([]), bool("0"))Output:
x = None print(x == 0) print(x is None)Output:
Each snippet has exactly one error. Mark the line number and write the corrected line.
1 t = (1, 2, 3)
2 t[0] = 99
3 print(t)
Wrong line: Correction: 1 d = {[1, 2]: "hi"}
2 print(d)
Wrong line: Correction: 1 s = "hello" 2 s[0] = "H" 3 print(s)Wrong line: Correction:
1 L = [10, 20, 30]
2 print(L[3])
Wrong line: Correction: 1 t = (5) 2 print(type(t)) 3 # Expected: tuple, got intWrong line: Correction:
1 d = {"a": 1, "b": 2}
2 print(d["c"])
Wrong line: Correction: / and //? Show the result of 17/5 and 17//5.
type() function do? List the data types of these literals: 5, 5.0, "5", [5], (5,).
Each program should produce a clear output. Use a separate sheet if you need more space.
colors = ("red", "green", "blue", "yellow") and write code to: print the second colour, print the colours in reverse order, and print whether "green" is in the tuple.
student with keys name, roll, marks. Then update the marks, add a new key grade, and print the final dictionary.
L = [25, 10, 47, 3, 88, 19]. Print the maximum, the minimum, the sum and the average of all elements (use built-ins max, min, sum, len).
L = [1, 2, 3] and a tuple T = (1, 2, 3). Try to change the first element of each. Print the result and explain in one line.