💻
Chapter 1 · Class 12 Computer Science
Python Revision Tour
1 exercises3 questions solved
Exercise 1.1Python Revision Tour — Data Types, Operators, and Control Flow
Q1
What are the built-in data types in Python? Explain mutable and immutable types with examples.
Solution
Python's Built-in Data Types:
Python is a dynamically typed language — variable types are inferred at runtime.
Numeric Types:
• int: Integer values — e.g., x = 10, y = -5
• float: Floating-point numbers — e.g., pi = 3.14
• complex: Complex numbers — e.g., z = 3 + 4j
• bool: Boolean — True or False (subclass of int; True == 1, False == 0)
Sequence Types:
• str: Immutable sequence of characters — e.g., name = 'Arjun'
• list: Mutable ordered sequence — e.g., marks = [85, 90, 78]
• tuple: Immutable ordered sequence — e.g., coordinates = (10, 20)
Set Types:
• set: Mutable, unordered, no duplicates — e.g., s = {1, 2, 3}
• frozenset: Immutable version of set.
Mapping Type:
• dict: Key-value pairs — e.g., student = {'name': 'Priya', 'age': 17}
Special Type:
• NoneType: Represents absence of value — None.
Mutable vs. Immutable:
Immutable types (cannot be changed after creation):
• int, float, complex, bool, str, tuple, frozenset
• Example: s = 'hello'; s[0] = 'H' ← raises TypeError
• When you 'change' an immutable variable, Python creates a new object.
Mutable types (can be changed in place):
• list, dict, set
• Example:
lst = [1, 2, 3]
lst[0] = 10 ← modifies in place; lst is now [10, 2, 3]
Why it matters:
• Immutable objects are hashable → can be used as dictionary keys or set members.
• Mutable objects passed to functions can be modified inside the function (side effects).
• Immutable objects are safer for concurrent programming.
Q2
Explain the different types of operators in Python with examples. What is operator precedence?
Solution
Python Operators:
1. Arithmetic Operators:
• + (add), - (subtract), * (multiply), / (true divide), // (floor divide), % (modulo), ** (exponentiation)
• Example: 17 // 5 = 3; 17 % 5 = 2; 2 ** 8 = 256
2. Comparison / Relational Operators:
• == (equal), != (not equal), > (greater), < (less), >= (greater or equal), <= (less or equal)
• Return bool: True or False
• Example: 10 > 5 → True; 'a' == 'b' → False
3. Assignment Operators:
• = (assign), += (add and assign), -= (subtract and assign), *= (multiply and assign), //= (floor divide and assign)
• Example: x = 5; x += 3 is equivalent to x = x + 3 → x becomes 8
4. Logical Operators:
• and, or, not
• and: True if both operands are True
• or: True if at least one operand is True
• not: Reverses the boolean value
• Example: (5 > 3) and (10 > 7) → True; not True → False
5. Bitwise Operators:
• & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift)
• Operate on individual bits of integer operands.
• Example: 5 & 3 → 1 (binary: 0101 & 0011 = 0001)
6. Membership Operators:
• in, not in — test if a value is present in a sequence.
• Example: 3 in [1, 2, 3] → True; 'a' not in 'hello' → True
7. Identity Operators:
• is, is not — test if two variables refer to the same object in memory.
• Example: x = [1, 2]; y = x; x is y → True (same object)
Operator Precedence (high to low):
1. ** (exponentiation)
2. ~ + - (unary operators)
3. * / // %
4. + - (addition, subtraction)
5. << >> (bitwise shift)
6. & (bitwise AND)
7. ^ (bitwise XOR)
8. | (bitwise OR)
9. Comparison: == != > < >= <=
10. not
11. and
12. or
Example: 2 + 3 * 4 ** 2 = 2 + 3 * 16 = 2 + 48 = 50 (** first, then *, then +)
Q3
Explain list, tuple, and dictionary in Python. How do you access, update, and delete elements in each?
Solution
List:
• Ordered, mutable, allows duplicates.
• Syntax: lst = [10, 20, 30, 'hello']
Access: lst[0] → 10; lst[-1] → 'hello'; lst[1:3] → [20, 30] (slicing)
Update: lst[0] = 99 → lst becomes [99, 20, 30, 'hello']
Delete: del lst[1] removes index 1; lst.remove(30) removes by value; lst.pop() removes last element.
Useful methods: append(), extend(), insert(), sort(), reverse(), index(), count(), len()
Tuple:
• Ordered, immutable, allows duplicates.
• Syntax: tup = (10, 20, 30, 10)
Access: tup[1] → 20; tup[-1] → 10; tup[0:2] → (10, 20)
Update: Not allowed — tuples are immutable. You must create a new tuple.
Delete: Cannot delete individual elements. del tup deletes the entire tuple variable.
Useful methods: count(), index(), len()
Note: A single-element tuple needs a trailing comma: t = (5,)
Dictionary:
• Unordered (Python 3.7+ maintains insertion order), mutable, key-value pairs, keys must be unique and immutable.
• Syntax: d = {'name': 'Arjun', 'age': 17, 'marks': 95}
Access: d['name'] → 'Arjun'; d.get('age') → 17 (safer — returns None if key absent)
Update: d['age'] = 18 → updates existing key; d['city'] = 'Delhi' → adds new key.
Delete: del d['marks'] removes the key; d.pop('age') removes and returns the value.
Useful methods: keys(), values(), items(), update(), clear(), len()
Key differences:
• List: mutable ordered sequence — use when you need a changeable ordered collection.
• Tuple: immutable ordered sequence — use when data should not change (e.g., coordinates, RGB values).
• Dictionary: key-value mapping — use when you need to look up values by a meaningful key.
More chapters
← All chapters: Class 12 Computer Science