Python Tutorial for AI/ML Beginners

by Selwyn Davidraj     Posted on September 28, 2025

Python Tutorial for AI/ML Beginners

This blog provides a very high level introduction to python basics


Python Tutorial for AI/ML Beginners

Python has become the de-facto programming language for Artificial Intelligence (AI) and Machine Learning (ML).
If you’re just starting your AI/ML journey, mastering Python basics is essential.
This tutorial will walk you through the core concepts with examples and code snippets.


1. What is Python?

Python is a high-level, interpreted, general-purpose programming language known for its simplicity and readability.
It was created by Guido van Rossum in 1991 and has grown into one of the most popular programming languages.

Advantages

  • Easy to learn (English-like syntax).
  • Huge standard library and ecosystem of third-party packages.
  • Cross-platform compatibility.
  • Strong community support.

Disadvantages

  • Slower execution compared to C/C++ (since it is interpreted).
  • Not ideal for low-level hardware programming.
  • High memory consumption in large applications.

How it differs from other languages

  • Python vs C/C++ → Easier to write and read, but slower.
  • Python vs Java → Less verbose; dynamic typing vs Java’s static typing.
  • Python vs R → R is good for statistics, but Python is more general-purpose and widely adopted for AI/ML.

2. Why Python is used in AI/ML?

  • Extensive libraries like numpy, pandas, scikit-learn, tensorflow, and pytorch.
  • Readable syntax → Great for prototyping.
  • Community support → Many tutorials, datasets, and frameworks.
  • Integration with other languages (C, C++, Java).

3. Variables

Variables store data values. You don’t need to declare their type explicitly.

x = 10
y = "Hello"
z = 3.14
print(x, y, z)

Output:

10 Hello 3.14

4. Types of Variables

  • Integer (int) → Whole numbers
  • Float (float) → Decimal numbers
  • String (str) → Text
  • Boolean (bool) → True/False
  • Complex (complex) → Numbers with imaginary part
a = 5            # int
b = 2.7          # float
c = "Python"     # str
d = True         # bool
e = 3 + 4j       # complex
print(type(a), type(b), type(c), type(d), type(e))

5. Type Conversion

Convert one data type into another.

x = "100"
y = int(x)   # String → Integer
z = float(y) # Integer → Float
print(y, z)

Output:

100 100.0

6. Lists

Lists are mutable and store multiple values.

fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
print(fruits)

Output:

['apple', 'banana', 'cherry', 'mango']

7. Tuples

Tuples are immutable (cannot be changed).

colors = ("red", "green", "blue")
print(colors[0])

Output:

red

8. Dictionary

Dictionaries store data as key-value pairs.

student = {"name": "Alice", "age": 22, "course": "AI/ML"}
print(student["name"])

Output:

Alice

9. Conditional Statements

age = 20
if age >= 18:
    print("Eligible to vote")
else:
    print("Not eligible")

10. Loops

For Loop

for i in range(5):
    print(i)

Output:

0
1
2
3
4

While Loop

count = 0
while count < 3:
    print("Hello")
    count += 1

11. List Comprehension

Compact way to create lists.

squares = [x**2 for x in range(5)]
print(squares)

Output:

[0, 1, 4, 9, 16]

12. Functions

Functions group reusable code.

def greet(name):
    return f"Hello, {name}"

print(greet("Alice"))

13. *args and **kwargs

  • *args → Arbitrary number of arguments
  • **kwargs → Arbitrary keyword arguments
def my_func(*args, **kwargs):
    print("Args:", args)
    print("Kwargs:", kwargs)

my_func(1, 2, 3, name="Alice", age=25)

14. Object-Oriented Programming (OOPs)

Python supports OOP concepts like classes and objects.

class Person:
    def __init__(self, name):
        self.name = name
    
    def greet(self):
        return f"Hi, I'm {self.name}"

p = Person("Alice")
print(p.greet())

15. Debugging

Use the pdb module for debugging.

import pdb

def add(a, b):
    pdb.set_trace()  # Breakpoint
    return a + b

print(add(2, 3))

16. OS Module

Interact with the operating system.

import os

print(os.getcwd())       # Current working directory
print(os.listdir("."))   # List files in directory

Conclusion

Python’s simplicity, flexibility, and AI/ML-friendly ecosystem make it the go-to language for beginners and professionals alike.
By mastering these basics—variables, data structures, loops, functions, OOP, and modules—you are ready to dive deeper into AI/ML libraries and frameworks.

🚀 Next Steps: Explore numpy, pandas, and matplotlib before jumping into machine learning frameworks like scikit-learn and tensorflow.