Full Detailed Python Notes
Full Detailed Python Notes
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.
print("Hello, World!")
# Comments
# This is a single-line comment
'''
This is
a multi-line
comment
'''
# Data Types:
a = 10 # int
Full Detailed Python Notes
b = 3.14 # float
c = "Python" # str
d = True # bool
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)
Chapter 4: Functions
def greet(name):
print("Hello", name)
greet("K.T.")
print(add(5, 3))
# 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"])
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()
try:
x = 5 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Done")
# Writing to a file
with open("sample.txt", "w") as f:
f.write("Hello, File!")
import math
print(math.sqrt(16))
# 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))