1.
Basic Syntax and Hello World
Theory:
Python’s syntax is designed to be clean and readable. Each line of code uses indentation
(spaces) instead of curly braces to define code blocks. Python files end in .py.
Example:
print("Hello, World!")
Prints a message. No special syntax for the main method needed in simple scripts.
2. Variables and Data Types
Theory:
Variables store values that your program uses. Python automatically determines the
type.
int: Integer numbers.
float: Decimal numbers.
str: Text (strings).
bool: True/False.
Python is dynamically typed—you can assign any type to any variable at any time.
Example:
age = 29 # int
height = 5.8 # float
name = "Sara" # str
is_student = True # bool
3. User Input and Typecasting
Theory:
input() lets users enter data. Data from input() is always a string, so you may need to
convert it (typecasting) to numbers for calculations.
Example:
number = input("Enter a number: ")
number = int(number) # typecasting from str to int
print(number + 2)
4. Control Flow (if, elif, else)
Theory:
These statements let you run code only in certain cases, making your program respond
differently in different situations.
Example:
temp = 32
if temp > 30:
print("It's hot!")
elif temp > 20:
print("It's warm.")
else:
print("It's cool or cold.")
5. Loops
Theory:
Loops let you repeat code many times with different data:
for: “For each” item in a sequence.
while: Repeats as long as a condition is true.
Examples:
For loop:
for i in range(3):
print(i)
While loop:
n=0
while n < 3:
print(n)
n += 1
6. Data Structures
Theory:
These are ways to store groups of related data:
List: Ordered, changeable series of items.
Tuple: Like a list, but can’t change (immutable).
Dictionary: Pairs of keys and values.
Set: Unordered, unique items.
Examples:
# List
pets = ["dog", "cat", "parrot"]
# Tuple
dimensions = (1920, 1080)
# Dictionary
student = {"name": "Tom", "score": 94}
# Set
unique_nums = {1, 2, 2, 3}
print(unique_nums) # Shows {1, 2, 3}
7. Functions
Theory:
Functions organize and reuse code. Each function does a specific job.
Example:
def add(a, b):
return a + b
result = add(5, 3) # result is 8
8. List Comprehensions
Theory:
A short way to make new lists from existing sequences.
Example:
numbers = [1, 2, 3]
squares = [x*x for x in numbers]
print(squares) # [1, 4, 9]
9. Exception Handling
Theory:
Catches and responds to errors, so your program doesn’t crash.
Example:
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero.")
10. File I/O
Theory:
Read from and write to files stored on your computer.
Examples:
Write:
with open("note.txt", "w") as file:
file.write("Study Python daily.")
Read:
with open("note.txt", "r") as file:
print(file.read())
11. Object-Oriented Programming (OOP)
Theory:
OOP creates reusable code by modeling data as objects with properties (attributes) and
actions (methods).
Example:
class Car:
def __init__(self, brand):
self.brand = brand
def honk(self):
print(f"{self.brand} goes beep!")
my_car = Car("Toyota")
my_car.honk()
12. Modules and Packages
Theory:
A module is any Python file. You can import code from modules. Packages are collections
of modules. This helps you organize code and use external libraries.
Example:
import math
print(math.pi)
By studying both the theory and code provided for every concept, you’ll build a solid
foundation in Python. Expand concepts into projects for the best learning! If you want a
specific real-world project with step-by-step build instructions, let me know.