Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 1: Revision of Python Topics (Class XI)

Welcome to Unit 1! Before we dive into the new concepts of Class XII, it is essential to have a solid grasp of the foundational Python programming concepts you learned in Class XI.

This chapter provides a quick refresher on the core components of Python.

1.1 Python Basics

Python is a high-level, interpreted, interactive, and object-oriented scripting language. It uses indentation to define blocks of code instead of curly braces.

Variables and Data Types

A variable is a named location in memory used to store data. In Python, you do not need to explicitly declare the data type of a variable.

# Variables
age = 17          # Integer (int)
marks = 95.5      # Floating point (float)
name = "Alice"    # String (str)
is_student = True # Boolean (bool)

Core Data Types:

  • Numbers: Integers (int), Floating-point numbers (float), Complex numbers (complex).
  • Strings: (str) A sequence of characters enclosed in single or double quotes. They are immutable.
  • Lists: (list) An ordered, mutable collection of items enclosed in square brackets [].
  • Tuples: (tuple) An ordered, immutable collection of items enclosed in parentheses ().
  • Dictionaries: (dict) An unordered, mutable collection of key-value pairs enclosed in curly braces {}. Keys must be unique and immutable.
  • Boolean: (bool) Represents True or False.
  • None: represents the absence of a value.

Mutable vs. Immutable Types

  • Mutable (can be changed in place): Lists, Dictionaries, Sets.
  • Immutable (cannot be changed in place): Integers, Floats, Strings, Tuples, Booleans.

1.2 Operators and Expressions

Python supports various operators to perform operations on variables and values.

  • Arithmetic: + (addition), - (subtraction), * (multiplication), / (division - float), // (floor division), % (modulus/remainder), ** (exponentiation).
  • Relational (Comparison): == (equal), != (not equal), > (greater than), < (less than), >= (greater than or equal), <= (less than or equal). They return a boolean value.
  • Logical: and, or, not. Used to combine conditional statements.
  • Assignment: =, +=, -=, *=, /=, etc.
  • Identity: is, is not. Checks if two variables refer to the same object in memory.
  • Membership: in, not in. Checks if a sequence is present in an object.

1.3 Flow of Control

Control flow statements determine the order in which code executes.

Conditional Statements

Used for decision making.

score = 85

if score >= 90:
    print("Grade A")
elif score >= 80:
    print("Grade B")
else:
    print("Grade C")

Iterative Statements (Loops)

Used to repeat a block of code.

for Loop: Used to iterate over a sequence (list, tuple, dictionary, set, or string).

# Using range()
for i in range(1, 6): # Prints 1, 2, 3, 4, 5
    print(i)

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

while Loop: Executes a set of statements as long as a condition is true.

count = 0
while count < 3:
    print(count)
    count += 1

Jump Statements:

  • break: Stops the loop entirely.
  • continue: Stops the current iteration and continues with the next iteration.

1.4 Strings, Lists, Tuples, and Dictionaries (Quick Reference)

Strings (Immutable)

s = "Hello World"
print(len(s))       # Length: 11
print(s[0])         # Indexing: 'H'
print(s[0:5])       # Slicing: 'Hello'
print(s.upper())    # Methods: 'HELLO WORLD'
print(s.replace("World", "Python")) # 'Hello Python'

Lists (Mutable)

L = [10, 20, 30]
L.append(40)     # Adds 40 to the end [10, 20, 30, 40]
L.insert(1, 15)  # Inserts 15 at index 1 [10, 15, 20, 30, 40]
L.pop()          # Removes and returns the last element (40)
L.remove(20)     # Removes the first occurrence of 20

Tuples (Immutable)

T = (1, 2, 3)
# T[0] = 5 # Error! Tuples cannot be modified
print(T.count(2)) # Counts occurrences of 2

Dictionaries (Mutable, Key-Value Pairs)

D = {"name": "Bob", "age": 18}
print(D["name"])       # Access value by key: 'Bob'
D["marks"] = 90        # Add a new key-value pair
del D["age"]           # Delete a key-value pair

for key in D.keys():   # Iterate through keys
    print(key, D[key])

1.5 Introduction to Modules

A module is a file containing Python definitions and statements. You can use code from a module by importing it.

import math
print(math.sqrt(16)) # Prints 4.0

import random
print(random.randint(1, 6)) # Simulates rolling a die (1 to 6)

This brief revision covers the foundational tools you will need. In the next chapter, we will learn how to organize these tools into reusable blocks using Functions.