Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
281 changes: 280 additions & 1 deletion 00_python_colab/01_introduction_to_python/readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,280 @@
# [Lesson 01: Introduction to Python](https://colab.research.google.com/drive/1LqRMqVAff7jR_Xha7arxwHkFdQL08xAu?usp=sharing)
## 📌 Summary

This document covers essential Python concepts — from basics to how Python handles your code internally — with simple explanations, real-world examples, and best practices.

---

## 🔗 Useful Links

- [Download Python](https://www.python.org/downloads/)
- [Python 3.13.2 Documentation](https://docs.python.org/3/)
- [W3Schools.com – Python Tutorial](https://www.w3schools.com/python/)
- [Tutorialspoint.com – Python Programming](https://www.tutorialspoint.com/python/index.htm)
- [Visual Studio Code (VS Code)](https://code.visualstudio.com/)
- [Cursor IDE](https://cursor.sh/)

---

## 🐍 What is Python?

- Python is a **high-level**, **interpreted**, and **simple-to-read** programming language.
- Created by **Guido van Rossum**.
- Supports multiple programming styles:
- Procedural
- Object-Oriented
- Functional
- Huge library support and strong global community.
- Works on **Windows, Mac, and Linux**.
- Used for:
- Web development
- Data analysis
- AI and machine learning
- Automation
- Much more!

✅ Python is **easy** to learn and **extremely popular** worldwide.

---

## 🤖 Python in Agentic AI (Self-Acting AI)

Python plays a huge role in building **autonomous AI agents** (systems that think, decide, and act).

### Tools it supports:
- LangChain
- CrewAI
- Microsoft AutoGen
- Auto-GPT
- OpenAI APIs

### Common AI tasks:
- Natural Language Processing (NLP)
- Reinforcement Learning
- Intelligent Automation

✅ Python helps AI systems **interact, learn, and adapt**!

---

## 🛠️ Practical Applications of Python

Python is used for:

- **Data Science**: Data analysis, machine learning, visualization
- **Agentic AI**: Smart agents, chatbots, digital assistants
- **Machine Learning**: Predictive models, recommendation systems
- **Natural Language Processing (NLP)**: Text analysis, translation
- **Computer Vision**: Image and face recognition
- **Robotics**: Controlling drones and robots
- **Web Development**: Websites, APIs
- **AI & Deep Learning**: Neural networks, deep learning apps
- **Automation**: Scripts that save time
- **Scientific Computing**: Simulations, experiments
- **Cybersecurity**: Penetration testing
- **IoT (Internet of Things)**: Smart device programming

✅ Python is everywhere!

---

## 🧠 Python's Role in Agentic AI

- Python’s **simplicity** + **huge library support** = perfect combination for building intelligent, self-learning systems.

✅ It empowers developers to build **self-adapting**, **autonomous** agents.

---

## 🖥️ Code Execution Journey

Here’s how Python code travels:

```text
Source Code ➡️ Bytecode ➡️ Runtime Execution ➡️ Output
```

✅ It’s like a **chain reaction** — from writing to seeing the results!

---

## 🧩 What is Python Bytecode?

- After writing Python code, it **compiles into bytecode** (special low-level instructions).
- Then, the Python Interpreter executes this bytecode.

✅ **Bytecode is platform-independent**, meaning it can run anywhere Python is installed!

---

## 🔍 How Python Compiles Code

1. **Lexical Analysis**: Breaks your code into pieces (tokens).
2. **Syntax Analysis**: Checks the structure.
3. **Semantic Analysis**: Ensures logical sense.
4. **Bytecode Generation**: Creates bytecode instructions.

---

## 🧾 Example: Python Code and Bytecode

```python
class Person:
def __init__(self, name: str, age: int):
self.name = name
self.age = age

def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")

person = Person("Arif Rozani", 20)
person.greet()
```

✅ Behind the scenes, Python **translates** this into **bytecode**!

---

## 🔍 Seeing Bytecode Using `dis` Module

```python
import dis
dis.dis(Person)
```

✅ `dis` shows the **low-level steps** your code follows, like the **__init__** and **greet** methods.

---

## 🧠 Why is Python Bytecode Important?

- **Platform Independent**: Runs anywhere.
- **Dynamic Typing**: Types decided at runtime.
- **Flexibility**: Easy code updates and changes.

✅ Bytecode makes Python powerful and easy to run!

---

## ⚙️ How Python Uses Bytecode

| Step | Description |
|:---|:---|
| Compilation | Python compiles `.py` to `.pyc` bytecode |
| Execution | Python Virtual Machine (PVM) runs bytecode |
| Caching | Saves compiled bytecode in `__pycache__` for faster loading |

---

## 📂 Can Bytecode Run on Any OS?

✅ Yes!
But you must have:

- The correct **Python version** installed.
- (.pyc files aren't backward compatible, e.g., 3.10 ➡️ 3.8 won't work.)

---

## 🚀 Can You Run Bytecode Directly?

❌ No.
You **still need the Python interpreter** (like **CPython**) to run `.pyc` files.

✅ Different from Java bytecode which runs inside a JVM.

---

## 🛠️ How to Run Bytecode Manually

### 1. Compile Python file

```bash
python -m compileall TestP.py
```

✅ Creates a `.pyc` file inside a `__pycache__` folder.

---

### 2. Run Bytecode

Navigate into `__pycache__` and run:

```bash
python TestP.cpython-312.pyc
```

✅ You are now running **compiled bytecode**!

---

## 📏 Indentation in Python

### What is Indentation?

- **Spaces or tabs** at the start of a line.
- Groups lines into **blocks** of code.

---

### Why is Indentation Important?

- Defines **structure**.
- Prevents **errors**.
- Makes code **readable**.

---

### Rules of Indentation

- Use **only spaces** or **only tabs** (never mix).
- Standard: **4 spaces** per level.
- After a colon `:`, always indent the next line.
- Inside functions, loops, classes — **always indent**.

---

### Examples

✅ Correct:

```python
if True:
print("Hello")
print("World")
```

❌ Incorrect:

```python
if True:
print("Hello")
print("World")
```

✅ Correct Function:

```python
def greet(name: str):
print("Hello, " + name + "!")
```

---

## 📋 Best Practices for Indentation

- Use editors like **VS Code** or **Cursor** with **auto-indentation**.
- Prefer **spaces** over **tabs**.
- Stay **consistent** across your project.

---

## 📝 Exercise

✅ Write a simple Python program using correct indentation:

```python
if True:
print("This is indented properly!")
```

Loading