Circle of Learning Logo CIRCLE OF LEARNING Excellence in Education
CBSE Class 12 · Computer Science Chapter 1 Session 2026-27

Review of Python Basics & Core Data Structures Formulas & Cheat Sheet

Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.

Core Concepts & Theoretical Foundations

  • Mutability vs Immutability: Immutable types (int, float, bool, string, tuple) cannot be altered in-place; any modification creates a new object in memory. Mutable types (list, dict, set) allow in-place modification of elements.
  • String & List Slicing: General syntax `seq[start : stop : step]`. Indexing starts at 0 (positive) or -1 (negative/reverse). Stop boundary is strictly EXCLUSIVE.
  • Dictionary Mechanics: Stores key-value mappings. Keys must be of an immutable hashable data type (int, string, tuple). Values can be any arbitrary data type.
  • List Comprehensions: Compact concise syntax `[expression for item in iterable if condition]` providing high readability and fast execution.

Chapter Formulas & Equations (10 entries)

# Topic / Formula Name Mathematical Form & Equation Conditions, Meaning & Board Tips
1 General Sequence Slicing Rule
Slice[i:j:k]  ⟹  {Xm∣m=i+n⋅k,i≤m<j}\text{Slice}[i:j:k] \implies \{ X_m \mid m = i + n \cdot k, \quad i \le m < j \}

If step is negative, slice traverses backwards and start must be greater than stop (e.g. s[::-1] reverses string s).

2 String Inversion (Reverse Slicing)
Srev=S[::−1]S_{\text{rev}} = S[::-1]

Idiomatic Python technique to reverse strings or tuples in O(N) time complexity.

3 List In-Place Append vs Extend
L.append([1,2])  ⟹  len+1;L.extend([1,2])  ⟹  len+2L.\text{append}([1, 2]) \implies \text{len} + 1; \quad L.\text{extend}([1, 2]) \implies \text{len} + 2

Append nests the container; extend unpacks and concatenates the elements.

4 Dictionary Safe Value Access
D.get(k,d)={D[k]k∈Ddk∉DD.\text{get}(k, d) = \begin{cases} D[k] & k \in D \\ d & k \notin D \end{cases}

Retrieves value associated with key without raising a KeyError if the key is missing.

5 Floor Division and Modulo Identity
a=(a//b)⋅b+(a(modb))a = (a // b) \cdot b + (a \pmod b)

Python integer division rounds down toward negative infinity; modulo result always has the same sign as divisor b.

6 Operator Precedence Hierarchy (Highest to Lowest)
()>∗∗>unary>{∗,/,//,%}>{+,−}>comparison>logical() > ** > \text{unary} > \{*, /, //, \%\} > \{+, -\} > \text{comparison} > \text{logical}

Strict evaluation sequence governing Python expression resolution without explicit parentheses.

7 List Comprehension Formula
L={f(x)∣x∈Iterable∧P(x)=True}L = \{ f(x) \mid x \in \text{Iterable} \land P(x) = \text{True} \}

Generates a new filtered and mapped list in a single memory-optimized line.

8 Membership Complexity in Containers
Tlist=O(n);Tset=O(1)T_{\text{list}} = O(n); \quad T_{\text{set}} = O(1)

Sets and dictionaries use hash tables, making membership testing instantaneous regardless of size.

9 Truthy vs Falsy Value Evaluation
bool(x)=False  ⟺  x∈{0,0.0,None,∅}\text{bool}(x) = \text{False} \iff x \in \{0, 0.0, \text{None}, \emptyset\}

Used extensively in conditional `if x:` checks.

10 Swapping Two Variables Without Temp Variable
(a,b)←(b,a)(a, b) \leftarrow (b, a)

Evaluates right-hand tuple first, then unpacks into left-hand identifiers simultaneously.

Step-by-Step Worked Board Exam Examples

Example 1: Predict the output of the following Python code snippet: s = 'ComputerScience' print(s[1:8:2]) print(s[::-3])

1. For s[1:8:2]:

Characters from index 1 to 7 with step 2: indices 1, 3, 5, 7.

Index 1 = 'o', Index 3 = 'p', Index 5 = 't', Index 7 = 'r'. Output: 'optr'.

2. For s[::-3]:

Reverses string starting from last character (index -1) in steps of 3.

Index -1 ('e'), Index -4 ('i'), Index -7 ('S'), Index -10 ('u'), Index -13 ('m').

String output: 'eiSum'.

Ans: Output: optr eiSum

Example 2: What will be the content of list `L` after executing: L = [10, 20, 30] L.append([40, 50]) L.extend([60, 70]) print(len(L))?

1. L begins as [10, 20, 30] (len = 3)

2. L.append([40, 50]) adds the list [40, 50] as a single nested element -> L = [10, 20, 30, [40, 50]] (len = 4)

3. L.extend([60, 70]) unpacks elements 60 and 70 -> L = [10, 20, 30, [40, 50], 60, 70] (len = 6)

4. len(L) evaluates to 6.

Ans: L = [10, 20, 30, [40, 50], 60, 70]; len(L) = 6.

Common Mistakes to Avoid in Exam

  • Assuming tuples are mutable: Statements like `t = (1, 2); t[0] = 9` raise `TypeError: 'tuple' object does not support item assignment`.
  • Using mutable objects as dictionary keys: Lists and dictionaries cannot be used as dictionary keys because they are unhashable (`TypeError: unhashable type: 'list'`). Use tuples instead.
  • Confusing `is` with `==`: `==` checks value equality; `is` checks identity (whether both variables point to the exact same memory address `id(a) == id(b)`).
  • Forgetting that slice upper boundary is exclusive: `s[0:4]` extracts indices 0, 1, 2, 3 (4 characters), NEVER index 4.

Target Practice Questions

Q1. Write a list comprehension to generate squares of all even numbers from 1 to 20.

Correct Answer: `[x**2 for x in range(1, 21) if x % 2 == 0]`

Q2. What will be the output of `bool('')`, `bool([])`, and `bool('0')` in Python?

Correct Answer: `bool('')` -> False; `bool([])` -> False; `bool('0')` -> True (non-empty string is truthy).

Q3. Give the output of: `x = (10); y = (10,); print(type(x), type(y))`.

Correct Answer: `<class 'int'> <class 'tuple'>` (A single-item tuple must have a trailing comma).

Frequently Asked Questions

Q. What is the difference between mutable and immutable data types in Python?

Mutable data types (lists, dictionaries, sets) allow elements to be modified, appended, or deleted in place without altering the memory identity (id) of the object. Immutable data types (integers, floats, booleans, strings, tuples) cannot be altered once created; any modification creates a new object in memory.

Q. How does floor division `//` differ from standard division `/`?

Standard division `/` always returns a floating-point number (e.g. `7 / 2 = 3.5`). Floor division `//` discards the fractional part and rounds down to the nearest integer toward negative infinity (e.g. `7 // 2 = 3`, but `-7 // 2 = -4`).

Q. What is the role of the `dict.keys()`, `dict.values()`, and `dict.items()` methods?

`keys()` returns a view of all keys; `values()` returns a view of all values; `items()` returns a view of `(key, value)` tuple pairs, commonly used in `for k, v in d.items():` loops.

Q. Why is a set faster than a list for checking membership?

A set is implemented as a hash table with average lookup complexity of O(1) constant time, whereas a list requires an O(n) sequential linear search.

Q. What does the expression `eval()` do in Python?

`eval(string)` parses and evaluates a valid Python expression represented as a string and returns the computed result (e.g. `eval('5 + 3 * 2')` returns 11).

← View All 11 Chapters in Class 12 Computer Science Formula Book