Class 12 IP – Unit 1: Python Revision Tour (FULL Detailed
Notes)
This document explains every heading from your syllabus (1.1 → 1.13) with plain-language concepts,
board-style definitions, examples, and code outputs. The focus is on WHY a concept works, not just
syntax.
1.1 Introduction
Python is a high-level, interpreted language. Interpreted means your code is executed line-by-line by
the Python interpreter. You don’t compile to machine code yourself; Python translates it at runtime.
This makes development fast and interactive.
Key features: simple readable syntax, huge standard library, runs on all OS, supports multiple styles
(procedural, object-oriented, functional). Indentation (spaces) defines blocks—no braces.
Exam line: "Python is a high-level, interpreted, general-purpose programming language created by
Guido van Rossum (1991)."
1.2 Tokens in Python
Tokens are the smallest building blocks the interpreter understands—like words and punctuation in
English. Categories: keywords, identifiers, literals, operators, and punctuators.
1.2.1 Keywords
Keywords are reserved words with special meaning. You cannot use them as variable names.
Examples: False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except,
finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with,
yield.
1.2.2 Identifiers (Names)
Identifiers are names you give to variables, functions, classes etc.
• Can contain letters (A–Z, a–z), digits (0–9), and underscore (_).
• Cannot start with a digit.
• Case-sensitive: Score and score are different.
• No spaces or special characters like @, $, %.
valid_name = 10
_name2 = 'ok'
# invalid: 2cool = 5 | total% = 99 | my name = 'Aman'
1.2.3 Literals / Values
Literals are fixed values written directly in the code.
• Numeric: integers (10, -3), floats (3.14), complex (3+2j).
• String: text enclosed in quotes—single (' '), double (" "), or triple for multi-line.
• Boolean: True or False used in conditions.
• Special: None represents 'no value' / null.
a = 10 # int
b = 3.5 # float
c = 2+4j # complex
name = "Nikunj" # string
flag = True # boolean
x = None # special literal
1.2.4 Operators (with Concepts)
Operators perform operations on operands. Python follows precedence (PEMDAS-like); use
parentheses to control order.
• Arithmetic: +, -, *, / (true division), // (floor division), % (remainder), ** (power). Concept: /
produces float; // rounds down to the nearest integer result.
• Relational: ==, !=, <, >, <=, >= → produce booleans.
• Logical: and, or, not → combine conditions using short-circuiting (Python stops evaluating as
soon as result is known).
• Assignment: = (plus augmented: +=, -=, *=, //= ...).
• Membership: in, not in → checks presence in a sequence.
• Identity: is, is not → checks whether two names point to the same object in memory.
5/2 # 2.5 (true division)
5//2 # 2 (floor)
-5//2 # -3 (floors toward -infinity)
'a' in 'data' # True
x = [1,2,3]; y = x; (x is y) # True
1.2.5 Punctuators
Symbols that structure code: parentheses (), brackets [], braces {}, comma ',', colon ':', dot '.', hash
'#'(comment), quotes, and backslash for line continuation. In Python, ':' starts an indented block (after
if/for/def/class).
1.3 Barebones of a Python Program
A minimal program includes statements and blocks defined by indentation. Comments (starting with
#) are ignored by the interpreter. Docstrings (triple quotes directly under def/class) document code.
# simple program
# 1) input → 2) processing → 3) output
name = input('Enter name: ')
msg = f'Hello, {name}!'
print(msg)
Why indentation matters: a block is a group of statements executed together. All lines in a block must
have the same indentation level.
1.4 Variables and Assignments
Variable is a name bound to an object in memory. Python uses dynamic typing: a name can be
rebound to different types at runtime.
1.4.1 Dynamic Typing (Concept)
The name stores a reference to an object, not a fixed type. Reassigning changes the reference, not
the old object.
x = 10 # x refers to int object 10
x = 'ten' # now x refers to a string object
1.4.2 Multiple Assignments & Unpacking
You can assign multiple names at once or unpack a sequence into variables.
a = b = 0 # chain assignment (both refer to same 0)
x, y, z = 1, 2, 3
x, y = y, x # swap without temp
name, *rest = ['CBSE','IP',12] # extended unpacking
1.5 Simple Input and Output
input() reads text from the user and returns a string. Convert it if you need a number. print() outputs
values, separated by sep and ending with end (default '\n').
age = int(input('Enter age: '))
print('Next year you will be', age+1)
print('A','B','C', sep='-', end='!') # A-B-C!
1.6 Data Types
Built-ins used in Class 12: int, float, bool, str, NoneType, and collections: list, tuple, set, dict.
Strings, lists and tuples are sequences (ordered, indexable).
type(10) # int
type(3.0) # float
type(True) # bool
type('IP') # str
type([1,2,3]) # list
type((1,2,3)) # tuple
type({'a':1}) # dict
1.7 Mutable and Immutable Types
Immutable objects cannot change their contents after creation (int, float, str, tuple). Mutable objects
can be changed in place (list, dict, set).
Why it matters: operations on immutable objects create new objects; on mutable objects they modify
the same object.
s = 'abc'
s += 'd' # creates a NEW string
L = [1,2]
L.append(3) # modifies the SAME list
Exam trick: id() gives the memory identity. After modifying a list, id(L) stays same; after modifying a
string, id(s) changes.
1.8 Expressions
An expression combines values, variables and operators to produce another value. Python follows
precedence; parentheses make order explicit.
1.8.1 Evaluating Arithmetic Expressions
Key concepts: true division vs floor division, exponent precedence, modulo with negatives follows
floor rule.
5 + 3*2 # 11 (multiplication first)
(5 + 3)*2 # 16
2 ** 3 ** 2 # 2 ** (3 ** 2) = 512 (right-associative)
7 % 3 # 1
-7 % 3 # 2 (because -7 = 3*(-3) + 2)
1.8.2 Evaluating Relational Expressions
Relational operators compare values and return booleans. Strings compare lexicographically
(dictionary order by Unicode).
'apple' < 'banana' # True
10 >= 10 # True
3 != 4 # True
1.8.3 Evaluating Logical Expressions (Short-Circuit)
and returns the first falsey operand or the last value; or returns the first truthy operand. This is called
short-circuit evaluation.
0 and 5 # 0 (stops at first falsey)
7 or 0 # 7 (stops at first truthy)
not (3>2) # False
Use short-circuiting to avoid errors: x!=0 and (10/x>1) is safe even if x is 0.
1.8.4 Type Casting (Explicit Conversion)
Convert between types when required—especially after input().
int('123') # 123
float('3.5') # 3.5
str(25) # '25'
bool(0) # False, bool(5) → True
1.8.5 Math Library Functions
Import the math module to use common functions.
import math
math.sqrt(16) # 4.0
math.ceil(3.2) # 4
math.floor(3.8) # 3
math.pow(2,5) # 32.0
math.pi # 3.14159...
math.fabs(-7) # 7.0
1.9 Statement Flow Control
Program flow can be sequential (top to bottom), conditional (choose a branch), or iterative
(repeat). We control flow using if-statements and loops.
1.10 The if Conditionals
1.10.1 Plain if
x = 5
if x > 0:
print('Positive')
1.10.2 if–else
n = 7
if n % 2 == 0:
print('Even')
else:
print('Odd')
1.10.3 if–elif–else
m = 72
if m >= 90:
grade = 'A+'
elif m >= 75:
grade = 'A'
elif m >= 60:
grade = 'B'
else:
grade = 'C'
1.10.4 Nested if
x = 12
if x > 0:
if x % 2 == 0:
print('Positive Even')
else:
print('Positive Odd')
1.10.5 Storing Conditions
Store boolean expressions in variables to make code readable or to reuse the test.
in_range = (0 < x < 100) # chained comparison
if in_range:
print('Within 0..100')
Python supports chained comparisons like 0 < x < 100 which read naturally and are evaluated efficiently.
1.11 Looping Statements
1.11.1 for Loop (iterate over a sequence)
Use for to iterate over items of a sequence (string, list, range). range(start, stop, step) generates a
sequence of integers.
for i in range(1,6):
print(i) # 1 2 3 4 5
for ch in 'IP':
print(ch) # I then P
Use enumerate() to get index and value together.
for i, ch in enumerate('CBSE', start=1):
print(i, ch) # (1,C) (2,B) (3,S) (4,E)
1.11.2 while Loop (repeat while condition is true)
Use while when you don’t know the number of iterations in advance. Always update the loop variable
to avoid infinite loops.
count = 3
while count > 0:
print(count)
count -= 1
print('Go!')
1.12 Jump Statements — break and continue
break exits the nearest loop immediately. continue skips to the next iteration. They affect only the
innermost loop.
The break Statement
for i in range(10):
if i == 5:
break
print(i) # prints 0..4
The continue Statement
for i in range(5):
if i == 2:
continue
print(i) # prints 0,1,3,4
Use break when you have 'found it, stop now'. Use continue to 'skip this case but keep looping'.
1.13 More on Loops
1.13.1 Loop else Statement (Concept Many Students Miss)
The else part of a loop executes only if the loop wasn’t terminated by break. It is useful for search
problems: run else when you didn’t find the item.
for n in [2,3,5,7]:
if n == 4:
print('found')
break
else:
print('not found') # runs because loop ended normally
1.13.2 Nested Loops
A loop inside another loop. The inner loop completes all its iterations for each iteration of the outer
loop.
for i in range(1,4):
for j in range(1,3):
print(i, j)
# Pairs: (1,1) (1,2) (2,1) (2,2) (3,1) (3,2)
Board■Style Questions & Answers
Q1 (2 marks): Define dynamic typing in Python with an example.
Answer: In dynamic typing, a variable name is not bound to a fixed type; it simply refers to any
object. The type can change at runtime.
x = 5 # int
x = 'five' # now string
Q2 (3 marks): Differentiate between mutable and immutable with examples.
Answer: Immutable objects (int, float, str, tuple) cannot change in-place; operations create new
objects. Mutable objects (list, dict, set) can be modified without changing their identity.
s = 'ab'; s += 'c' # new string object
L = [1,2]; L.append(3) # same list modified
Q3 (3 marks): Explain short■circuit evaluation in logical operators with examples.
Answer: For and, Python stops at the first falsey operand; for or, it stops at the first truthy operand.
This prevents unnecessary computation and can avoid errors.
x = 0
x != 0 and (10/x > 1) # safe; RHS not evaluated
Q4 (4 marks): Write a program to read three numbers and print the largest using
if■elif■else.
a = int(input('A: '))
b = int(input('B: '))
c = int(input('C: '))
if a>=b and a>=c:
m = a
elif b>=a and b>=c:
m = b
else:
m = c
print('Largest =', m)
Q5 (5 marks): Explain the loop else construct with a real■world program.
Answer: Use it to search for an item; if not found, else runs:
rolls = [101,102,104]
target = 103
for r in rolls:
if r == target:
print('Present')
break
else:
print('Absent') # executes when target not found
High■Yield Exam Points (Remember!)
• input() returns a string. Convert with int()/float() for arithmetic.
• Use // for floor division; % remainder follows floor rule with negatives.
• Indentation defines blocks; a missing or inconsistent indent is a SyntaxError.
• Chained comparisons like 0 < x < 10 are valid and idiomatic.
• break skips the loop-else; continue does not.
• Strings are immutable—s[0] = 'A' is an error. Lists are mutable.