Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Python Mini Course (Basics to OOP)

This course covers Python fundamentals with definitions, examples, and outputs, structured in Parts for easy learning.


Part 1: Python Basics

Definition

Python basics include variables, data types, and operators — the foundation for writing Python programs.

1.1 Variables and Data Types

x = 10        # int
y = 3.14      # float
name = "Mohan" # string
is_active = True # boolean

Explanation:

  • x → integer
  • y → float (decimal number)
  • name → string (text)
  • is_active → boolean (True/False)

Output

print(x, y, name, is_active)
# 10 3.14 Mohan True

1.2 Operators

a = 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 False

Explanation:

  • Arithmetic: + - * / % ** //
  • Comparison: == != > < >= <=
  • Logical: and, or, not

Part 2: Data Structures

Definition

Data structures store and organize data in Python for easy manipulation.

2.1 List

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
# ['apple', 'banana', 'cherry', 'orange']
  • Ordered, mutable collection.

2.2 Tuple

numbers = (1, 2, 3)
print(numbers[0])
# 1
  • Ordered, immutable collection.

2.3 Set

s = {1, 2, 2, 3}
print(s)
# {1, 2, 3}
  • Unordered, unique elements.

2.4 Dictionary

person = {"name": "Mohan", "age": 20}
print(person["name"])
# Mohan
  • Key-value pairs, unordered.

Part 3: Control Flow

Definition

Control flow statements decide the order of execution in Python programs.

3.1 Conditional Statements

age = 18
if age >= 18:
    print("Adult")
else:
    print("Minor")
# Adult
  • if → condition true
  • else → condition false

3.2 Loops

# 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 2

3.3 Loop Control

for i in range(5):
    if i == 3:
        break
    print(i)
# 0 1 2
  • break stops the loop

3.4 List Comprehension

squares = [x**2 for x in range(5)]
print(squares)
# [0, 1, 4, 9, 16]
  • Short way to create a list using a formula.

Part 4: Functions

Definition

Functions are reusable blocks of code that perform a specific task.

def greet(name):
    return f"Hello, {name}!"

print(greet("Mohan"))
# Hello, Mohan!

Lambda Function

square = lambda x: x**2
print(square(5))
# 25
  • Small, one-line function.

*args and **kwargs

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

Part 5: Object-Oriented Programming (OOP)

Definition

OOP is a programming paradigm based on objects, classes, inheritance, encapsulation, and polymorphism.

5.1 Classes and Objects

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.

5.2 Inheritance

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.

5.3 Encapsulation

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.

5.4 Polymorphism

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.

Part 6: Modules and Packages

Definition

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.123456

Part 7: File Handling

Definition

File 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!
  • with automatically closes the file.

Part 8: Exception Handling

Definition

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")
# Done
  • try → code that may fail
  • except → handle errors
  • finally → runs no matter what

End of Python Mini Course (Basics to OOP)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages