Python Programming Basics
Python is a high-level, interpreted programming language known for its simplicity and
readability. It's widely used in web development, data science, AI, automation, and more.
1. **Indentation:** Python uses indentation (whitespace) to define code blocks, unlike other
languages that use braces. This makes code cleaner but requires consistency.
2. **Variables and Data Types:** Variables in Python are dynamically typed. Common data
types include int, float, str, list, tuple, dict, set, and bool.
3. **Functions:** Defined using the `def` keyword. Functions help organize code into
reusable blocks.
Example:
```python
def greet(name):
return f"Hello, {name}!"
```
4. **Control Flow:** Python supports standard control flow statements like `if`, `elif`, `else`,
`for`, and `while`.
5. **Data Structures:** Lists (mutable), Tuples (immutable), Dictionaries (key-value pairs),
and Sets (unordered, unique elements).
6. **List Comprehension:** Concise way to create lists.
Example: `[x**2 for x in range(5)]`
7. **Error Handling:** Use try-except blocks to catch exceptions.
Example:
```python
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
```
8. **Modules and Libraries:** Python has a rich ecosystem of libraries (NumPy, Pandas,
Matplotlib, Requests, etc.) that can be imported using `import` keyword.
9. **OOP in Python:** Python supports Object-Oriented Programming. Define classes using
`class` keyword and create objects using constructors.
Example:
```python
class Person:
def __init__(self, name):
self.name = name
```
10. **File Handling:** Python can read/write files using `open()`.
Example:
```python
with open("file.txt", "r") as file:
content = file.read()
```