PYTHON NOTES
Introduc on to Python
High-level, interpreted, and object-oriented programming language.
Developed by Guido van Rossum in 1991.
Extension: .py
Features:
o Easy to learn and read
o Open-source
o Pla orm-independent
o Large library support
o Dynamically typed
o Object-oriented and func onal support
Python Basics
➤ Comments
# Single-line comment
""" Mul -line
comment """
➤ Variables
No need to declare type explicitly.
x = 10
name = "Rahul"
➤ Data Types
Type Example
int 5
float 3.14
str "Hello"
bool True / False
list [1,2,3]
Type Example
tuple (1,2,3)
set {1,2,3}
dict {"a":1,"b":2}
complex 2+3j
Check type:
type(x)
Type Conversion
int("5") # string → int
float(3) # int → float
str(10) # int → string
list("abc") # string → list
Input & Output
name = input("Enter your name: ")
print("Hello", name)
Operators
Category Operators
Arithme c +, -, *, /, %, **, //
Comparison ==, !=, >, <, >=, <=
Logical and, or, not
Assignment =, +=, -=, *=, /=
Membership in, not in
Iden ty is, is not
Bitwise &, |, ^, ~, <<, >>
Condi onal Statements
if a > b:
print("A is greater")
elif a == b:
print("Equal")
else:
print("B is greater")
Loops
➤ For Loop
for i in range(5):
print(i)
➤ While Loop
i=0
while i < 5:
print(i)
i += 1
➤ Loop Control
break – exit loop
con nue – skip current itera on
pass – do nothing
Func ons
def add(a, b):
return a + b
print(add(5, 3))
➤ Lambda Func on
f = lambda x: x*x
print(f(5))
Data Structures
➤ List
fruits = ["apple", "banana", "cherry"]
[Link]("mango")
[Link]("apple")
print(fruits[1])
➤ Tuple
t = (1, 2, 3)
print(t[0])
➤ Set
s = {1, 2, 3, 2}
[Link](4)
print(s)
➤ Dic onary
d = {"name":"Rahul", "age":21}
print(d["name"])
d["age"] = 22
Strings
s = "Hello World"
print([Link]())
print([Link]())
print([Link]("Hello","Hi"))
print([Link]())
print(s[0:5])
String Forma ng:
name = "Rahul"
age = 21
print(f"My name is {name} and I am {age}")
File Handling
f = open("fi[Link]", "w")
[Link]("Hello World")
[Link]()
f = open("fi[Link]", "r")
print([Link]())
[Link]()
Excep on Handling
try:
x=1/0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Done")
Object-Oriented Programming (OOP)
➤ Class and Object
class Student:
def __init__(self, name, age):
[Link] = name
[Link] = age
def display(self):
print([Link], [Link])
s1 = Student("Rahul", 21)
[Link]()
➤ Inheritance
class Parent:
def show(self):
print("Parent")
class Child(Parent):
def display(self):
print("Child")
obj = Child()
[Link]()
[Link]()
➤ Polymorphism
class Dog:
def sound(self):
print("Bark")
class Cat:
def sound(self):
print("Meow")
for a in (Dog(), Cat()):
[Link]()
➤ Encapsula on
Using private variables: _var or __var
➤ Abstrac on
Using abstract class via abc module.
Modules and Packages
import math
print([Link](16))
from math import pi
print(pi)
To create your own module:
# [Link]
def greet():
print("Hello")
# [Link]
import mymodule
[Link]()
Libraries Overview
Library Use
NumPy Numerical compu ng
Pandas Data manipula on
Matplotlib / Seaborn Data visualiza on
Scikit-learn Machine learning
TensorFlow / PyTorch Deep learning
Flask / Django Web development
Miscellaneous
➤ List Comprehension
squares = [x*x for x in range(5)]
➤ Map, Filter, Reduce
from functools import reduce
nums = [1,2,3,4]
print(list(map(lambda x:x*2, nums)))
print(list(filter(lambda x:x%2==0, nums)))
print(reduce(lambda a,b:a+b, nums))
➤ Enumerate
for i, val in enumerate(["a","b","c"]):
print(i, val)