Python is one of the most widely used programming languages for artificial intelligence, machine learning, and deep learning.
Its simple syntax and libraries such as scikit-learn and TensorFlow make it a good starting point for beginners.
In this guide, we will look at two short examples: one for machine learning and one for deep learning.
Why Python for AI?
Python provides libraries that handle much of the complex mathematics behind AI models.
For beginners, this means you can focus on understanding the basic process:
Data → Model → Training → Prediction → Evaluation
You can run these examples in Google Colab, Jupyter Notebook, or a local Python environment.
Example 1: Machine Learning with Python
We can use the well-known Iris dataset to train a simple K-Nearest Neighbours (k-NN) classification model. The dataset contains 150 flower samples belonging to three classes.
# Import the Iris dataset
from sklearn.datasets import load_iris
# Import the function used to split data into training and testing sets
from sklearn.model_selection import train_test_split
# Import the k-NN classification model
from sklearn.neighbors import KNeighborsClassifier
# Load the Iris features (X) and target classes (y)
X, y = load_iris(return_X_y=True)
# Split the dataset into 80% training data and 20% testing data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Create a k-NN model using 3 nearest neighbours
model = KNeighborsClassifier(n_neighbors=3)
# Train the model using the training data
model.fit(X_train, y_train)
# Evaluate the model using the testing data
print("Accuracy:", model.score(X_test, y_test))
What happens here?
The dataset is loaded and divided into training data and testing data.
The k-NN model learns from the training examples and then predicts the classes of flowers it has not seen before.
Finally, model.score() measures how many test examples were classified correctly.
This demonstrates a basic machine-learning workflow:
Load Data → Split Data → Train → Test
Example 2: Deep Learning with Python
For deep learning, we can use TensorFlow/Keras to create a small neural network that recognises handwritten numbers.
TensorFlow's beginner workflow follows the same general process: load data, build a neural network, train it, and evaluate its accuracy.
# Import TensorFlow
import tensorflow as tf
# Load the MNIST training and testing datasets
(x_train, y_train), (x_test, y_test) = \
tf.keras.datasets.mnist.load_data()
# Scale pixel values from 0–255 to 0–1
x_train = x_train / 255.0
x_test = x_test / 255.0
# Build a simple neural network
model = tf.keras.Sequential([
# Define the input image shape
tf.keras.Input(shape=(28, 28)),
# Convert each 28×28 image into a one-dimensional vector
tf.keras.layers.Flatten(),
# Add a hidden layer with 64 neurons
tf.keras.layers.Dense(64, activation="relu"),
# Add the output layer for the 10 digit classes
tf.keras.layers.Dense(10, activation="softmax")
])
# Configure how the neural network will learn
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
# Train the neural network for 3 epochs
model.fit(x_train, y_train, epochs=3)
# Evaluate the trained model using the test dataset
model.evaluate(x_test, y_test)
What happens here?
The MNIST dataset contains images of handwritten numbers from 0 to 9.
The neural network receives the images, learns patterns during training, and then tries to identify the number shown in new images.
The final evaluation shows how accurately the model performs on test data.
Machine Learning vs Deep Learning in Python
The first example uses k-NN, a classical machine-learning algorithm.
The second uses a multi-layer neural network, which is a deep-learning approach.
The basic idea remains similar:
Data → Training → Model → Prediction
The main difference is that deep-learning models can learn more complex patterns using multiple neural-network layers.
What Should Beginners Learn Next?
After understanding these examples, beginners can gradually explore:
- Pandas for working with data
- Matplotlib for visualisation
- scikit-learn for machine learning
- TensorFlow or PyTorch for deep learning
There is no need to learn everything at once. Start with small datasets and simple models, then gradually move to more complex projects.
Final Thoughts
Python makes it possible to build simple AI models with only a small amount of code.
The most important goal for beginners is not memorising every command, but understanding the process behind the model.
At AIWiseUp, we’ll continue turning AI concepts into practical, beginner-friendly examples.
Wise up to AI. Learn it. Use it. Grow with it.
References and Further Reading
Scikit-learn Developers (2026) Iris dataset. scikit-learn documentation.
Scikit-learn Developers (2026) Nearest Neighbors Classification. scikit-learn documentation.
Nearest Neighbors Classification – scikit-learn
TensorFlow (2026) TensorFlow 2 Quickstart for Beginners. TensorFlow documentation.