Full Detailed Python Notes
Chapter 1: Introduction to Python
Python is a high-level, interpreted, interactive, and object-oriented programming language.
Developed by Guido van Rossum and first released in 1991.
Python is known for its simple syntax, readability, and versatility.
Features:
- Easy to learn and use.
- Open source and free.
- Interpreted language.
- Extensive standard library.
- Dynamically typed.
- Portable and platform-independent.
- Supports procedural, object-oriented, and functional programming.
Python Syntax Example:
Chapter 2: Python Basics Code
print("Hello, World!")
# Comments
# This is a single-line comment
'''
This is
a multi-line
comment
'''
# Variables and Data Types
x = 10
name = "K.T."
pi = 3.14
# Data Types:
a = 10 # int
Full Detailed Python Notes
b = 3.14 # float
c = "Python" # str
d = True # bool
Arithmetic Operators: +, -, *, /, //, %, **
Relational Operators: ==, !=, >, <, >=, <=
Logical Operators: and, or, not
Assignment Operators: =, +=, -=, *=, /=
Bitwise Operators: &, |, ^, ~, <<, >>
Chapter 3: Control Structures
if-elif-else example:
x = 5
if x > 0:
print("Positive")
elif x < 0:
print("Negative")
else:
print("Zero")
# Loops
# while loop
i = 1
while i <= 5:
print(i)
i += 1
# for loop
for i in range(1, 6):
print(i)
# Loop Control Statements
# break, continue, pass
Chapter 4: Functions
Defining and using functions in Python:
Full Detailed Python Notes
def greet(name):
print("Hello", name)
greet("K.T.")
def add(a, b):
return a + b
print(add(5, 3))
# Lambda function example
square = lambda x: x * x
print(square(5))
Chapter 5: Data Structures
List, Tuple, Set, Dictionary examples:
# List
lst = [1, 2, 3, "Python"]
print(lst[0])
lst.append(5)
# Tuple
tup = (1, 2, 3)
print(tup[1])
# Set
s = {1, 2, 3}
s.add(4)
# Dictionary
d = {"name": "K.T.", "age": 18}
print(d["name"])
Chapter 6: Object-Oriented Programming (OOP)
Classes and objects, inheritance examples:
class Student:
def __init__(self, name):
self.name = name
Full Detailed Python Notes
def show(self):
print("Student name:", self.name)
s = Student("K.T.")
s.show()
class Animal:
def sound(self):
print("Animal sound")
class Dog(Animal):
def sound(self):
print("Bark")
d = Dog()
d.sound()
Chapter 7: Exception Handling
Handling errors gracefully using try-except-finally blocks:
try:
x = 5 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Done")
Chapter 8: File Handling
Reading from and writing to files in Python:
# Writing to a file
with open("sample.txt", "w") as f:
f.write("Hello, File!")
# Reading from a file
with open("sample.txt", "r") as f:
print(f.read())
Full Detailed Python Notes
Chapter 9: Modules and Packages
Using built-in and custom modules:
import math
print(math.sqrt(16))
# Custom module example (mymodule.py)
def greet():
print("Hello from module")
# Importing custom module
import mymodule
mymodule.greet()
Chapter 10: Built-in Functions
Important Python built-in functions:
- len(), type(), input(), int(), float(), str(), list(), dict(), set(), range(), enumerate(), zip(), map(), filter(), reduce()
List comprehensions, decorators, and generators:
Chapter 11: Advanced Topics Code
# List Comprehension
squares = [x*x for x in range(1, 6)]
print(squares)
# Decorators
def decorator(func):
def wrapper():
print("Before")
func()
print("After")
return wrapper
@decorator
def hello():
print("Hello")
Full Detailed Python Notes
hello()
# Generators
def gen():
yield 1
yield 2
g = gen()
print(next(g))
print(next(g))