0% found this document useful (0 votes)
11 views40 pages

Python Fundamentals

The document outlines a comprehensive workshop on Python programming, covering topics from basics to advanced concepts including control flow, data structures, object-oriented programming, and essential libraries for data science and machine learning. It also introduces generative AI and agentic AI, explaining their functionalities and applications. The workshop aims to equip students with the skills necessary to become proficient in Python and AI engineering.

Uploaded by

Akshay Arora
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views40 pages

Python Fundamentals

The document outlines a comprehensive workshop on Python programming, covering topics from basics to advanced concepts including control flow, data structures, object-oriented programming, and essential libraries for data science and machine learning. It also introduces generative AI and agentic AI, explaining their functionalities and applications. The workshop aims to equip students with the skills necessary to become proficient in Python and AI engineering.

Uploaded by

Akshay Arora
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python

From Basics to Generative AI & Beyond

A Complete Workshop for Students

Beginner → Intermediate → Advanced → AI Engineer


What We'll Cover Today

2. Control Flow &


1. Python Basics 3. Data Structures 4. OOP & File I/O
Functions
Variables, types, Loops, conditionals, Lists, dicts, tuples, sets, Classes, objects,
operators, I/O functions, modules comprehensions exceptions, file handling
Why Python Rules the World

~$120
#1 95% 3.5M+
k
Most Popular Language of ML/AI Projects Open-source Libraries Avg. Annual Salary
(Stack Overflow 2024) use Python on PyPI for Python Developers
(US)
§1
SECTION
Python Foundations

Let's write your very first lines of Python code.


Setting Up Python

• Download Python 3.12+ from [Link] — tick 'Add to PATH'


• Install VS Code + Python extension (or use Jupyter Notebook online)
• Run your first command: python --version in the terminal
• For beginners: try Google Colab — zero setup, runs in browser
• Virtual environments: python -m venv myenv (keeps projects clean)
• Package manager: pip install <package> installs anything you need
Variables & Data Types

name = 'Alice' # str – text python


age = 22 # int – whole number
gpa = 9.1 # float – decimal
active = True # bool – True or False

# Python infers the type (dynamic typing)


print(type(name)) # <class 'str'>
print(f'Hi {name}, you are {age} years old!')

💡 f-strings let you embed variables directly inside strings — very handy!
Operators in Python

Arithmetic & Comparison Logical & Special

• + Addition, - Subtraction • and, or, not (logical ops)


• * Multiply, / Divide (float) • in — membership: 'a' in 'cat' → True
• // Floor divide, % Modulus • is — identity: checks same object
• ** Power (2**10 = 1024) • Ternary: x = 5 if a>0 else -5
• == Equal, != Not equal • Augmented: x += 1 same as x = x+1
• > > < >= <= Comparisons • Walrus: if (n := len(data)) > 10: ...
User Input & Output

# Taking input from the user python


name = input('Enter your name: ')
age = int(input('Enter your age: ')) # cast to int

# Formatted output
print(f'Hello {name}! Next year you will be {age+1}.')
print('Score: {:.2f}'.format(98.567)) # → Score: 98.57

💡 input() always returns a string — remember to convert with int() or float().


§2
SECTION
Control Flow & Functions

Teaching Python how to make decisions and repeat tasks.


If / Elif / Else

score = 85 python

if score >= 90:


print('A grade')
elif score >= 75:
print('B grade') # This runs!
else:
print('Below B')

# One-liner
grade = 'Pass' if score >= 50 else 'Fail'

💡 Python uses indentation (4 spaces) instead of curly braces — keep it consistent!


Loops: for & while

# for loop — iterate over a sequence python


for i in range(5): # 0 1 2 3 4
print(i, end=' ')

# while loop — runs until condition is False


count = 0
while count < 3:
print('tick', count)
count += 1

# Loop over a list


fruits = ['apple','banana','cherry']
for fruit in fruits:
print([Link]())

💡 Use break to exit early, continue to skip to next iteration.


