This course covers Python fundamentals with definitions, examples, and outputs, structured in Parts for easy learning.
Python basics include variables, data types, and operators — the foundation for writing Python programs.
x = 10 # int
y = 3.14 # float
name = "Mohan" # string
is_active = True # booleanExplanation:
x→ integery→ float (decimal number)name→ string (text)is_active→ boolean (True/False)
Output
print(x, y, name, is_active)
# 10 3.14 Mohan Truea = 5
b = 2
# Arithmetic
print(a + b, a - b, a * b, a / b, a % b, a ** b, a // b)
# 7 3 10 2.5 1 25 2
# Comparison
print(a == b, a > b, a != b)
# False True True
# Logical
print(a > b and b < 5, a > b or b > 5, not(a > b))
# True True FalseExplanation:
- Arithmetic:
+ - * / % ** // - Comparison:
== != > < >= <= - Logical:
and, or, not
Data structures store and organize data in Python for easy manipulation.
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
# ['apple', 'banana', 'cherry', 'orange']- Ordered, mutable collection.
numbers = (1, 2, 3)
print(numbers[0])
# 1- Ordered, immutable collection.
s = {1, 2, 2, 3}
print(s)
# {1, 2, 3}- Unordered, unique elements.
person = {"name": "Mohan", "age": 20}
print(person["name"])
# Mohan- Key-value pairs, unordered.
Control flow statements decide the order of execution in Python programs.
age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
# Adultif→ condition trueelse→ condition false
# For loop
for i in range(3):
print(i)
# 0 1 2
# While loop
count = 0
while count < 3:
print(count)
count += 1
# 0 1 2for i in range(5):
if i == 3:
break
print(i)
# 0 1 2breakstops the loop
squares = [x**2 for x in range(5)]
print(squares)
# [0, 1, 4, 9, 16]- Short way to create a list using a formula.
Functions are reusable blocks of code that perform a specific task.
def greet(name):
return f"Hello, {name}!"
print(greet("Mohan"))
# Hello, Mohan!square = lambda x: x**2
print(square(5))
# 25- Small, one-line function.
def add(*args):
return sum(args)
print(add(1, 2, 3))
# 6
def info(**kwargs):
print(kwargs)
info(name="Mohan", age=20)
# {'name': 'Mohan', 'age': 20}*args→ multiple positional arguments**kwargs→ multiple keyword arguments
OOP is a programming paradigm based on objects, classes, inheritance, encapsulation, and polymorphism.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hi, I'm {self.name}."
p = Person("Mohan", 20)
print(p.greet())
# Hi, I'm Mohan.class Student(Person):
def study(self):
return f"{self.name} is studying."
s = Student("Ali", 22)
print(s.greet())
print(s.study())
# Hi, I'm Ali.
# Ali is studying.- Child class can use parent methods and have its own methods.
class BankAccount:
def __init__(self, balance):
self.__balance = balance # private
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
account = BankAccount(100)
account.deposit(50)
print(account.get_balance())
# 150- Private variables start with
__and cannot be accessed directly.
class Payment:
def process_payment(self, amount):
raise NotImplementedError
class CreditCard(Payment):
def process_payment(self, amount):
return f"Credit card: ${amount}"
class PayPal(Payment):
def process_payment(self, amount):
return f"PayPal: ${amount}"
def make_payment(p, amt):
print(p.process_payment(amt))
make_payment(CreditCard(), 100)
make_payment(PayPal(), 200)
# Credit card: $100
# PayPal: $200- Same method behaves differently depending on the object type.
Modules are prewritten code that you can use to perform tasks easily.
import math
print(math.sqrt(16))
# 4.0
from datetime import datetime
print(datetime.now())
# 2025-09-14 14:35:12.123456File handling is reading and writing files in Python.
# Writing
with open("test.txt", "w") as f:
f.write("Hello, Python!")
# Reading
with open("test.txt", "r") as f:
print(f.read())
# Hello, Python!withautomatically closes the file.
Exception handling is handling errors without stopping the program.
try:
x = int("abc")
except ValueError as e:
print("Error:", e)
# Error: invalid literal for int() with base 10: 'abc'
finally:
print("Done")
# Donetry→ code that may failexcept→ handle errorsfinally→ runs no matter what
End of Python Mini Course (Basics to OOP)