Python has become one of the most popular programming languages. Here's why:
β’ Simple syntax (close to English).
β’ Great for beginners and pros alike.
print("Hello, World!")
β’ Can be used in web development, automation, AI, data analysis, machine learning, etc.
β’ Tons of tutorials, solutions, and discussions online.
β’ Rich set of libraries for every use case: data, AI, visualization, web, etc.
Python is the #1 language for Data Science. Here's how it fits into each stage:
β’ Scrape data from the web using BeautifulSoup, Scrapy.
β’ Collect API data using requests.
import requests
response = requests.get("https://api.github.com")
print(response.json())
β’ Use pandas and numpy to clean, filter, and preprocess data. import pandas as pd df = pd.read_csv("data.csv") df.dropna(inplace=True)
β’ Analyze trends, find patterns, and summarize data.
print(df.describe())
print(df.groupby("Gender")["Salary"].mean())
β’ Use matplotlib, seaborn, or plotly to create graphs and dashboards.
import matplotlib.pyplot as plt
df['Salary'].hist()
plt.show()
β’ Use scikit-learn, TensorFlow, or PyTorch to build models.
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X, y)
β’ Deploy models via Flask, FastAPI, or cloud services (AWS, Heroku).
Variables are used to store data. You donβt need to declare the type; Python figures it out.
name = "Alice"
age = 25
height = 5.6
β’ name is a string
β’ age is an integer
β’ height is a float
β’ Must start with a letter or underscore _
β’ Cannot start with a number
β’ Case-sensitive (name β Name)
| Type | Example | Description |
|---|---|---|
| int | 10, -3 | Whole numbers |
| float | 3.14, -7.2 | Decimal numbers |
| str | "hello" | Text |
| bool | True, False | Boolean (logical) values |
| list | [1, 2, 3] | Ordered, changeable collection |
| tuple | (1, 2, 3) | Ordered, immutable collection |
| dict | {"name": "John"} | Key-value pairs |
| set | {1, 2, 3} | Unordered, no duplicates |
x = 42 # int
pi = 3.14 # float
name = "Python" # str
is_active = True # bool
colors = ["red", "blue", "green"] # list
Comments help explain the code. They are ignored by the interpreter. β Single-line Comment:
# This is a comment
name = "Alice" # This is also a comment
β Multi-line Comment (Hacky Way):
# This is a multi-line comment
# You can continue using #
# on every line
Docstrings are multi-line strings used to describe what a function/class/module does. Theyβre enclosed in triple quotes (''' or """).
β Example:
def greet(name):
"""
This function greets the user with the given name.
"""
print(f"Hello, {name}!")
You can access the docstring using:
print(greet.__doc__)
πΉ 1. Input in Python input() is used to take user input from the keyboard. It always returns a string. β Example:
name = input("Enter your name: ")
print("Hello", name)
π Note: You need to convert input to integer/float if needed (explained in type conversion below).
πΉ 2. Output in Python print() is used to display output. β Example:
print("Hello, world!")
β
Multiple Values:
name = "Alice"
age = 25
print("Name:", name, "| Age:", age)
β Using f-strings (modern & readable):
print(f"My name is {name} and I am {age} years old.")
πΉ 3. Type Conversion in Python Used to convert data from one type to another. β Common Functions:
| Function | Converts To |
|---|---|
| int() | Integer |
| float() | Float |
| str() | String |
| bool() | Boolean |
πΉ 4. Examples of Type Conversion π§ͺ Convert string input to integer:
age = input("Enter your age: ")
age = int(age) # now it's an integer
print(f"Next year youβll be {age + 1}")
π§ͺ Float to int:
num = 3.99
num_int = int(num)
print(num_int) # Output: 3
π§ͺ Int to float:
x = 5
print(float(x)) # Output: 5.0
π§ͺ String to float:
rate = float(input("Enter interest rate: "))
print(rate * 2)
β Bonus: Check Type
x = "123"
print(type(x)) # Output: <class 'str'>
y = int(x)
print(type(y)) # Output: <class 'int'>
π§ Mini Practice
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"{name}, you were born in {2025 - age}")
Operators are symbols or words used to perform operations on variables and values.
β Categories of Operators
| Type | Used | For |
|---|---|---|
| Arithmetic Operators | Basic | math |
| Assignment Operators | Assigning | values |
| Comparison Operators | Comparing | values |
| Logical Operators | Combining | conditions |
| Identity Operators | Object | identity |
| Membership Operators | Membership | testing |
| Bitwise Operators | Binary | operations |
Used for mathematical operations.
| Operator | Description | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 5 - 3 | 2 |
| * | Multiplication | 5 * 3 | 15 |
| / | Division | 5 / 2 | 2.5 |
| // | Floor Division | 5 // 2 | 2 |
| % | Modulus (remainder) | 5 % 2 | 1 |
| ** | Exponentiation | 2 ** 3 | 8 |
Used to assign values to variables.
| Operator | Example | Same As |
|---|---|---|
| = | x = 5 | Assign 5 to x |
| += | x += 3 | x = x + 3 |
| -= | x -= 2 | x = x - 2 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 2 | x = x / 2 |
| //= | x //= 2 | x = x // 2 |
| %= | x %= 2 | x = x % 2 |
| **= | x **= 2 | x = x ** 2 |
Used to compare values. Returns True or False.
| Operator | Description | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal to | 5 != 3 | True |
| > | Greater than | 5 > 3 | True |
| < | Less than | 3 < 5 | True |
| >= | Greater or equal | 5 >= 5 | True |
| <= | Less or equal | 5 <= 6 | True |
Used to combine conditional statements.
| Operator | Description | Example | Result |
|---|---|---|---|
| and | True if both are True | True and False | False |
| or | True if at least one is True | True or False | True |
| not | Reverses the result | not True | False |
Used to compare memory locations (not just values).
| Operator | Description | Example | Result |
|---|---|---|---|
| is True | if both refer to same object | a is b | True/False |
| is not True | if not the same object | a is not b | True/False |
| π§ͺ Example: |
a = [1, 2]
b = a
print(a is b) # True (same memory)
print(a == b) # True (same value)
c = [1, 2]
print(a is c) # False
print(a == c) # True
Used to test whether a value is in a sequence.
| Operator | Description | Example | Result |
|---|---|---|---|
| in | True if value exists | "a" in "cat" | True |
| not in | True if value doesnβt | 3 not in [1, 2] | True |
Used for binary operations on integers.
| Operator | Description | Example | Result |
|---|---|---|---|
| & | AND | 5 & 3 | 1 |
| ` | ` | OR | `5 |
| ^ | XOR | 5 ^ 3 | 6 |
| ~ | NOT | ~5 | -6 |
| << | Left Shift | 5 << 1 | 10 |
| >> | Right Shift | 5 >> 1 | 2 |
π§ Practice Examples:
# Arithmetic
a, b = 10, 3
print(a + b, a % b, a ** b)
# Comparison
print(a > b, a == 10)
# Logical
print((a > 5) and (b < 5))
# Identity
x = [1, 2]
y = [1, 2]
print(x is y, x == y)
# Membership
print("e" in "hello", 2 in [1, 2, 3])
Control Flow Statements Control flow statements allow you to control the execution of your Python code based on conditions and loops.
πΉ if, elif, and else These statements are used to make decisions in Python. β Syntax:
if condition:
# block of code
elif another_condition:
# block of code
else:
# block of code
β Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
elif age > 0:
print("You are too young to vote.")
else:
print("Invalid age")
num = 10
if num > 0:
if num % 2 == 0:
print("Positive Even")
else:
print("Positive Odd")
Used to iterate over a sequence (list, string, range, etc.) β Example 1: Iterating a list
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
β Example 2: Using range()
for i in range(1, 6):
print(i)
Runs as long as the condition is True. β Example:
count = 0
while count < 5:
print("Count:", count)
count += 1
These help manage flow inside loops.
for i in range(10):
if i == 5:
break
print(i)
for i in range(5):
if i == 2:
continue
print(i)
for i in range(3):
pass # used when code is not yet written
You can use an else block with loops. It runs only if the loop doesn't break. β Example:
for i in range(5):
print(i)
else:
print("Loop finished normally.")
β Example with break:
for i in range(5):
if i == 3:
break
print(i)
else:
print("Wonβt print because loop was broken.")
nums = [1, 4, 7, 8, 10, 13]
even = 0
odd = 0
for num in nums:
if num % 2 == 0:
even += 1
else:
odd += 1
print(f"Even numbers: {even}")
print(f"Odd numbers: {odd}")
| Statement | Purpose |
|---|---|
| if / elif / else | Decision making |
| for | Loop through iterable |
| while | Repeat until condition is false |
| break | Exit loop early |
| continue | Skip current iteration |
| pass | Do nothing (placeholder) |
| else (with loop) | Run if loop wasnβt broken |
π What are Data Structures? Data structures are ways to store and organize data so it can be used efficiently. Pythonβs Built-in Data Structures:
- List
- Tuple
- Set
- Dictionary
- String (optional, sometimes treated as a data structure)
A list is an ordered, mutable collection.
β Create a list:
fruits = ['apple', 'banana', 'cherry']
β Key Methods:
| Method | Description |
|---|---|
| append() | Add item to end |
| insert() | Insert at a position |
| pop() | Remove and return item |
| remove() | Remove specific item |
| sort() | Sort the list |
| reverse() | Reverse the list |
| index() | Find index of an item |
| count() | Count occurrences |
fruits.append("mango")
fruits.insert(1, "orange")
print(fruits.pop())
print(fruits.index("banana"))
A tuple is an ordered, immutable collection. β Create a tuple:
colors = ('red', 'green', 'blue')
β Key Methods:
| Method | Description |
|---|---|
| count() | Count occurrences of value |
| index() | Return first index of value |
print(colors.count("red"))
print(colors.index("blue"))
A set is an unordered, mutable, no-duplicates collection.
β Create a set:
nums = {1, 2, 3, 4}
β Key Methods:
| Method | Description |
|---|---|
| add() | Add an element |
| update() | Add multiple elements |
| remove() | Remove element (error if not found) |
| discard() | Remove if present |
| clear() | Remove all elements |
| union() | Return union of sets |
| intersection() | Return common elements |
| difference() | Return difference |
nums.add(5)
nums.update([6, 7])
nums.remove(2)
nums2 = {5, 6, 8}
print(nums.union(nums2))
A dictionary is an unordered collection of key-value pairs.
β Create a dict:
student = {
"name": "Alice",
"age": 21,
"course": "Python"
}
β Key Methods:
| Method | Description |
|---|---|
| get() | Get value for key |
| keys() | All keys |
| values() | All values |
| items() | All key-value pairs |
| update() | Update with another dict |
| pop() | Remove and return value by key |
| clear() | Remove all items |
print(student.get("name"))
student.update({"age": 22})
print(student.keys())
student.pop("course")
Strings are immutable sequences of characters.
β Create a string:
text = "hello python"
β Key Methods:
| Method | Description |
|---|---|
| upper() | Convert to uppercase |
| lower() | Convert to lowercase |
| split() | Split into list |
| replace() | Replace text |
| find() | Find index of substring |
| strip() | Remove leading/trailing spaces |
print(text.upper())
print(text.replace("python", "world"))
print(text.split())
# List
shopping = ["milk", "bread"]
shopping.append("eggs")
shopping.remove("milk")
# Tuple
coords = (10, 20)
print("X:", coords[0])
# Set
unique_nums = set([1, 2, 2, 3])
print(unique_nums)
# Dict
book = {"title": "Python", "pages": 300}
print(book["title"])
| Structure | Ordered | Mutable | Duplicate Allowed | Example Type |
|---|---|---|---|---|
| List | β | β | β | [] |
| Tuple | β | β | β | () |
| Set | β | β | β | {} |
| Dictionary | β | β | β | (on keys) {key: val} |
Object-Oriented Programming (OOP) is a programming paradigm that models real-world entities as objects, with attributes (data) and behaviors (methods).
| Pillar | Description |
|---|---|
| Encapsulation | Binding data and methods in a class |
| Abstraction | Hiding internal details, showing only essentials |
| Inheritance | Reusing code from parent class |
| Polymorphism | Same method name behaves differently in classes |
πΉ Class: Blueprint for creating objects πΉ Object: Instance of the class
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def start(self):
print(f"{self.brand} {self.model} is starting...")
# Creating an object
my_car = Car("Toyota", "Camry")
my_car.start()
Protects data by restricting access using methods.
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.__balance = balance # private variable
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
acc = BankAccount("Alice", 1000)
acc.deposit(500)
print(acc.get_balance()) # 1500
Hides complex internal logic and shows only the interface.
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
return "Bark"
d = Dog()
print(d.sound())
One class inherits properties from another.
class Vehicle:
def __init__(self, brand):
self.brand = brand
def start(self):
print(f"{self.brand} is ready to go!")
class Bike(Vehicle):
def ring_bell(self):
print("Ring Ring!")
b = Bike("Hero")
b.start()
b.ring_bell()
Same function name behaves differently. πΉ With Inheritance
class Bird:
def sound(self):
print("Some generic sound")
class Parrot(Bird):
def sound(self):
print("Squawk")
class Crow(Bird):
def sound(self):
print("Caw")
for bird in (Parrot(), Crow()):
bird.sound()
πΉ Operator Overloading
class Book:
def __init__(self, pages):
self.pages = pages
def __add__(self, other):
return self.pages + other.pages
b1 = Book(100)
b2 = Book(200)
print(b1 + b2) # 300
| Concept | Syntax | Feature |
|---|---|---|
| Constructor | init() | method |
| Access Modifiers | self.__private | |
| Abstraction | from abc import ABC,Inheritance | class B(A) |
| Polymorphism | Method | Overriding/Overloading |
We'll create a small project that allows:
- Adding students
- Viewing student details
- Calculating average marks
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
self.marks = []
def add_mark(self, mark):
self.marks.append(mark)
def get_average(self):
if self.marks:
return sum(self.marks) / len(self.marks)
return 0
def display_info(self):
print(f"Name: {self.name}")
print(f"Roll No: {self.roll}")
print(f"Marks: {self.marks}")
print(f"Average: {self.get_average():.2f}")
# Driver code
students = []
while True:
print("\n1. Add Student\n2. Show Students\n3. Exit")
choice = input("Enter choice: ")
if choice == "1":
name = input("Enter name: ")
roll = input("Enter roll no: ")
s = Student(name, roll)
for i in range(3):
mark = int(input(f"Enter mark {i+1}: "))
s.add_mark(mark)
students.append(s)
elif choice == "2":
for s in students:
s.display_info()
elif choice == "3":
break
else:
print("Invalid choice")