Python – Notes
1. Introduction
Python is a high-level, interpreted, general-purpose programming language.
Developed by Guido van Rossum in 1991.
Known for simple syntax, readability, and versatility.
Supports Object-Oriented, Procedural, and Functional Programming.
2. Features of Python
Easy to learn and read
Interpreted – Code executes line by line
Cross-platform – Works on Windows, Linux, Mac
High-level – Abstracts complex details
Dynamic typing – No need to declare variable type
Extensive libraries – e.g., NumPy, Pandas, Matplotlib
Open-source
3. Python Syntax
Python uses indentation instead of
{}
if 5 > 2:
print("Five is greater than two")
4. Variables & Data Types
Variables: No need to declare type
x = 10
name = "Dinesh"
pi = 3.14
flag = True
Data types:
Numbers: int, float, complex
String: “Hello”, ‘Hello’
Boolean: True, False
List:
[1,2,3]Tuple:
(1,2,3)Set:
{1,2,3}Dictionary:
{"name":"Dinesh", "age":25}
5. Type Conversion
x = int(3.5) # float to int
y = float(5) # int to float
s = str(100) # int to string
6. Operators
Arithmetic:
+ - * / % ** //Comparison:
== != > < >= <=Logical:
and or notAssignment:
= += -= *= /= %=Membership:
in, not inIdentity:
is, is not
7. Strings
s = "Hello Python"
print(len(s)) # Length
print(s.upper()) # Uppercase
print(s.lower()) # Lowercase
print(s[0]) # Indexing
print(s[0:5]) # Slicing
print(s.split()) # Split into list
print(s.replace("Python","World")) # Replace
8. Lists
fruits = ["apple","banana","orange"]
fruits.append("mango") # Add element
fruits.remove("banana") # Remove element
fruits.pop(1) # Remove by index
print(fruits[0]) # Access element
print(len(fruits)) # Length
fruits.sort() # Sort list
9. Tuples
Immutable list
t = (1,2,3,4)
print(t[0])
10. Sets
Unique unordered elements
s = {1,2,3,2,1}
s.add(4)
s.remove(3)
print(s)
11. Dictionaries
person = {"name":"Dinesh","age":25}
print(person["name"])
person["city"] = "Aurangabad"
del person["age"]
12. Conditional Statements
age = 18
if age >= 18:
print("Adult")
elif age > 12:
print("Teenager")
else:
print("Child")
13. Loops
for loop
for i in range(5):
print(i)
while loop
i = 0
while i < 5:
print(i)
i += 1
break & continue
for i in range(5):
if i==3:
break
print(i)
14. Functions
def greet(name):
return "Hello "+name
print(greet("Dinesh"))
Default parameters:
def greet(name="Guest"):
return "Hello "+name
Variable-length arguments:
def add(*nums):
return sum(nums)
15. Lambda Functions
Anonymous single-line functions
square = lambda x: x*x
print(square(5))
16. Object-Oriented Programming
class Car:
def __init__(self, color, model):
self.color = color
self.model = model
def drive(self):
print("Car is driving")
myCar = Car("Red","2025")
myCar.drive()
Inheritance
class Vehicle:
def info(self):
print("Vehicle info")
class Car(Vehicle):
pass
myCar = Car()
myCar.info()
Encapsulation: Use
_or__for private variablesPolymorphism: Same method name, different class
17. Modules & Packages
Import built-in or custom modules
import math
print(math.sqrt(16))
from math import sqrt
print(sqrt(25))
Create package: folder with
__init__.py
18. File Handling
# Write file
with open("test.txt","w") as f:
f.write("Hello World")
# Read file
with open("test.txt","r") as f:
print(f.read())
19. Exception Handling
try:
x = 5/0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Execution finished")
20. Python Libraries (Popular)
NumPy – Numerical computations
Pandas – Data analysis
Matplotlib/Seaborn – Data visualization
Tkinter – GUI development
Requests – HTTP requests
BeautifulSoup – Web scraping
21. Important Notes
Python is case-sensitive
Indentation is mandatory
Dynamic typing → variables can change type
Comments:
# Single line
""" Multi-line """