Defining & Calling Functions

def greet(name, greeting='Hello'): # default arg python


return f'{greeting}, {name}!'

print(greet('Alice')) # Hello, Alice!


print(greet('Bob', 'Hey')) # Hey, Bob!

# *args and **kwargs


def add(*nums):
return sum(nums)

print(add(1, 2, 3, 4)) # 10

💡 Functions are reusable blocks — write once, call many times.


Lambdas & List Comprehensions

# Lambda — anonymous one-liner function python


square = lambda x: x ** 2
print(square(7)) # 49

# List comprehension — elegant & fast


evens = [x for x in range(20) if x % 2 == 0]
squared = [x**2 for x in range(10)]

# Dict comprehension
word_len = {w: len(w) for w in ['cat','elephant','bee']}

💡 List comprehensions are up to 35% faster than equivalent for-loops.


§3
SECTION
Data Structures

Python's built-in containers are powerful and flexible.


Lists & Tuples

# LIST — mutable, ordered python


nums = [3, 1, 4, 1, 5, 9]
[Link](2) # add to end
[Link]() # sort in place
print(nums[0:3]) # slice: [1, 1, 3]

# TUPLE — immutable, ordered (great for fixed data)


point = (10.5, 20.3) # x, y coordinate
x, y = point # unpacking!
print(x) # 10.5

💡 Use tuples when data should not change — they're also faster than lists.
Dictionaries & Sets

# DICT — key:value pairs, O(1) lookup python


student = {'name':'Arjun', 'gpa':9.2, 'year':3}
student['major'] = 'CS' # add key
print([Link]('gpa', 0)) # safe access → 9.2

for key, val in [Link]():


print(f'{key}: {val}')

# SET — unique elements, fast membership test


tags = {'python', 'ml', 'python', 'ai'} # {'python','ml','ai'}
print('ml' in tags) # True

💡 Dictionaries are everywhere in real Python code — master them well.


§4
SECTION
OOP & File Handling

Write cleaner code with objects, exceptions and files.


Object-Oriented Programming

class Animal: python


def __init__(self, name, sound):
[Link] = name
[Link] = sound

def speak(self):
return f'{[Link]} says {[Link]}!'

class Dog(Animal): # inheritance


def fetch(self):
return f'{[Link]} fetches the ball!'

d = Dog('Rex', 'Woof')
print([Link]()) # Rex says Woof!

💡 OOP organises code into reusable blueprints — classes model real-world things.
Exceptions & File I/O

# Try / except — handle errors gracefully python


try:
result = 10 / int(input('Divisor: '))
print(result)
except ZeroDivisionError:
print('Cannot divide by zero!')
except ValueError as e:
print(f'Invalid input: {e}')
finally:
print('Always runs.')

# File reading with context manager


with open('[Link]', 'r') as f:
content = [Link]()

💡 The with statement auto-closes files even if an error occurs.


§5
SECTION
Essential Libraries

NumPy · Pandas · Matplotlib — the data science holy trinity.


NumPy — Fast Numerical Computing

import numpy as np python

a = [Link]([1, 2, 3, 4, 5])
print(a * 2) # [2 4 6 8 10] — vectorised!

matrix = [Link]((3,3)) # 3x3 matrix of zeros


eye = [Link](4) # 4x4 identity matrix

# Stats in one line


data = [Link](1000) # 1000 random values
print([Link](), [Link]())

💡 NumPy operations run in C under the hood — 100x faster than Python loops.
Pandas — Data Manipulation

import pandas as pd python

df = pd.read_csv('[Link]')
print([Link]()) # first 5 rows
print([Link]()) # stats summary

# Filter, group, aggregate


toppers = df[df['gpa'] > 9.0]
avg_gpa = [Link]('dept')['gpa'].mean()

# Handle missing data


[Link]([Link](), inplace=True)
[Link](subset=['name'], inplace=True)

💡 Pandas is your spreadsheet on steroids — essential for any data work.


