ch 1 cs
ch 1 cs
Here�s a breakdown of the essential topics typically covered in the Python Revision
Tour for Class 12:
Lists (list): Ordered collections of items (e.g., [1, 2, 3], ['apple', 'banana'])
Example:
python
Copy
x = 10 # Integer
y = 3.14 # Float
name = "John" # String
is_active = True # Boolean
2. Control Structures
2.1 Conditional Statements:
Conditional statements allow you to execute certain blocks of code based on
conditions.
if-else Statement: Executes one block if the condition is true and another if
false.
Example:
python
Copy
age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
2.2 Loops:
Loops are used to repeat a block of code multiple times.
python
Copy
for i in range(5):
print(i) # Prints numbers 0 to 4
Example of while loop:
python
Copy
count = 0
while count < 5:
print(count)
count += 1 # Increments count by 1 after each loop
3. Functions in Python
Functions are blocks of code designed to do one specific task. They are defined
using the def keyword.
Example:
python
Copy
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
3.2 Return Statement:
A function can return a value using the return keyword.
Example:
python
Copy
def add(a, b):
return a + b
result = add(5, 7)
print(result) # Outputs 12
3.3 Scope of Variables:
Local Variables: Defined within a function, accessible only inside it.
Global Variables: Defined outside any function, accessible throughout the program.
4. Data Structures
4.1 Lists:
Lists are mutable sequences of elements. You can modify the contents of a list
after it is created.
Example:
python
Copy
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # Adds "orange" to the list
print(fruits[1]) # Outputs "banana"
4.2 Tuples:
Tuples are immutable, meaning you cannot change their elements once they are
defined.
Example:
python
Copy
coordinates = (10, 20)
print(coordinates[0]) # Outputs 10
4.3 Dictionaries:
Dictionaries store key-value pairs, where each key is unique.
Example:
python
Copy
person = {"name": "Alice", "age": 25}
print(person["name"]) # Outputs "Alice"
4.4 Sets:
Sets are unordered collections of unique elements.
Example:
python
Copy
unique_numbers = {1, 2, 3, 4}
unique_numbers.add(5) # Adds 5 to the set
5. String Manipulation
Strings in Python are immutable sequences of characters. You can perform various
operations on strings, like slicing, concatenation, and modification.
Example:
python
Copy
message = " Hello World! "
print(message.strip()) # Outputs "Hello World!"
5.2 String Slicing:
You can extract portions of a string using slicing.
Example:
python
Copy
word = "Python"
print(word[0:3]) # Outputs "Pyt"
6. File Handling
File handling allows you to interact with files�open, read, write, and close them.
Example:
python
Copy
file = open("data.txt", "r")
content = file.read()
file.close()
6.2 Writing to a File:
You can write data to a file using the write() method.
Example:
python
Copy
file = open("output.txt", "w")
file.write("Hello, World!")
file.close()
7. Exception Handling
In Python, exceptions are errors that occur during runtime. To handle errors
gracefully, Python uses try, except, and finally blocks.
Example:
python
Copy
try:
x = 10 / 0 # Division by zero
except ZeroDivisionError:
print("Error: Cannot divide by zero")
finally:
print("Execution completed")
8. Object-Oriented Programming (OOP)
Python is an object-oriented language, which allows the use of classes and objects
to organize code better.
python
Copy
class Car:
def __init__(self, model, color):
self.model = model
self.color = color
def start(self):
print(f"{self.model} started")
Example:
python
Copy
class Animal:
def speak(self):
print("Animal speaking")
class Dog(Animal):
def bark(self):
print("Woof!")
dog = Dog()
dog.speak() # Inherited method from Animal
dog.bark() # Method of Dog class
9. Libraries and Modules
Python provides many standard libraries and modules that extend its functionality.
Example:
python
Copy
import math
print(math.sqrt(16)) # Outputs 4.0
Conclusion
The Python Revision Tour for Class 12 covers the foundational concepts required for
solving more complex problems. Understanding variables, data types, control
structures, functions, file handling, exception handling, object-oriented
programming, and modules is essential for every student aspiring to master Python.
By revisiting and practicing these key concepts, students can confidently tackle a
wide variety of problems and applications.
This overview should help you revise and refresh your knowledge of Python and
prepare you for further studies or practical applications