Python Complete Notes (A to Z) with Examples
1. Introduction to Python - Python is an interpreted, high-level, general-purpose programming
language. - Features: Simple, readable, dynamic typing, extensive libraries. - Install: From https://
www.python.org/downloads/ - Run code: Using Python interpreter or IDE (PyCharm, VS Code).
Example:
print("Hello, Python!")
2. Basic Syntax - Variables: store data. - Data types: int, float, str, bool. - Comments: # for single line.
Example:
name = "Alice"
age = 20
print(name, age)
3. Operators - Arithmetic: + , - , * , / , % , ** , // - Assignment: = , += , -= - Comparison:
== , != , > , < , >= , <= - Logical: and , or , not
Example:
x = 10
y = 3
print(x + y, x ** y)
print(x > y and y < 5)
4. Conditional Statements
if x > y:
print("x is greater")
elif x == y:
print("x equals y")
else:
print("y is greater")
1
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("Alice"))
# Lambda function
square = lambda x: x**2
print(square(5))
7. Data Structures
# List
fruits = ["apple", "banana"]
fruits.append("cherry")
# Tuple
coords = (10, 20)
# Set
unique = {1, 2, 3}
unique.add(4)
# Dictionary
d = {"name": "Alice", "age": 20}
print(d["name"])
8. Strings
2
s = "Hello, World!"
print(s[0:5]) # Slice
print(s.lower())
print(f"Name: {d['name']}")
9. Modules & Packages
import math
print(math.sqrt(16))
from random import randint
print(randint(1, 10))
10. File Handling
# Write file
with open("test.txt", "w") as f:
f.write("Hello Python")
# Read file
with open("test.txt", "r") as f:
print(f.read())
11. Exception Handling
try:
num = int(input("Enter number: "))
except ValueError:
print("Invalid input")
finally:
print("End of program")
12. Object-Oriented Programming
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
3
print(f"Hi, I'm {self.name}")
p = Person("Alice", 20)
p.greet()
13. Advanced Topics
# Decorator
def decorator(func):
def wrapper():
print("Before function")
func()
return wrapper
@decorator
def say_hello():
print("Hello")
say_hello()
# Generator
def gen():
for i in range(5):
yield i
for val in gen():
print(val)
14. Popular Libraries Overview
# NumPy
import numpy as np
arr = np.array([1,2,3])
print(arr * 2)
# Pandas
import pandas as pd
df = pd.DataFrame({"A": [1,2], "B": [3,4]})
print(df)
# Matplotlib
import matplotlib.pyplot as plt
plt.plot([1,2,3], [4,5,6])
plt.show()
4
End of Python Notes