Skip to content

Latest commit

 

History

History
280 lines (184 loc) · 6.08 KB

File metadata and controls

280 lines (184 loc) · 6.08 KB

📌 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


🐍 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:

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

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

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

python -m compileall TestP.py

✅ Creates a .pyc file inside a __pycache__ folder.


2. Run Bytecode

Navigate into __pycache__ and run:

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:

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

❌ Incorrect:

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

✅ Correct Function:

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:

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