Python (Complete Tutorial)
1. Variables
x = 10 # integer
name = "Sai" # string
pi = 3.14 # float
is_active = True # boolean
2. Data Types
int, float, str, bool, list, tuple, set, dict
Data Type Example Range / Size
int 42 Unlimited (memory-based)
float 3.14 ±1.8 × 10^308 (15–17 digits precision)
complex 3+4j Based on float range
str "Hello" Unlimited
list [1,2,3] Unlimited
tuple (1,2,3) Unlimited
range range(0,10) Limited by int size
dict {"a":1} Unlimited
set {1,2,3} Unlimited
frozenset frozenset({1,2,3}) Unlimited
bool True / False Only 2 values
bytes b'ABC' 0–255 per element
bytearray bytearray(5) 0–255 per element
memoryview memoryview(b'abc') 0–255 per element
3. Operators
+, -, *, /, %, //, ** (arithmetic)
==, !=, >, <, >=, <= (comparison)
and, or, not (logical)
4. Conditional Statements
x = 10
if x > 5:
print("Greater")
elif x == 5:
print("Equal")
else:
print("Smaller")
5. Loops
#For loop
for i in range(5):
print(i)
# While loop
count = 0
while count < 5:
print(count)
count += 1
6. Functions
def greet(name):
return f"Hello {name}"
print(greet("Sai"))
7. Lists
fruits = ["apple", "banana", "cherry"]
[Link]("mango")
print(fruits[0])
8. Tuples
colors = ("red", "green", "blue")
print(colors[1])
9. Sets
nums = {1,2,3,3,2}
print(nums) # {1,2,3}
● List is a collection which is ordered and changeable. Allows duplicate
members.
● Tuple is a collection which is ordered and unchangeable. Allows
duplicate members.
● Set is a collection which is unordered, unchangeable*, and unindexed.
No duplicate members.
● Dictionary is a collection which is ordered** and changeable. No
duplicate members.
10. Dictionaries
student = {"name":"Sai", "age":22}
print(student["name"])
11. What is a Class?
A class is a blueprint for creating objects. Objects have:
- Attributes (variables / data)
- Methods (functions / actions)
12. Defining a Class
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age
def greet(self):
print(f"Hello, my name is {[Link]} and I am {[Link]} years old.")
13. Creating Objects
p1 = Person("Sai", 22)
p2 = Person("Anu", 25)
[Link]()
[Link]()
14. self Keyword
self represents the current object. Every method must have self as the first parameter.
15. Adding More Methods
class Calculator:
def __init__(self, a, b):
self.a = a
self.b = b
def add(self):
return self.a + self.b
def multiply(self):
return self.a * self.b
16. Inheritance
class Animal:
def sound(self):
print("Animals make sounds")
class Dog(Animal):
def sound(self):
print("Bark Bark!")
17. Special Methods
class Book:
def __init__(self, title, pages):
[Link] = title
[Link] = pages
def __str__(self):
return f"Book: {[Link]}, Pages: {[Link]}
18. Class vs Instance Variables
class Student:
school_name = "ABC School"
def __init__(self, name, grade):
[Link] = name
[Link] = grade
19. Class & Static Methods
class MathUtils:
pi = 3.14
@classmethod
def circle_area(cls, radius):
return [Link] * radius * radius
@staticmethod
def add(a, b):
return a + b
20. Encapsulation
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
21. Inheritance & super()
class Vehicle:
def __init__(self, brand):
[Link] = brand
class Car(Vehicle):
def __init__(self, brand, model):
super().__init__(brand)
[Link] = model
22. Multiple Inheritance
class A:
def show(self): print("A")
class B:
def show(self): print("B")
class C(A, B): pass
23. Abstract Classes
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self): pass
24. Polymorphism
class Dog:
def sound(self): return "Bark"
class Cat:
def sound(self): return "Meow"
animals = [Dog(), Cat()]
25. Operator Overloading
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
26. Property Decorator
class Employee:
def __init__(self, salary):
self._salary = salary
@property
def salary(self): return self._salary
@[Link]
def salary(self, value):
if value < 0: raise ValueError("Salary cannot be negative")
self._salary = value
27. Metaclasses
class Meta(type):
def __new__(cls, name, bases, dct):
print(f"Creating class {name}")
return super().__new__(cls, name, bases, dct)
class MyClass(metaclass=Meta):
pass
28. Dataclasses
from dataclasses import dataclass
@dataclass
class Product:
name: str
price: float
quantity: int = 0
Directive Description Example
%a Weekday, short version Wed
%A Weekday, full version Wednesday
%w Weekday as a number 0-6, 3
0 is Sunday
%d Day of month 01-31 31
%b Month name, short version Dec
%B Month name, full version December
%m Month as a number 01-12 12
%y Year, short version, without 18
century
%Y Year, full version 2018
%H Hour 00-23 17
%I Hour 00-12 05
%p AM/PM PM
%M Minute 00-59 41
%S Second 00-59 08
%f Microsecond 000000- 548513
999999
%z UTC offset +0100
%Z Timezone CST
%j Day number of year 001-366 365
%U Week number of year, 52
Sunday as the first day of
week, 00-53
%W Week number of year, 52
Monday as the first day of
week, 00-53
%c Local version of date and Mon Dec 31 17:41:00
time 2018
%C Century 20
%x Local version of date 12/31/18
%X Local version of time 17:41:00
%% A % character %
%G ISO 8601 year 2018
%u ISO 8601 weekday (1-7) 1
%V ISO 8601 weeknumber (01- 01
53)