Python Programming Basics
1. Introduction
- Python is a beginner-friendly, high-level programming language.
- Widely used in web development, data science, AI, automation.
2. Syntax & Data Types
- Variables: x = 10, name = "Alice"
- Data Types: int, float, str, bool, list, tuple, dict, set
Example:
age = 21
fruits = ["apple", "banana"]
student = {"name": "Riya", "id": 101}
3. Control Flow
- if, elif, else
if age > 18:
print("Adult")
else:
print("Minor")
- for loops: for i in range(5): print(i)
- while loops: while x > 0: x -= 1
4. Functions
- Defined with def keyword.
def greet(name):
return "Hello " + name
print(greet("Suha"))
5. Collections
- List: Ordered, mutable → [1,2,3]
- Tuple: Ordered, immutable → (1,2,3)
- Dictionary: Key-value pairs → {"name":"Ali","age":20}
- Set: Unique elements → {1,2,3}
6. File Handling
with open("data.txt","r") as f:
content = f.read()
7. Error Handling
try:
x = 5/0
except ZeroDivisionError:
print("Cannot divide by zero")
8. Modules & Libraries
- import math, import random
- NumPy, Pandas, Matplotlib, Scikit-learn
9. Best Practices
- Use meaningful variable names.
- Follow PEP8 style guide.
- Comment your code.
- Break code into reusable functions.
Quick Tip: Practice small coding exercises daily to improve!