AI Learning Courses
AI Learning Courses › Courses › Become an AI Professional › Lesson 3
AI PROFESSIONAL ยท LEVEL 1 · LESSON 3 · 9 MIN READ

Python for AI, part 1: the building blocks you will use every day

Python is the language of AI because it is readable and has the best AI libraries. These six building blocks are 90% of everyday Python. Type each example into Colab and run it.

Turn on JavaScript to watch the animated lesson. The full tutorial text is below.

Variables: names for values Watch

A variable is a name that holds a value. Python works out the type by itself: text, whole numbers, decimals, or True and False.

name = "Priya"          # text (a string)
age = 24                # a whole number (int)
score = 87.5            # a decimal number (float)
is_learning = True      # True or False (bool)
print(f"{name} is {age} and scored {score}")

# Output:
# Priya is 24 and scored 87.5

The f before the quotes makes an f-string: anything inside curly brackets is replaced by its value. Everything after a # is a comment for humans; Python ignores it.

Lists and dictionaries: holding many values Watch

A list keeps values in order, in square brackets. A dictionary stores values under names, called keys, in curly brackets. Real AI data - rows of a table, settings of a model, a reply from an AI API - comes in these shapes.

marks = [72, 88, 95, 64, 81]           # a list
student = {"name": "Arjun", "city": "Pune", "marks": marks}   # a dictionary

print(len(marks), max(marks), min(marks))
print(student["city"])
marks.append(90)
print(marks)

# Output:
# 5 95 64
# Pune
# [72, 88, 95, 64, 81, 90]

Loops and if: repeating and deciding Watch

A for loop repeats code for each item. An if chooses what to do. The indented lines (four spaces) belong to the loop or the if - in Python, the spaces are part of the code, not decoration.

marks = [72, 88, 95, 64, 81]
for m in marks:
    if m >= 80:
        print(m, "distinction")
    else:
        print(m, "keep going")

# Output:
# 72 keep going
# 88 distinction
# 95 distinction
# 64 keep going
# 81 distinction

Functions: reusable steps Watch

A function packs steps under a name so you can use them again. It takes inputs and returns a result. The last line below is a list comprehension - a one-line loop that builds a new list, used everywhere in AI code.

def average(numbers):
    """Return the average of a list of numbers."""
    return sum(numbers) / len(numbers)

def grade(mark):
    if mark >= 90:
        return "A"
    elif mark >= 75:
        return "B"
    return "C"

marks = [72, 88, 95, 64, 81]
print("Average:", average(marks))
print([grade(m) for m in marks])

# Output:
# Average: 80.0
# ['C', 'B', 'A', 'C', 'B']

Reading errors like a professional Watch

Lesson learned Watch

Variables name values, lists and dictionaries hold many values, loops repeat, if decides, and functions package steps to reuse. With these six blocks - and the habit of reading errors calmly - you can read most AI code.

Frequently asked questions

Why is Python used for AI?

It is easy to read and has the most widely used AI and data libraries, such as pandas, scikit-learn, PyTorch and the official SDKs of AI companies.

How much Python do I need for AI?

Start with variables, lists, dictionaries, loops, if and functions, then learn pandas for data. That is enough to begin machine learning and LLM projects.

What is an f-string in Python?

A string with f before the quotes, like f"Hi {name}". Python puts the value of each name in curly brackets into the text.

Last checked: November 2026. Tools and free tiers change - check each official page. All examples are original; the data in them is made up for teaching.