Class XI · Chapter 5Unit 2, Python Programming (45 marks)6 min read
Chapter 5: Getting Started with Python
CBSE Unit: Unit 2, Python Programming (45 marks) Marks Weightage: ~8-10 marks (foundational Python, heavily tested) Priority: CRITICAL, basics of Python, every concept tested
Key Concepts
5.1 Features of Python
- High-level, free, and open source language
- Interpreted (executed line by line by interpreter)
- Case-sensitive (NUMBER and number are different)
- Portable and platform independent (runs on any OS), Rich library of predefined functions
- Uses indentation for blocks (not braces like C/Java)
5.2 Execution Modes
- Interactive mode: Type on
>>>prompt, executes immediately; cannot save for reuse - Script mode: Write in a file (.py), save and execute; reusable
5.3 Python Keywords (Reserved Words)
Cannot be used as identifiers. Examples:
False, True, None, and, or, not, if, elif, else, for, while, break, continue, pass, def, return, class, import, from, as, try, except, finally, raise, with, yield, lambda, global, nonlocal, del, in, is, assert
5.4 Identifiers, Names given to variables, functions, classes, etc., Rules:
- Must start with letter (A-Z/a-z) or underscore (_)
- Can contain letters, digits (0-9), and underscores
- Cannot start with a digit
- Cannot be a keyword
- Case-sensitive
- No special characters (!, @, #, $, %, etc.) except underscore
5.5 Comments
- Single-line:
# This is a comment - Multi-line: Use
#on each line OR triple quotes ('''comment'''or"""comment"""), Comments are ignored by interpreter
5.6 Data Types
| Type | Description | Example |
|---|---|---|
int |
Integer (no size limit in Python 3) | 25, -10, 0 |
float |
Decimal numbers | 3.14, -2.5, 1.0 |
complex |
Complex numbers | 3+5j |
bool |
Boolean (True/False) | True, False |
str |
String (sequence of characters) | 'Hello', "World" |
list |
Ordered, mutable sequence | [1, 2, 3] |
tuple |
Ordered, immutable sequence | (1, 2, 3) |
dict |
Key-value pairs | {'a': 1, 'b': 2} |
None |
Null/absence of value | None |
- Mutable: Can be changed after creation (list, dict)
- Immutable: Cannot be changed (int, float, str, tuple, bool)
type()function returns data type of a variable
5.7 Variables, Name that refers to a value stored in memory, Created by assignment statement: x = 10
- Dynamic typing: type determined at runtime (no declaration needed), Multiple assignment:
a, b, c = 10, 20, 30ora = b = c = 0
5.8 Operators
Arithmetic Operators
| Operator | Operation | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division (float) | 7 / 2 |
3.5 |
// |
Floor Division (integer) | 7 // 2 |
3 |
% |
Modulus (remainder) | 7 % 2 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
Relational (Comparison) Operators
==, !=, <, >, <=, >= - return True or False
Logical Operators
| Operator | Description |
|---|---|
and |
True if BOTH are True |
or |
True if at least ONE is True |
not |
Reverses the boolean value |
Assignment Operators
=, +=, -=, *=, /=, //=, %=, **=
Identity Operators
is, is not - check if two variables refer to same object
Membership Operators
in, not in - check if value exists in sequence
Operator Precedence (High to Low)
** > ~, +, - (unary) > *, /, //, % > +, - > relational > not > and > or
5.9 Expressions, Combination of values, variables, operators that evaluates to a value
- Arithmetic expression:
a + b * c - Relational expression:
a > b - Logical expression:
a > b and c < d
5.10 Input and Output
Input
name = input("Enter your name: ") # Always returns STRING
num = int(input("Enter number: ")) # Convert to int
price = float(input("Enter price: ")) # Convert to float
input()always returns a string, must typecast for numeric operations
Output
print("Hello", "World") # Separated by space (default sep=' ')
print("Hello", end="") # No newline at end (default end='\n')
print("Name:", name, "Age:", age) # Multiple values
print("Sum =", a + b) # Expression in print
5.11 Type Conversion
Implicit (Automatic), Python automatically converts smaller type to larger: int + float = float
Explicit (Type Casting)
int() # Convert to integer: int(3.7) = 3, int("25") = 25
float() # Convert to float: float(5) = 5.0, float("3.5") = 3.5
str() # Convert to string: str(25) = "25"
bool() # Convert to boolean: bool(0) = False, bool(1) = True
5.12 Debugging
- Syntax Error: Violation of language rules (detected before execution)
- Logical Error: Program runs but gives wrong output
- Runtime Error: Error during execution (e.g., division by zero, wrong type)
Common Board Exam Question Patterns
- Find output of given Python code (2-4 marks): Most common question type
- Identify errors in code (1-2 marks)
- Data types and
type()function (1-2 marks) - Operator precedence questions (2 marks)
- Difference between
/and//,=and==,isand==(2 marks) - input() and type conversion (2 marks)
- Keywords vs Identifiers (1-2 marks)
- Write a Python program for simple calculation (3-4 marks)
Key Points Students Miss
input()ALWAYS returns a string, must convert for arithmetic/gives float result (7/2 = 3.5);//gives integer floor division (7//2 = 3)**has right-to-left associativity:2**3**2 = 2**(3**2) = 2**9 = 512- Python is case-sensitive:
Trueis boolean,trueis an error - Indentation is mandatory in Python (not optional like in C/Java)
bool(0)= False,bool("")= False,bool([])= False; any non-zero/non-empty = Trueint(3.7)= 3 (truncates, does NOT round)Noneis a valid value and data type (NoneType)- Multiple assignment:
a, b = b, aswaps values without temp variable - Comments are ignored by interpreter but essential for code readability
Prefer watching over reading?
Subscribe for free.