CSIP12.in
Back to List
Calculating...
UNIT 1 : CH 1 Dec 08, 2025

🐍 Python Revision Tour I

Welcome to the Class 12 Python Revision Tour! This unit recaps the fundamentals of Python you learned in Class 11.

## 1. Tokens in Python

A token is the smallest individual unit in a program. Python has the following tokens:

- **Keywords:** Reserved words with special meaning (e.g., `if`, `else`, `while`, `def`, `import`).
- **Identifiers:** Names given to variables, functions, etc. (e.g., `my_var`, `calculate_sum`).
- **Literals:** Constant values (e.g., `10`, `"Hello"`, `3.14`, `True`).
- **Operators:** Symbols that perform computations (e.g., `+`, `-`, `*`, `/`, `**`).
- **Punctuators:** Symbols used to organize code (e.g., `()`, `{}`, `[]`, `:`, `#`).

---

## 2. Variables and Assignments

Variables are containers for storing data values.

### Dynamic Typing

- Python is **dynamically typed** — variables can change type during execution.
- Use `type()` to check the data type of a variable.

### Multiple Assignments

- **Same value to multiple variables:** `a = b = c = 100`
- **Different values:** `x, y, z = 10, 20, 30`
- **Swapping:** `a, b = b, a`

### Code Example: Variables

```python
# Dynamic typing
x = 10 # Integer
x = "Hello" # Now a string
x = 3.14 # Now a float

# Multiple assignments
a, b, c = 5, 10, 15
print(a, b, c) # Output: 5 10 15

# Swapping
a, b = b, a
print(a, b) # Output: 10 5
```

---

## 3. Data Types

Python handles various types of data dynamically.

### Mutable vs Immutable

- **Immutable:** Value cannot be changed after creation.
- *Examples:* Integers, Floats, Strings, Tuples.
- **Mutable:** Value can be changed in place.
- *Examples:* Lists, Dictionaries, Sets.

### Code Example: Data Types

```python
# Integer
x = 10

# String
name = "Python"

# List (Mutable)
my_list = [1, 2, 3]
my_list[0] = 100 # Allowed

# Tuple (Immutable)
my_tuple = (1, 2, 3)
# my_tuple[0] = 100 # ERROR: TypeError
```

---

## 4. Input and Output

### Input: `input()` function

- Reads user input as a **string**.
- Type conversion needed for numbers: `int()`, `float()`.

### Output: `print()` function

- Displays output to the console.
- Use f-strings for formatted output: `f"Name: {name}"`

### Code Example: Input/Output

```python
# Input
name = input("Enter your name: ")
age = int(input("Enter your age: "))

# Output
print(f"Hello, {name}!")
print(f"You are {age} years old.")

# Controlling output format
print("A", "B", "C", sep="-") # Output: A-B-C
```

---

## 5. Operators and Expressions

### Arithmetic Operators

- `+` (Addition), `-` (Subtraction), `*` (Multiplication)
- `/` (Division), `//` (Floor Division), `%` (Modulus)
- `**` (Exponentiation)

### Relational Operators

- `==` (Equal), `!=` (Not Equal)
- `>`, `<`, `>=`, `<=`

### Logical Operators

- `and`, `or`, `not`

### Code Example: Operators

```python
# Arithmetic
a, b = 10, 3
print(a + b) # 13
print(a // b) # 3 (Floor division)
print(a ** b) # 1000 (Exponentiation)

# Relational
x, y = 5, 10
print(x < y) # True
print(x == y) # False

# Logical
age = 17
has_id = True
can_vote = (age >= 18) and has_id
print(can_vote) # False
```

---

## 6. Type Casting

Converting one data type to another explicitly.

### Common Conversions

- `int()` — Convert to integer
- `float()` — Convert to float
- `str()` — Convert to string
- `list()` — Convert to list

### Code Example: Type Casting

```python
# String to Integer
age_str = "18"
age = int(age_str)
print(age) # 18

# Float to Integer (truncates decimal)
pi = 3.14
pi_int = int(pi)
print(pi_int) # 3

# Integer to String
marks = 95
marks_str = str(marks)
print(marks_str) # "95"
```

---

