Python_Programming_Notes
Python_Programming_Notes
3. Data Structures
Lists:
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits[1]) # banana
Tuples:
coordinates = (10, 20)
print(coordinates[0])
Sets:
unique_items = {1, 2, 3, 3}
unique_items.add(4)
Dictionaries:
person = {"name": "Alice", "age": 25}
print(person["name"])
person["age"] = 26
4. Operators
Arithmetic: + - * / % // **
Comparison: == != > < >= <=
Logical: and or not
Assignment: = += -= *= /=
Membership: in, not in
Identity: is, is not
5. Control Flow
while loops:
x=0
while x < 5:
print(x)
x += 1
6. Functions
def greet(name):
return "Hello " + name
print(greet("Amantle"))
Lambda:
square = lambda x: x * x
print(square(5))
def drive(self):
print(f"{self.brand} is driving")
my_car = Car("Toyota")
my_car.drive()
Inheritance:
class ElectricCar(Car):
def charge(self):
print("Charging...")
e_car = ElectricCar("Tesla")
e_car.drive()
e_car.charge()
8. Error Handling
try:
x=1/0
except ZeroDivisionError:
print("Cannot divide by zero!")
finally:
print("Done")
9. File Handling
# Write
with open("file.txt", "w") as f:
f.write("Hello File")
# Read
with open("file.txt", "r") as f:
content = f.read()
print(content)
import math
print(math.sqrt(16))
len(), type(), str(), int(), float(), sum(), min(), max(), input(), print()
Generators:
def count_up_to(n):
i=0
while i <= n:
yield i
i += 1
Decorators:
def decorator(func):
def wrapper():
print("Before function")
func()
print("After function")
return wrapper
@decorator
def greet():
print("Hello")
greet()