Python Mastery Notes
1. Basics of Python
- Python is a high-level, interpreted programming language.
- No need to declare data types (dynamic typing).
- Example:
x=5
print(x)
2. Control Statements
- Used for decision-making and loops.
- if, if-else, elif, for, while
Example:
if x > 0:
print("Positive")
3. Functions
- A function is a block of code which runs only when it is called.
Example:
def greet():
print("Hello")
greet()
4. Data Structures
- List: Ordered, changeable
my_list = [1, 2, 3]
- Tuple: Ordered, unchangeable
my_tuple = (1, 2, 3)
Python Mastery Notes
- Set: Unordered, unique items
my_set = {1, 2, 3}
- Dictionary: Key-value pairs
my_dict = {'a': 1, 'b': 2}
5. Object-Oriented Programming
- Class and Object: class is blueprint; object is instance.
- self: Refers to the object.
- Inheritance, Encapsulation, Polymorphism
Example:
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello", self.name)
6. File Handling
- Used to read/write files.
- open(), read(), write(), close()
Example:
f = open("file.txt", "r")
print(f.read())
f.close()
7. Modules and Packages
- Module: A file with Python code (.py)
- Importing modules:
Python Mastery Notes
import math
print(math.sqrt(16))
- Package: A folder with __init__.py and modules.
8. Exception Handling
- Try-except block to handle errors.
Example:
try:
x=1/0
except ZeroDivisionError:
print("Cannot divide by zero")
9. Advanced Concepts
- Lambda: Anonymous function
square = lambda x: x * x
- map(), filter(), reduce()
- List Comprehension:
[x for x in range(5) if x % 2 == 0]
10. Popular Libraries
- pandas: Data manipulation
- matplotlib: Data visualization
- tkinter: GUI applications
- requests: API calls
11. Project Ideas
Python Mastery Notes
- Calculator (GUI)
- To-do List App
- Weather App using API
- Web Scraper
- File Organizer
12. Interview Questions
1. What are Python data types?
2. Explain difference between list and tuple.
3. What is inheritance?
4. How to handle exceptions?
5. What is lambda function?