## 7. Math Library Functions

The `math` module provides mathematical functions.

### Common Functions

- `math.sqrt(x)` — Square root
- `math.pow(x, y)` — Power
- `math.ceil(x)` — Ceiling
- `math.floor(x)` — Floor
- `math.factorial(x)` — Factorial

### Code Example: Math Functions

```python
import math

print(math.sqrt(16)) # 4.0
print(math.pow(2, 3)) # 8.0
print(math.ceil(3.2)) # 4
print(math.floor(3.8)) # 3
print(math.factorial(5)) # 120
print(math.pi) # 3.14159...
```

---

## 8. Conditional Statements

### if Statement

Executes code only if condition is `True`.

### if-else Statement

Executes one block if condition is `True`, another if `False`.

### if-elif-else Statement

Checks multiple conditions in sequence.

### Code Example: Conditionals

```python
# Simple if
age = 18
if age >= 18:
print("You can vote")

# if-else
marks = 65
if marks >= 40:
print("Pass")
else:
print("Fail")

# if-elif-else
marks = 85
if marks >= 90:
grade = 'A+'
elif marks >= 80:
grade = 'A'
elif marks >= 70:
grade = 'B'
elif marks >= 60:
grade = 'C'
else:
grade = 'D'
print(f"Grade: {grade}") # Grade: A
```

---

## 9. Looping Statements

### for Loop

Iterates over a sequence (list, tuple, string, range).

### while Loop

Repeats as long as condition is `True`.

### Code Example: Loops

```python
# for loop with range
for i in range(1, 6):
print(i) # Prints 1 to 5

# for loop with list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

# while loop
count = 1
while count <= 5:
print(count)
count += 1

# while loop with user input
password = ""
while password != "python123":
password = input("Enter password: ")
print("Access granted!")
```

---

## 10. Jump Statements

### break Statement

Exits the loop immediately.

### continue Statement

Skips the rest of current iteration and moves to next.

### Code Example: Jump Statements

```python
# break - Find first multiple of 7
for i in range(1, 100):
if i % 7 == 0:
print(f"First multiple of 7: {i}")
break # Exit loop

# continue - Print only odd numbers
for i in range(1, 11):
if i % 2 == 0: # If even
continue # Skip this iteration
print(i) # Only prints odd numbers
# Output: 1 3 5 7 9
```

---

## 11. Nested Loops

A loop inside another loop.

### Code Example: Nested Loops

```python
# Multiplication table
for i in range(1, 4):
for j in range(1, 4):
print(f"{i} x {j} = {i*j}")
print() # Blank line

# Pattern printing
for i in range(1, 6):
for j in range(i):
print("*", end=" ")
print() # New line

# Output:
# *
# * *
# * * *
# * * * *
# * * * * *
```

---

## 12. Loop else Statement

The `else` clause executes when loop completes normally (not terminated by `break`).

### Code Example: Loop else

```python
# Check if number is prime
num = 17
for i in range(2, num):
if num % i == 0:
print(f"{num} is not prime")
break
else:
print(f"{num} is prime") # Executes if no break occurred
```

---

## Practice Problems

Try these problems to test your understanding:

1. Write a program to check if a number is even or odd
2. Find the factorial of a number using a loop
3. Print the Fibonacci series up to n terms
4. Check if a string is a palindrome
5. Find the sum of all even numbers between 1 and 100
6. Print all prime numbers between 1 and 50
7. Create a simple calculator using if-elif-else
8. Count the number of vowels in a string
9. Find the largest of three numbers
10. Generate a multiplication table for any number

---

## Quick Reference

**Input/Output:**

- `input()` — Read input as string
- `print()` — Display output

**Type Conversion:**

- `int()`, `float()`, `str()`, `bool()`, `list()`, `tuple()`

**Common Functions:**

- `type()` — Get data type
- `len()` — Get length
- `range()` — Generate sequence of numbers

**Math Module:**

- Import first: `import math`
- `math.sqrt()`, `math.pow()`, `math.ceil()`, `math.floor()`

**Loop Control:**

- `break` — Exit loop
- `continue` — Skip to next iteration
- `pass` — Do nothing (placeholder)