Chapter 9 — Python Data Types

Easy-language Notes · Part 1 of 2 (tree · numbers · boolean · None)
REVISION NOTES
Class XI · Computer Science (083)
Sheet 1 (Front)

1. The Data-Type Tree · 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.

A bottle is for liquid, a basket is for fruits, a wallet is for money. You wouldn't put soup in your wallet. Similarly, each Python data type has a job — numbers do maths, strings hold text, lists hold many items.

Built-in Type Families (CBSE syllabus)

FamilyMembers
Numericint, float, complex
Booleanbool — only True / False
Sequencestr, list, tuple
Mappingdict — key/value pairs
NoneNoneType — the value None

The type() Function

type(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'>
Python is dynamically typed. The same variable can hold different types at different points. The type lives with the value, not with the variable name.

2. Numeric Types — int, float, complex

int — integers

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)

float — decimals

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)

complex — complex numbers

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'>

Quick comparison

Propertyintfloatcomplex
Decimal point?NoYesn/a
Example103.142+3j
10/33.333... (always float)—
10//33 (int if both int)—
Even 10 / 5 returns 2.0 (float), not 2 (int). The / operator always produces a float in Python 3. Use // for integer division.

Type conversion among numbers

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)

3. Boolean · None

bool — the True/False type

Has 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

Booleans are also numbers

True behaves like 1 and False like 0 in arithmetic.

True + True gives 2 · False * 99 gives 0 · True + 4.5 gives 5.5.

Truthy and Falsy values

When any value is used in an if, Python first converts it to bool. The values that count as False:

  • The number 0, 0.0, 0j
  • The empty string ""
  • Empty list [], tuple (), dict {}
  • None and False itself

Everything else is True — including non-zero numbers and any non-empty container.

NoneType — the None value

None 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)     # True
Functions that don't return anything implicitly return None.
Test for None with is None — not == None. x is None reads more clearly and is the recommended Python style.

4. Sequences — str & list

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).

str — string

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.

list — ordered, mutable

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.

Lists support indexing, slicing, len(), +, * and in just like strings — plus the mutating methods above.

Chapter 9 — Python Data Types

Easy-language Notes · Part 2 of 2 (str · list · tuple · dict · mutability)
REVISION NOTES
Class XI · Computer Science (083)
Sheet 1 (Back)

5. Tuple · Dictionary

tuple — ordered, immutable

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.
A single-item tuple must have a trailing comma: (5,). Without the comma, (5) is just the integer 5 in brackets.

String vs List vs Tuple — at a glance

Propertystrlisttuple
Brackets" "[ ]( )
Mutable?NoYesNo
Mixed types?chars onlyanyany
Indexing?YesYesYes

dict — key/value mapping

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().

6. Mutable vs Immutable

This is one of the most-asked exam topics. The classification tells whether a value can change in place.

Mutable (changeable)Immutable (fixed)
listint, float, complex
dictbool
—str
—tuple
—NoneType
A mutable object is like a notebook — you can rub out and rewrite words on the same page. An immutable object is like a printed photo — if you want a different image, you must create a new one.
A list can be changed:
a = [1, 2, 3]
a[0] = 99
print(a)         # [99, 2, 3]   same list, content modified
A string cannot:
s = "hello"
# s[0] = "H"  -> TypeError
s = "Hello"      # this creates a NEW string and rebinds s

Why does this matter?

  • If you pass a list to a function and the function changes it, the caller's list also changes.
  • If you pass a string, the function cannot affect the original.
  • Only immutable objects can be used as dictionary keys.
Memory trick (NCERT-style): "Numbers, Booleans, Strings, Tuples and None are immutable. Lists and Dictionaries are mutable."
A common exam catch: 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.

Chapter 9 Worksheet — Python Data Types

Total: 50 marks  |  Time: 75 min
Name: Class/Sec: Roll No.: Date:

Section A — Multiple Choice (1 × 10 = 10)

  1. Which of the following is a mutable data type?
    (a) tuple(b) string(c) list(d) int
  2. The output of type(3.0) is:
    (a) <class 'int'>(b) <class 'float'>(c) <class 'number'>(d) <class 'real'>
  3. Which of these uses round brackets?
    (a) list(b) tuple(c) dict(d) string
  4. Which type uses key:value pairs?
    (a) list(b) tuple(c) dict(d) str
  5. The result of 10 / 5 in Python 3 is:
    (a) 2(b) 2.0(c) "2"(d) Error
  6. Which is NOT a sequence type?
    (a) str(b) list(c) tuple(d) dict
  7. The value of bool("") is:
    (a) True(b) False(c) None(d) Error
  8. In (5,) the trailing comma is needed because:
    (a) it makes a list(b) Python ignores it(c) without it, it is just 5(d) it's optional
  9. Which can be a valid dictionary key?
    (a) [1, 2](b) {1: 2}(c) (1, 2)(d) [3, 4, 5]
  10. The output of int(7.9) is:
    (a) 7(b) 8(c) 7.9(d) "7.9"

Section B — Find the Output (1 × 8 = 8)

  1. print(type(True + 5))
    Output:
  2. a = [1, 2, 3]
    b = a
    b.append(4)
    print(a)
    Output:
  3. s = "Python"
    print(s[1:4])
    print(s[::-1])
    Output:
  4. t = (10, 20, 30)
    print(t + (40,))
    print(len(t))
    Output:
  5. L = [5, 1, 9, 3]
    L.sort()
    print(L)
    print(sum(L))
    Output:
  6. d = {"a": 1, "b": 2}
    d["c"] = 3
    print(len(d), d["b"])
    Output:
  7. print(bool(0), bool([]), bool("0"))
    Output:
  8. x = None
    print(x == 0)
    print(x is None)
    Output:

Section C — Find the Error (1 × 6 = 6)

Each snippet has exactly one error. Mark the line number and write the corrected line.

  1. 1  t = (1, 2, 3)
    2  t[0] = 99
    3  print(t)
    Wrong line:   Correction:
  2. 1  d = {[1, 2]: "hi"}
    2  print(d)
    Wrong line:   Correction:
  3. 1  s = "hello"
    2  s[0] = "H"
    3  print(s)
    Wrong line:   Correction:
  4. 1  L = [10, 20, 30]
    2  print(L[3])
    Wrong line:   Correction:
  5. 1  t = (5)
    2  print(type(t))
    3  # Expected: tuple, got int
    Wrong line:   Correction:
  6. 1  d = {"a": 1, "b": 2}
    2  print(d["c"])
    Wrong line:   Correction:

Section D — Short Answer (2 × 5 = 10)

  1. Differentiate between mutable and immutable data types with one example each.
  2. Write any three differences between a list and a tuple.
  3. What is the difference between / and //? Show the result of 17/5 and 17//5.
  4. Why must a dictionary key be immutable? Give one valid and one invalid key example.
  5. What does the type() function do? List the data types of these literals: 5, 5.0, "5", [5], (5,).

Chapter 9 Worksheet — (continued)

Sheet 2 (Back)

Section E — Code Tasks (16 marks)

Each program should produce a clear output. Use a separate sheet if you need more space.

  1. (2) Create a list of 5 favourite subjects, then print the first, the last and the total number of items in the list.
  2. (3) Create a tuple 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.
  3. (3) Create a dictionary student with keys name, roll, marks. Then update the marks, add a new key grade, and print the final dictionary.
  4. (3) Read a string from the user and print: (a) its length, (b) the first three characters, (c) the string repeated 3 times, (d) the string reversed.
  5. (3) Take a list 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).
  6. (2) Demonstrate the difference between mutable and immutable types: take a list 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.