Python Basics & Tokens Formulas & Cheat Sheet
Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.
Core Concepts & Theoretical Foundations
- Features of Python: High-level, interpreted, dynamically typed, case-sensitive, object-oriented language created by Guido van Rossum (1991).
- Tokens (Lexical Units): Smallest individual unit in a Python program. Five types: 1. Keywords; 2. Identifiers; 3. Literals; 4. Operators; 5. Punctuators / Delimiters.
- Keywords: Reserved words having special meaning (e.g. False, None, True, and, as, assert, break, class, def, if, import, in, is, lambda, pass, return). Note: True, False, and None start with capital letters.
- Identifier Rules: Can be a combination of letters, digits, and underscore (_); must NOT start with a digit; cannot be a keyword; no special characters allowed (e.g. $, @, -); case-sensitive (Total != total).
- Data Types in Python: Numbers (int, float, complex: a + bj), Boolean (True, False), NoneType, Sequence (str, list, tuple), Mapping (dict), Sets (set).
- Mutable vs Immutable: Immutable (value cannot be changed in-place; creates new object): int, float, bool, str, tuple. Mutable (value can be modified in-place): list, dict, set.
- Operators in Python: Arithmetic (+, -, *, / float division, // floor division, % modulus, ** exponentiation); Relational (==, !=, >, <, >=, <=); Logical (and, or, not); Bitwise (&, |, ^, ~, <<, >>); Assignment (=, +=, etc.); Identity (is, is not); Membership (in, not in).
- Type Casting: Implicit Type Conversion (Python automatically coerces narrower type into wider type, e.g. int + float -> float); Explicit Type Conversion (User forces type conversion using constructors: int(), float(), str(), list(), tuple()).
Chapter Formulas & Equations (10 entries)
| # | Topic / Formula Name | Mathematical Form & Equation | Conditions, Meaning & Board Tips |
|---|---|---|---|
| 1 | Python Division Operator Disparity |
|
In Python, // rounds down to the nearest lesser integer, which is why -7 // 2 evaluates to -4, not -3. |
| 2 | Python Modulus Operator Rule |
|
Yields the remainder; with negative operands, sign matches the divisor (e.g. -7 % 2 = 1 because -7 - (2 * -4) = 1). |
| 3 | Operator Precedence Hierarchy |
|
PEMDAS-style order of operations; exponentiation (**) binds right-to-left (e.g. 2 ** 3 ** 2 = 2 ** 9 = 512). |
| 4 | Exponentiation Right-Associative Rule |
|
Unlike all other binary arithmetic operators that evaluate left-to-right, exponentiation associates right-to-left. |
| 5 | Identity vs Equality Operators |
|
Two distinct objects can have identical values (== is True), but reside at different memory addresses (is is False). |
| 6 | Short-Circuit Logical Evaluation |
|
Python stops evaluating compound Boolean expressions as soon as the definitive outcome is established. |
| 7 | Complex Number Syntax & Attributes |
|
Python uses j or J as the imaginary unit sqrt(-1); both real and imaginary components are stored as floats. |
| 8 | Bitwise Shift Multiplication/Division |
|
Bitwise shifts provide rapid power-of-two multiplication and integer division at the hardware level. |
| 9 | Dynamic Typing Re-Binding |
|
Python variables do not possess fixed datatypes; variables are simply reference pointers to dynamic heap objects. |
| 10 | String Repetition & Concatenation |
|
Operator overloading allows * and + to manipulate sequences, while type safety prevents adding mismatched types. |
CBSE Board Exam Tips & Frequent Pitfalls
- In questions evaluating expressions, remember that // performs floor division (rounds down towards negative infinity): -5 // 2 is -3, NOT -2.
- Remember that True, False, and None MUST be capitalized in Python.
- Check invalid variable names: 2var (starts with digit - invalid), my-var (contains hyphen - invalid), for (keyword - invalid), my_var (valid).
Step-by-Step Worked Board Exam Examples
Example 1: Evaluate the Python expression: 12 + 3 * 4 ** 2 // 8 - 5.
Step 1 (Exponentiation **): 4 ** 2 = 16. Expression: 12 + 3 * 16 // 8 - 5.
Step 2 (Multiplication * and Floor Division // left-to-right):
3 * 16 = 48.
48 // 8 = 6. Expression: 12 + 6 - 5.
Step 3 (Addition and Subtraction left-to-right):
12 + 6 = 18.
18 - 5 = 13.
Example 2: What is the difference between mutable and immutable data types? Give two examples of each.
Immutable data types: Objects whose values cannot be changed in-place once created. Modifying an immutable variable creates a completely new object in memory with a new id() address. Examples: integers (int), strings (str), tuples (tuple).
Mutable data types: Objects whose internal contents or elements can be modified in-place without altering the object's identity or memory address (id()). Examples: lists (list), dictionaries (dict), sets (set).
Common Mistakes to Avoid in Exam
- Writing that // truncates the decimal part. It does NOT truncate; it calculates the mathematical FLOOR (e.g. -5 // 2 is -3, not -2).
- Believing a = b = [1, 2] creates two separate lists. It creates one list object with two variable references; modifying a[0] changes b[0] too!
- Confusing is with ==. == compares values; is checks whether both references point to the identical memory address.
Target Practice Questions
Q1. Identify the invalid variable names: total_sum, 1st_rank, def, my-name, _count.
Correct Answer: 1st_rank (starts with digit), def (keyword), and my-name (hyphen not allowed) are invalid.
Q2. What is the output of print(2 ** 2 ** 3) in Python?
Correct Answer: 2 ** (2 ** 3) = 2 ** 8 = 256 (due to right-associativity of **).
Q3. Which data types in Python are mutable?
Correct Answer: Lists, Dictionaries, and Sets are mutable.
Frequently Asked Questions
Q. What is an identifier in Python?
A user-defined name given to a variable, function, class, or module that follows specific naming rules.
Q. What is a dynamic type system in Python?
A variable does not need a declared type; its type is determined automatically at runtime based on the value currently assigned to it.
Q. What is the output of bool(0) and bool('') in Python?
Both evaluate to False, because numeric zero and empty sequences are falsy in Python.
Q. What does the id() function return in Python?
The id() function returns the unique identity (memory address) of the object.