Python Basics for AI/ML Learning

Introduction

Today's focus: Getting clear on the Python essentials for AI/ML.

While Python is a key language in the world of artificial intelligence, you don’t need to master it all to get started. This post breaks down the core basics you actually need — the ones that help you build models, work with data, and write logic a machine can learn from.

With real examples and simple explanations, it’s a practical guide for any beginner wondering:
“What Python do I really need to learn for AI/ML?”

Why Python?

  • Python is a high-level, general-purpose programming language with simple, readable syntax.

  • Powerful language used in many areas like web development, data science, automation, and AI

  • Created by Guido van Rossum in 1989 and released publicly in 1991.

  • Ideal for beginners and rapid application development.

  • Supports modularity and code reuse through modules and packages.

  • Widely used due to its rich standard library and third-party support.

  • Python uses indentation (spaces or tabs) to define blocks of code instead of brackets like {} used in other languages.

  • The chart below (from the PYPL index) shows how often people search for language tutorials. Python is at the top, showing it's widely used and in demand.

IDES for Python

I mostly use python notebooks in VSCode. You can also write and run Python programs using the following IDEs:

  • IDLE – Built-in and beginner-friendly.

  • PyCharm – A powerful IDE for professionals.

  • Spyder – Great for data science and research work.

Python Concepts You Actually Need for AI/ML

  1. Variables & Data Types

  • Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

  • Variables hold your data — whether that’s an input feature or the output of a model. They’re your starting point.

  • Data Types:

# String (text)
user_name = "Ambiga"

# Integer (whole number)
age = 25

# Float (decimal number)
height = 5.4

# Boolean (True or False)
is_learning = True

# Print all variables
print("Name:", user_name)
print("Age:", age)
print("Height:", height)
print("Learning Python:", is_learning)
  1. Lists, Tuples, Dictionaries – Like Shopping Lists and ID Cards

Lists = Ordered items

  • Lists are used to store ordered, changeable collections of items.

  • They are similar to arrays in other languages.

  • Items can be of any data type, and duplicates are allowed.

  • Created using square brackets [].

  • You can add, remove, or modify items easily.

features = [3.4, 5.6, 7.8]

fruits = ["apple", "banana", "mango"]
print(fruits[1])

# List (ordered, changeable collection)
skills = ["Python", "Data Science", "Web Development"]

# Tuple (ordered, unchangeable collection)
location = ("India", "Chennai")

# Set (unordered collection with unique items)
unique_ids = {1, 2, 3, 2}

print("Skills:", skills)
print("Location:", location)
print("Unique IDs:", unique_ids)

Tuples

  • Tuples are like lists, but immutable (can’t be changed after creation).

  • Used to store ordered collections of items.

  • Created using parentheses ().

  • You can’t add, remove, or change items in a tuple.

  • Useful when you want to protect data from modification.

colors = ("red", "green", "blue")
print(colors[1])  # Output: green

Dictionaries = Key-value pairs

  • Dictionaries in Python store data as key-value pairs using the dict type.

  • Created using {} or the dict() function.

  • Access values by passing the key in square brackets: my_dict["key"]

# Dictionary (key-value pairs)
profile = {
    "name": "Ambiga",
    "age": 25,
    "language": "Python"
}

print("Profile Info:", profile)
print(profile["name"]
  1. Control Flow Statement

Python control flow statements

Conditional Statements

Used to make decisions in your code.

  • if: Runs a block if the condition is true.

  • if–else: Runs one block if true, another if false.

  • if-elif-else: Checks multiple conditions in order.

  • nested if-else: An if statement inside another if.

age = 18
if age > 18:
    print("Adult")
elif age == 18:
    print("Just became an adult")
else:
    print("Minor")

Transfer Statements

Used to control the flow inside loops or conditional blocks.

  • break: Exits the loop early.

  • continue: Skips the current loop iteration and moves to the next.

  • pass: Does nothing, just a placeholder.

for i in range(5):
    if i == 3:
        break
    print(i)

Iterative Statements(Loops)

Used to repeat code multiple times.

  • for: Loops over a sequence like list, string, or range.

  • while: Loops while a condition is true.

count = 0
while count < 3:
    print("Hello")
    count += 1
  1. Functions – Reusable Code for Everything

  • A function is a reusable block of code that performs a specific task.

  • It helps avoid repetition and keeps code organized.

  • Defined using the def keyword.

  • Can take inputs (parameters) and return outputs.

  • Built-in functions: print(), len(), sum(), etc.

  • We can also define your own functions (user-defined).

def normalize(data):
    min_val = min(data)
    max_val = max(data)
    return [(x - min_val) / (max_val - min_val) for x in data]

# Sample data
raw_scores = [45, 67, 89, 34, 76]

# Normalize the data
normalized_scores = normalize(raw_scores)

print("Normalized Scores:", normalized_scores)
  1. File Handling

File handling lets you read from and write to files (like .txt, .csv, etc.).

Common operations:

  • Open a file: open("filename.txt", "r")

  • Read content: file.read() or file.readlines()

  • Write content: file.write("Hello")

  • Close the file: file.close()

# Writing to a file
with open("data.txt", "w") as f:
    f.write("Machine Learning is fun!")

# Reading from a file
with open("data.txt", "r") as f:
    print(f.read())
  1. Modules in Python

A module is a Python file (.py) that contains functions, variables, or classes.

  • We can create own or use built-in ones.

  • Helps organize code into reusable parts.

# math is a built-in module
import math

print(math.sqrt(16))  # Output: 4.0

We can also create our own:

# mymodule.py
def greet(name):
    return f"Hello, {name}!"

then use it

import mymodule
print(mymodule.greet("Ambiga"))
  1. Libraries – Where the Power Lives

A library is a collection of modules with pre-written code for specific tasks.

  • NumPy – for numerical operations

  • Pandas – for data handling

  • Matplotlib/Seaborn – for plotting

  • Scikit-learn – for ML models

  • TensorFlow / PyTorch – for deep learning

import pandas as pd

data = pd.read_csv("sample.csv")
print(data.head())

Libraries are where the real magic begins — think NumPy, Pandas, Scikit-learn, and more. I haven’t explored them deeply yet, but they’re the gateway to serious data and machine learning work. Tomorrow, I’ll dive into them. One thing’s for sure: you'll be importing a lot, so it’s good to get comfortable with it early!

Reflection

Honestly, today felt like a reset.

You don’t need to be a full-time Python developer to start with AI/ML — but you do need to understand how to structure logic, process data, and control flow.

Every ML model starts with:

  • Clean data

  • Correct logic

  • Structured processing

Today’s Python practice sets the stage for everything I’ll build going forward.

Tomorrow, I’ll be diving into the core Python libraries that make data manipulation and analysis a breeze — NumPy and Pandas. These are the true powerhouses behind most machine learning workflows. Whether it's handling arrays, cleaning datasets, or performing complex calculations, these libraries do the heavy lifting. I'm excited to explore how they work and why they’re so essential in the world of AI/ML. Stay tuned for the deep dive!


Didn’t catch Day 1? Check it out here — I shared why I’m learning AI and how I set up my tools to get started.

0
Subscribe to my newsletter

Read articles from Ambigavathi Durairaj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ambigavathi Durairaj
Ambigavathi Durairaj