Python Programming Fundamentals and Control Structures Formulas & Cheat Sheet
Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.
Core Concepts & Theoretical Foundations
- Python Features: High-level, interpreted, dynamically typed, case-sensitive language with clean indentation syntax.
- Tokens in Python: Keywords (reserved words, e.g. `if`, `for`, `def`, `class`), Identifiers (naming rules: letters, digits, underscore; cannot start with digit), Literals, Operators, Delimiters.
- Data Types: Integer, Float, Complex, Boolean (`True`, `False`), String (immutable), List (mutable ordered sequence `[...]`), Tuple (immutable ordered sequence `(...)`), Dictionary (mutable key-value pairs `{k: v}`).
- Operators: Arithmetic (`+`, `-`, `*`, `/`, `//` floor division, `%` modulo, `**` exponent), Relational (`==`, `!=`, `<`, `>`, `<=`, `>=`), Logical (`and`, `or`, `not`), Membership (`in`, `not in`), Identity (`is`, `is not`).
- Control Flow: Conditional statements (`if-elif-else`), Loops (`for` with `range(start, stop, step)`, `while` condition), Loop control (`break`, `continue`).
- String and List Slicing: Syntax `sequence[start : stop : step]` where stop index is strictly exclusive.
Chapter Formulas & Equations (12 entries)
| # | Topic / Formula Name | Mathematical Form & Equation | Conditions, Meaning & Board Tips |
|---|---|---|---|
| 1 | Floor Division vs True Division Rule |
|
In Python, true division always yields float; floor division rounds down towards negative infinity. |
| 2 | Modulo Remainder Identity |
|
The sign of the result matches the sign of divisor b in Python. |
| 3 | Sequence Slicing Formula |
|
If step is negative, slice traverses backwards from start down to stop + 1. |
| 4 | Reverse Sequence Slicing |
|
Concise idiomatic Python syntax to reverse any indexable sequence. |
| 5 | Range Function Generation Rule |
|
Stop argument b is strictly exclusive; default start is 0, default step is 1. |
| 6 | Operator Precedence Hierarchy |
|
Logical 'not' has higher precedence than 'and', which has higher precedence than 'or'. |
| 7 | Short-Circuit Logical Evaluation |
|
Python stops evaluating logical expressions as soon as the outcome is deterministic. |
| 8 | List Mutability vs String Immutability |
|
Immutable objects cannot have their contents altered in memory once created. |
| 9 | String Repetition and Concatenation |
|
Valid only between compatible sequence types (cannot concatenate str with int). |
| 10 | Membership Operator Rule |
|
For dictionaries, 'x in dict' checks whether x is present among the dictionary KEYS. |
| 11 | Negative Indexing Mapping |
|
Allows direct access from the end of a sequence (s[-1] is the last item). |
| 12 | Dictionary Key Uniqueness & Hashing |
|
Keys are mapped using hash tables for O(1) average lookup performance. |
Key Scientific Diagrams & Concept Schematics: Python Programming Fundamentals and Control Structures
Visual Reference: python-flowchart
Official NCERT board curriculum concept schematic and mathematical/scientific reference diagram.
CBSE Board Exam Tips & Frequent Pitfalls
- In Python slicing s[start:stop], remember that the item at index 'stop' is NEVER included (it stops at stop - 1).
- Do not confuse = (assignment) with == (relational equality check).
- Dictionary membership check 'x in d' checks whether x is a KEY in dictionary d, NOT a value.
Step-by-Step Worked Board Exam Examples
Example 1: What is the output of the following Python expression: 15 // 4 + 15 % 4 * 2 ** 3?
Step 1: Check operator precedence: Exponentiation (**) has highest precedence.
Step 2: 2 ** 3 = 8.
Step 3: Expression becomes: 15 // 4 + 15 % 4 * 8.
Step 4: Next precedence: Floor division (//), modulo (%), and multiplication (*) from left to right.
Step 5: 15 // 4 = 3 (since 4 goes into 15 three times, remainder 3).
Step 6: 15 % 4 = 3.
Step 7: 3 * 8 = 24.
Step 8: Final addition: 3 + 24 = 27.
Example 2: Given s = 'INFORMATICS', evaluate the slices: (a) s[2:7], (b) s[::3], (c) s[::-2].
Step 1: Write index mapping for 'INFORMATICS' (length 11): 0:I, 1:N, 2:F, 3:O, 4:R, 5:M, 6:A, 7:T, 8:I, 9:C, 10:S.
Step 2: Slice s[2:7] extracts indices 2, 3, 4, 5, 6 -> 'FORMA'.
Step 3: Slice s[::3] starts at 0 with step 3: indices 0, 3, 6, 9 -> 'I', 'O', 'A', 'C' -> 'IOAC'.
Step 4: Slice s[::-2] traverses backward from last index 10 in steps of -2: indices 10, 8, 6, 4, 2, 0 -> 'S', 'I', 'A', 'R', 'F', 'I' -> 'SIARFI'.
Common Mistakes to Avoid in Exam
- Thinking range(1, 5) generates numbers 1, 2, 3, 4, 5; it stops at 4 (generates 1, 2, 3, 4).
- Attempting to modify a string character in-place: s[0] = 'A' raises a TypeError because strings are immutable.
- Assuming 7 / 2 returns an integer in Python 3; true division `/` always returns float 3.5 (use `//` for integer 3).
Target Practice Questions
Q1. What will be the output of: print('Hello' * 3)?
Correct Answer: 'HelloHelloHello'.
Q2. Evaluate: print(bool(0), bool('False'), bool([])) in Python.
Correct Answer: False True False (Any non-empty string evaluates to True).
Q3. Write the output of: for i in range(10, 0, -3): print(i, end=' ').
Correct Answer: 10 7 4 1.
Frequently Asked Questions
Q. What is the difference between mutable and immutable data types in Python?
Mutable data types (such as lists, dictionaries, and sets) allow their values to be changed, added, or deleted in place without changing their memory address. Immutable data types (such as integers, floats, strings, and tuples) cannot be altered once created; any modification creates a new object in memory.
Q. How does the `range()` function work in Python?
`range(start, stop, step)` generates an immutable arithmetic sequence of integers starting from `start`, incrementing by `step`, up to but excluding `stop`. If `start` is omitted, it defaults to 0; if `step` is omitted, it defaults to 1.
Q. What is the difference between `break` and `continue` statements?
`break` terminates the current loop immediately and transfers program control to the statement following the loop. `continue` skips the remainder of the current loop iteration and jumps immediately to the next iteration.
Q. What is short-circuit evaluation in logical expressions?
In Python, logical operators `and` and `or` perform short-circuit evaluation: In `A and B`, if A is False, the entire expression is False and B is not evaluated. In `A or B`, if A is True, the expression is True and B is skipped.