Python Programming Handbook
Python Programming Handbook
This handbook is tailored to cover the essential Python programming concepts required for your
examination. It includes detailed theory, examples, and practice questions to solidify your
understanding.
Python Programming Handbook
Module 1: Python Basics
1. Why Python?
Python is a versatile, beginner-friendly language widely used in industries such as web
development, data science, AI, and automation.
2. Applications of Python:
- Web frameworks (Django, Flask)
- Data analysis (Pandas, NumPy)
- AI/ML libraries (TensorFlow, scikit-learn)
- Scripting and automation
3. Number Systems:
Python supports binary, octal, and hexadecimal number systems, along with conversion functions
like bin(), oct(), and hex().
4. Variables and Data Types:
- Variables store values, and their types are dynamically assigned.
- Examples of data types: int, float, str, bool, list, tuple, dict.
5. Control Structures:
- if/elif/else allows decision-making based on conditions.
Example:
x = 10
Python Programming Handbook
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
Practice Set
1. Write a Python program to convert a decimal number to binary.
2. Create a program that uses an if-elif-else structure to classify numbers as positive, negative, or
zero.
3. Write a Python script to find the largest of three numbers using nested if-else.
Python Programming Handbook
Module 2: Loops and Functions
1. Loops:
- while loop: Executes as long as a condition is true.
- for loop: Iterates over a sequence like a list or range.
- Keywords:
- break: Exits the loop.
- continue: Skips the current iteration.
- pass: Does nothing and is used as a placeholder.
Example:
for i in range(5):
if i == 3:
continue
print(i)
2. Functions:
- A reusable block of code defined using the def keyword.
Example:
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
3. Recursive Functions:
Python Programming Handbook
- A function that calls itself.
Example: Factorial Calculation
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)
Practice Set
1. Write a program to calculate the factorial of a number using recursion.
2. Create a function that checks if a number is prime.
3. Write a Python script to generate the Fibonacci sequence up to n terms.
Python Programming Handbook
Module 3: Advanced Topics
1. String Operations:
- Slicing: Extracting substrings using indices.
Example: s = "Hello"[1:4] # Output: 'ell'
2. File Handling:
- Reading and writing files using open().
Example:
with open("file.txt", "r") as file:
content = file.read()
3. Exception Handling:
- Use try-except to manage errors.
Example:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
4. Object-Oriented Programming (OOP):
- Core concepts: Classes, objects, inheritance, polymorphism.
Example:
class Animal:
Python Programming Handbook
def speak(self):
return "I make a sound"
class Dog(Animal):
def speak(self):
return "Bark"
Practice Set
1. Write a program to read a file and count the number of words in it.
2. Create a class representing a student, with attributes for name and marks, and methods to
calculate grades.
3. Write a program to handle exceptions for invalid user inputs.