Matplotlib & Seaborn — Visualisation

import [Link] as plt python


import seaborn as sns

x = [1,2,3,4,5]
y = [2,4,1,6,3]

[Link](x, y, marker='o', color='purple')


[Link]('My First Plot')
[Link]('X Axis'); [Link]('Y Axis')
[Link]()

# Seaborn — beautiful statistical plots in 1 line


[Link]([Link](), annot=True, cmap='coolwarm')

💡 Seaborn wraps matplotlib with stunning defaults — great for EDA.


§6
SECTION
Machine Learning

Teaching computers to learn from data — not explicit rules.


Types of Machine Learning

Unsupervised Reinforcement Self-Supervised /


Supervised Learning
Learning Learning SSL
Labeled data. Learns No labels. Finds hidden Agent learns via reward Creates its own labels
input→output mapping. patterns & clusters. & punishment. from raw data.
Examples: regression, Examples: K-Means, Examples: game AI, Basis of modern LLMs
classification. PCA, autoencoders. robotics, AlphaGo. like GPT and BERT.
The ML Workflow — Step by Step

• 1⃣ Define the problem — classification? regression? clustering?


• 2⃣ Collect & explore data — EDA with Pandas & Matplotlib
• 3⃣ Preprocess — handle nulls, encode categories, normalise features
• 4⃣ Split — train (70%) / validation (15%) / test (15%)
• 5⃣ Choose a model — start simple, then go complex
• 6⃣ Train the model — [Link](X_train, y_train)
• 7⃣ Evaluate — accuracy, F1, confusion matrix, RMSE
• 8⃣ Tune — GridSearchCV, RandomSearch, or Optuna
• 9⃣ Deploy — Flask/FastAPI API, Docker, cloud (AWS/GCP/Azure)
Scikit-learn — ML in 6 Lines

from [Link] import load_iris python


from sklearn.model_selection import train_test_split
from [Link] import RandomForestClassifier
from [Link] import accuracy_score

X, y = load_iris(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2)

model = RandomForestClassifier(n_estimators=100)
[Link](X_tr, y_tr)

print(accuracy_score(y_te, [Link](X_te))) # ~0.97

💡 Scikit-learn's consistent API means every model works the same way.
Neural Networks — How They Work

• Inspired by the human brain — networks of interconnected neurons


• Input Layer → Hidden Layers → Output Layer
• Each neuron: output = activation(weights · inputs + bias)
• Training: backpropagation adjusts weights to minimise a loss function
• Activation functions: ReLU (hidden layers), Sigmoid/Softmax (output)
• Optimiser: Adam (Adaptive Moment Estimation) is the standard choice
• Key hyperparameters: learning rate, batch size, epochs, architecture
• Deep Learning = many hidden layers → learns increasingly abstract features
Deep Learning with Keras / TensorFlow

import tensorflow as tf python


from tensorflow import keras

model = [Link]([
[Link](128, activation='relu', input_shape=(784,)),
[Link](0.2),
[Link](64, activation='relu'),
[Link](10, activation='softmax') # 10 classes
])

