Python Crash Course for Machine
Learning
1. Introduction to Python
Python is widely used in Machine Learning because it is:
• Easy to learn
• Has many libraries for data science (NumPy, Pandas, Scikit-learn, Streamlit)
• Strong community support
2. Python Basics
Variables and Data Types
# Numbers
x = 10 # integer
y = 3.14 # float
# Strings
name = “Ada Lovelace”
# Boolean
is_ml_fun = True
Basic Operations
a, b = 5, 2
print(a + b) # 7
print(a ** b) # 25 (power)
print(a / b) # 2.5 (float division)
print(a // b) # 2 (integer division)
3. Data Structures
Lists
fruits = [“apple”, “banana”, “mango”]
print(fruits[0]) # "apple"
[Link](“orange”) # add item
Tuples
point = (3, 4)
Dictionaries
student = {“name”: “Ada”, “age”: 25, “score”: 95}
print(student["name"])
Sets
unique_numbers = {1, 2, 3, 3, 2}
print(unique_numbers) # {1, 2, 3}
4. Control Flow
If-Else
x = 20
if x > 10:
print(“Greater than 10”)
else:
print(“Not greater than 10”)
Loops
# For loop
for i in range(5):
print(i)
# While loop
count = 3
while count > 0:
print(count)
count -= 1
5. Functions
def square(num):
return num ** 2
print(square(5)) # 25
6. Classes (Object-Oriented Python)
class Student:
def __init__(self, name, score):
[Link] = name
[Link] = score
def show_info(self):
print(f”{[Link]} scored {[Link]}”)
s1 = Student(“Ada”, 95)
s1.show_info()
7. Working with Libraries
NumPy
import numpy as np
arr = [Link]([1, 2, 3, 4])
print([Link]()) # average
Pandas
import pandas as pd
data = {“Name”: [“Ada”, “Tunde”], “Score”: [95, 88]}
df = [Link](data)
print([Link]())
Matplotlib
import [Link] as plt
x = [1,2,3,4,5]
y = [2,4,6,8,10]
[Link](x, y)
[Link](“Line Plot”)
[Link]()
8. File Handling
# Write to file
with open(“[Link]”, “w”) as f:
[Link](“Hello, Python!”)
# Read from file
with open(“[Link]”, “r”) as f:
print([Link]())
9. Streamlit Essentials
Streamlit is a library for creating ML-powered web apps.
Installation
pip install streamlit
Minimal Streamlit App
import streamlit as st
[Link](“Hello, Streamlit!”)
name = st.text_input(“Enter your name:”)
if name:
[Link](f”Hello {name}”)
Run with:
streamlit run [Link]
Displaying Data
import pandas as pd
import streamlit as st
df = [Link]({“Name”: [“Ada”, “Tunde”], “Score”: [95, 88]})
[Link](df)
Charts
import [Link] as plt
import streamlit as st
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
[Link](x, y)
[Link](plt)
10. Machine Learning Starter
With scikit-learn:
from sklearn.linear_model import LinearRegression
import numpy as np
# Training data
X = [Link]([[1], [2], [3], [4]])
y = [Link]([2, 4, 6, 8])
# Model
model = LinearRegression()
[Link](X, y)
# Prediction
print([Link]([[5]])) # [10.]
Study Materials
• Python Programming Tutorials – Tech With Tim
• Python Full Course – Bro Code
• Pandas Tutorial – Keith Galli
• Pandas Tutorial – Tech With Tim