1.
Introduction to Python
What is Python?
o A high-level, interpreted programming language.
o Easy-to-read syntax (looks like English).
o Widely used in web development, data science, automation, AI, cybersecurity,
and more.
How Python Works:
o You write code → Python interpreter reads it → executes line by line.
o No need for compilation (like in C/C++/Java).
Running Python:
1. Interactive mode → just type python in terminal.
2. Script mode → save file as program.py then run:
3. python program.py
2. Basic Syntax
✅ Printing Output
print("Hello, Python!")
Output:
Hello, Python!
✅ Comments
# This is a single-line comment
"""
This is
a multi-line
comment
"""
✅ Variables & Data Types
# Assigning values
name = "Alice" # String
age = 25 # Integer
height = 5.7 # Float
is_student = True # Boolean
print(name, age, height, is_student)
Output:
Alice 25 5.7 True
3. Operators
✅ Arithmetic Operators
a, b = 10, 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a % b) # 1
print(a ** b) # 1000 (10^3)
✅ Comparison Operators
print(10 > 5) # True
print(10 == 5) # False
print(10 != 5) # True
✅ Logical Operators
x, y = True, False
print(x and y) # False
print(x or y) # True
print(not x) # False
4. Control Flow
✅ If-Else
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Output:
You are an adult.
✅ For Loop
for i in range(5):
print("Number:", i)
Output:
Number: 0
Number: 1
Number: 2
Number: 3
Number: 4
✅ While Loop
count = 1
while count <= 3:
print("Count is:", count)
count += 1
5. Data Structures
✅ String
text = "Python"
print(text[0]) # P
print(text.upper()) # PYTHON
print(text[::-1]) # nohtyP
✅ List
fruits = ["apple", "banana", "cherry"]
fruits.append("mango") # Add
print(fruits[1]) # banana
print(len(fruits)) # 4
✅ Tuple (immutable list)
point = (3, 4)
print(point[0]) # 3
✅ Set (unique values)
nums = {1, 2, 2, 3}
print(nums) # {1, 2, 3}
✅ Dictionary (key-value pairs)
student = {"name": "Alice", "age": 20}
print(student["name"]) # Alice
6. Functions
def greet(name):
return "Hello, " + name
print(greet("Bob"))
Output:
Hello, Bob
7. Modules & Libraries
import math
print(math.sqrt(16)) # 4.0
print(math.pi) # 3.14159...
8. File Handling
# Writing
with open("test.txt", "w") as f:
f.write("Hello File")
# Reading
with open("test.txt", "r") as f:
print(f.read())
9. Error Handling
try:
num = int("abc") # invalid conversion
except ValueError:
print("Error: Invalid number")
Output:
Error: Invalid number
10. Object-Oriented Basics (OOP)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hi, I’m {self.name} and I’m {self.age} years old."
p1 = Person("Alice", 25)
print(p1.greet())
Output:
Hi, I’m Alice and I’m 25 years old.
📝 Practice Exercises (Starter Level)
1. Print your name, age, and favorite hobby in one line.
2. Write a program that takes two numbers and prints their sum.
3. Create a list of 5 favorite movies and print the third one.
4. Write a function that takes a number and returns whether it’s even or odd.
5. Create a dictionary of 3 countries and their capitals, then print one capital.