python notes
python notes
---
- **What is Python?**
- **Installing Python**
- **Hello, World!**
```python
print("Hello, World!")
```
- **Data Types**
Integer (`int`), Floating-point (`float`), String (`str`), Boolean (`bool`), List, Tuple, Dictionary,
Set.
1
- **Declaring Variables**
```python
x = 10 # Integer
y = 3.14 # Float
```
```python
x = 10 + 5 # Addition
y=x*2 # Multiplication
z = 10 / 3 # Division (float)
```
- **If-Else Statement**
```python
age = 18
print("Adult")
else:
print("Minor")
```
2
- **Loops**
- **For Loop**
```python
print(i)
```
- **While Loop**
```python
count = 0
print(count)
count += 1
```
#### 4. **Functions**
- **Defining a Function**
```python
def greet(name):
print(f"Hello, {name}!")
greet("Bob")
```
3
- **Return Values**
```python
return a + b
result = add(3, 4)
print(result)
```
---
- **Lists**
```python
```
- **Tuples**
```python
4
coordinates = (4, 5)
```
- **Dictionaries**
```python
```
- **Sets**
```python
unique_numbers = {1, 2, 3, 3}
```
- **Reading a File**
```python
content = file.read()
print(content)
```
5
- **Writing to a File**
```python
file.write("Hello, World!")
```
- **Try-Except Block**
```python
try:
x = 10 / 0
except ZeroDivisionError:
```
- **Finally Block**
```python
try:
f = open("file.txt", "r")
except FileNotFoundError:
finally:
f.close()
```
6
---
```python
class Car:
self.brand = brand
self.model = model
def drive(self):
car1.drive()
```
- **Inheritance**
```python
class ElectricCar(Car):
7
super().__init__(brand, model)
self.battery_size = battery_size
def charge(self):
electric_car.charge()
```
#### 9. **Decorators**
- **Function Decorators**
```python
def decorator(func):
def wrapper():
func()
return wrapper
@decorator
def greet():
print("Hello!")
8
greet()
```
- **Class Decorators**
```python
def add_method(cls):
def new_method(self):
cls.new_method = new_method
return cls
@add_method
class MyClass:
pass
obj = MyClass()
obj.new_method()
```
- **Generators**
```python
def count_up_to(max):
count = 1
9
while count <= max:
yield count
count += 1
counter = count_up_to(5)
print(num)
```
- **Iterators**
```python
class Reverse:
self.data = data
self.index = len(data)
def __iter__(self):
return self
def __next__(self):
if self.index == 0:
raise StopIteration
self.index = self.index - 1
return self.data[self.index]
10
rev = Reverse("giraffe")
print(char)
```
- **Anonymous Functions**
```python
add = lambda x, y: x + y
print(add(5, 7))
```
---
- **Custom Metaclass**
```python
class MyMeta(type):
11
class MyClass(metaclass=MyMeta):
pass
obj = MyClass()
```
- **Threading**
```python
import threading
def print_numbers():
for i in range(5):
print(i)
thread = threading.Thread(target=print_numbers)
thread.start()
thread.join()
```
- **Asyncio**
```python
12
import asyncio
print("Hello")
await asyncio.sleep(1)
print("World")
asyncio.run(say_hello())
```
```python
import numpy as np
print(arr)
```
```python
import pandas as pd
print(df)
```
13
- **Matplotlib** (for plotting)
```python
plt.show()
```
- **Singleton Pattern**
```python
class Singleton:
_instance = None
def __new__(cls):
if cls._instance is None:
return cls._instance
```
---
14
- **Official Documentation**: [Python Docs](https://docs.python.org/)
- **Online Courses**:
- **Books**:
This roadmap should help you gradually progress from beginner to advanced expert in Python!
15