Detailed Python Notes
1. Introduction to Python
- Python is an interpreted, high-level, general-purpose programming language created by Guido
van Rossum in 1991.
- Emphasizes code readability and simplicity.
- Widely used in web development, data science, AI, machine learning, automation, etc.
- Python is platform-independent and open-source.
2. Data Types and Variables
- Numbers: int, float, complex
- Strings: Immutable sequences of Unicode characters
- Boolean: True/False
- Lists: Ordered, mutable collections
- Tuples: Ordered, immutable collections
- Sets: Unordered collections of unique elements
- Dictionaries: Key-value pairs
Example:
x = 10 # int
y = 3.14 # float
name = "Alice" # string
items = [1, 2, 3] # list
3. Operators
- Arithmetic: +, -, *, /, //, %, **
- Comparison: ==, !=, >, <, >=, <=
- Logical: and, or, not
- Membership: in, not in
- Identity: is, is not
4. Conditional Statements
Example:
if age >= 18:
print("Adult")
elif age > 12:
print("Teen")
else:
print("Child")
5. Loops
- for loop: Iterates over sequences
Example:
for i in range(5):
print(i)
- while loop: Repeats until condition is False
Example:
count = 0
while count < 5:
print(count)
count += 1
6. Functions
- Defined using def keyword
- Can have parameters and return values
- Supports default arguments, keyword arguments, variable-length arguments
Example:
def greet(name="User"):
return f"Hello, {name}"
7. Collections in Detail
- List Methods: append(), remove(), sort(), reverse()
- Tuple: Immutable, used for fixed collections
- Set: union(), intersection(), difference()
- Dictionary: keys(), values(), items()
8. File Handling
f = open("[Link]", "w")
[Link]("Hello World")
[Link]()
with open("[Link]", "r") as f:
data = [Link]()
print(data)
9. Object-Oriented Programming (OOP)
- Class: Blueprint for objects
- Object: Instance of a class
- Inheritance: Deriving new classes from existing ones
- Polymorphism: Different forms of the same method
- Encapsulation: Restricting access to data
Example:
class Person:
def __init__(self, name):
[Link] = name
def greet(self):
print("Hello, my name is", [Link])
10. Modules and Packages
- Module: A Python file with functions/classes
- Package: A collection of modules with __init__.py
Example:
import math
print([Link](16))
11. Exception Handling
try:
num = int("abc")
except ValueError as e:
print("Error:", e)
finally:
print("Execution finished")
12. Advanced Topics
- List Comprehensions: [x**2 for x in range(5)]
- Generators: yield keyword for lazy evaluation
- Decorators: Functions that modify other functions
- Lambda Functions: Anonymous functions
- Iterators & Iterables: __iter__(), __next__()
13. Libraries and Frameworks
- Data Science: NumPy, Pandas, Matplotlib, Seaborn
- Machine Learning: Scikit-learn, TensorFlow, PyTorch
- Web Development: Flask, Django, FastAPI
- Automation: Selenium, PyAutoGUI
- GUI: Tkinter, PyQt
14. Virtual Environments
- Create virtual environment: python -m venv env
- Activate (Windows): env\Scripts\activate
- Activate (Linux/Mac): source env/bin/activate
15. Best Practices
- Use meaningful variable names
- Follow PEP 8 style guide
- Comment code properly
- Write modular and reusable code
- Use version control (Git)