[Link](optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
[Link](X_train, y_train, epochs=10, batch_size=32)

💡 Keras makes neural networks readable — even for beginners.


§7
SECTION
Generative AI & LLMs

The technology behind ChatGPT, Gemini, and Claude.


What Is Generative AI?

• AI that can create new content — text, images, code, audio, video
• Powered by Large Language Models (LLMs) trained on vast text corpora
• Transformer architecture (2017, 'Attention Is All You Need') is the core
• Learns probability distributions over tokens (words/sub-words)
• Generation = predicting the next most likely token, one at a time
• Key models: GPT-4, Claude 3, Gemini Ultra, Llama 3, Mistral
• Applications: code generation, summarisation, Q&A, translation, agents
• Multimodal models can see images, hear audio, and generate all of these
Inside a Transformer — Key Concepts

🔤 Tokenisation 📐 Embeddings 👁 Attention 🔁 Feed-Forward

Text split into sub-word Tokens mapped to Model learns which Dense layers process
tokens. high-dim vectors. tokens to focus on. each token.
'unbelievable' → Semantic meaning Enables long-range Applied identically at
['un','believ','able'] encoded as numbers. dependency capture. every position.
Prompt Engineering — Getting the Best Results

Best Practices Examples & Tips

• Be specific — vague prompts give vague answers • ❌ 'Write code'


• Give context — who are you, what is the task? • ✅ 'Write a Python function that takes a list of
• Use examples (few-shot) — 'Here are 2 integers and returns the top-k values, with type
examples…' hints and docstring'
• Chain-of-Thought: 'Think step by step' • Use temperature=0 for deterministic outputs
• Define the output format — JSON, table, bullet • Iterate — treat prompts like code, test & refine
list • System prompt vs user prompt — understand
• Assign a role: 'You are a senior Python both
engineer…' • RAG = Retrieval Augmented Generation for
facts
Calling LLM APIs with Python

from anthropic import Anthropic # pip install anthropic python

client = Anthropic() # uses ANTHROPIC_API_KEY env var

message = [Link](
model = 'claude-opus-4-6',
max_tokens = 1024,
messages = [
{'role':'user', 'content':'Explain backpropagation simply.'}
]
)

print([Link][0].text)

💡 Swap Anthropic for openai, [Link], or ollama for local models.


§8
SECTION
Agentic AI

Autonomous AI that plans, uses tools, and gets things done.


What Is an AI Agent?

• An agent perceives its environment, reasons, and takes actions


• Core loop: Observe → Think → Act → Observe → … (ReAct pattern)
• Tools: web search, code execution, database queries, API calls
• Memory: in-context (conversation), external (vector DB, SQL)
• Planning: break big goals into sub-tasks, execute step by step
• Multi-agent systems: specialised agents collaborate on complex tasks
• Frameworks: LangChain, LlamaIndex, CrewAI, AutoGen, Anthropic's MCP
• Safety: human-in-the-loop, permission gates, sandboxed execution
Building a Simple Tool-Using Agent

# Minimal agent using Anthropic tool_use python


tools = [{
'name': 'web_search',
'description': 'Search the web for current information',
'input_schema': {'type':'object',
'properties': {'query': {'type':'string'}},
'required': ['query']}
}]

response = [Link](
model='claude-opus-4-6', max_tokens=1024, tools=tools,
messages=[{'role':'user', 'content':'Latest AI news today?'}]
)
# Check if model wants to use a tool, execute it, loop back

💡 Agents loop: LLM decides → tool runs → result fed back → LLM continues.
ML vs Agentic AI — The Distinction

🧠 Machine Learning 🤖 Agentic AI

• Learn from data, optimise a metric • Use pre-trained models as reasoning engines
• Supervised, unsupervised, RL paradigms • Orchestrate tools, memory, and planning
• Model training: data → loss → backprop • No training: prompt design & tool integration
• Inference: fixed function, one call • Inference: multi-step, dynamic decisions
• Tools: Scikit-learn, PyTorch, TF, JAX • Tools: LangChain, CrewAI, MCP, vector DBs
• Career: ML Engineer, Research Scientist • Career: AI Engineer, Agent Developer
• Skills: Maths, statistics, experimentation • Skills: Systems thinking, API integration

Software is eating the world. AI is eating software. Python is the
fork.

— Adapted from Marc Andreessen (2011) — updated for 2025


What's Your Path?
I'd love to know — where do you want to go from here?

🤖 Agentic AI 🧠 Machine Learning 📊 Data Science ⚙ Backend Dev

Build autonomous AI Supervised / unsupervised EDA, statistics, APIs with FastAPI/Django,


agents, tool-using LLMs, learning, neural nets, model visualisation, storytelling databases, cloud
multi-agent pipelines training & evaluation with data, dashboards deployment, microservices

Raise your hand or shout it out!

You might also like