Python Theory Handout Easy and Clean
Python Theory Handout Easy and Clean
Theory:
Python is a high-level, interpreted programming language.
It emphasizes code readability and supports multiple programming paradigms.
It is used in web development, automation, data science, and more.
Example:
print("Welcome to Python!")
print(type(10)) # Output: <class 'int'>
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: Variables and Data Types
Theory:
Variables are containers for storing data values.
Python has dynamic typing: you don't need to declare types explicitly.
Example:
age = 20
price = 99.99
name = "Alice"
is_student = True
print(type(name), type(price))
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: Operators
Theory:
Python supports the following operators:
- Arithmetic: +, -, *, /, %, **, //
- Comparison: ==, !=, >, <, >=, <=
- Logical: and, or, not
- Assignment: =, +=, -=, *=
Example:
a = 10
b=3
print(a + b, a % b)
print(a > b and b < 5)
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: Control Statements
Theory:
Python uses if-elif-else for conditional branching.
The blocks are defined by indentation.
Example:
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: Loops
Theory:
Loops are used to repeat a block of code.
- for loop: iterate over a sequence
- while loop: continue as long as a condition is true
Example:
for i in range(1, 6):
print(i)
i=1
while i <= 5:
print(i)
i += 1
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: Functions
Theory:
Functions allow code reuse and better organization.
Use def to define a function and return to return a value.
Example:
def greet(name):
return "Hello, " + name
print(greet("John"))
def square(n):
return n * n
print(square(4))
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: Data Structures
Theory:
Python includes built-in data structures:
- List: ordered, mutable
- Tuple: ordered, immutable
- Set: unordered, unique items
- Dictionary: key-value pairs
Example:
fruits = ["apple", "banana"]
colors = ("red", "blue")
nums = {1, 2, 3}
info = {"name": "John"}
print(fruits[0], info["name"])
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: Strings and Methods
Theory:
Strings are sequences of characters.
Common methods: .upper(), .lower(), .replace(), .split()
Example:
text = "hello python"
print(text.upper())
print(text.replace("python", "world"))
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: File Handling
Theory:
Python can read and write files using open().
Modes: 'r' (read), 'w' (write), 'a' (append)
Example:
f = open("demo.txt", "w")
f.write("This is Python")
f.close()
f = open("demo.txt", "r")
print(f.read())
f.close()
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: OOP in Python
Theory:
Object-Oriented Programming uses classes and objects.
Classes define a blueprint; objects are instances.
Example:
class Student:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello,", self.name)
s = Student("Alice")
s.greet()
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: Modules and Packages
Theory:
Modules are Python files with functions/classes.
Packages are directories with __init__.py containing modules.
Example:
import math
print(math.sqrt(25))
# Custom module
# utils.py:
def add(x, y): return x + y
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: Exception Handling
Theory:
Use try-except to handle runtime errors.
Optional: else for no-exception block, finally to run always.
Example:
try:
x = int(input("Enter a number: "))
print(10 / x)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid input")
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________
Topic: Sample Programs
Theory:
1. Swap Two Numbers:
a, b = 5, 10
a, b = b, a
print(a, b)
Example:
N/A
Your Notes:
_______________________________________________________________________________
___________
_______________________________________________________________________________
___________