I1 · Python Programming for AI

Introduction to Python

Python is a general-purpose, high-level programming language known for its readable syntax. A Python program is a text file of instructions that the Python interpreter reads and runs line by line, from top to bottom.

8 min Intermediate · Sublevel Beginner Jump to the activity

What you need: The earlier topics of this module.

Definition

Why do almost all AI engineers write Python?

Python is a general-purpose, high-level programming language known for its readable syntax. A Python program is a text file of instructions that the Python interpreter reads and runs line by line, from top to bottom.

In simple words

Python reads almost like English: print("Hello") shows Hello on the screen. You don't need to manage memory or write long setup code. That simplicity, plus thousands of free libraries for data and AI, is why it became the language of machine learning.

Overview

  • Python is a high-level, general-purpose programming language created by Guido van Rossum and first released in 1991.
  • It is interpreted: the Python interpreter reads and runs code line by line, so you can try ideas instantly.
  • Its syntax uses indentation instead of brackets to show blocks, which keeps code readable.
  • It is dynamically typed: a variable's type is decided by the value it holds while the program runs.
  • Huge libraries such as NumPy, pandas, scikit-learn and PyTorch make Python the main language of AI and data science.
  • Python 3 is the current version; Python 2 reached end of life in 2020 and should not be used.

Why it matters

NumPy, pandas, scikit-learn, PyTorch, TensorFlow and the tools that build LLM applications are all used from Python. Learning it opens every later module of this course and most AI jobs.

Key terms

Programming language
A formal language for writing instructions a computer can carry out.
Interpreter
A program that reads source instructions and runs them, line by line.
Syntax
The grammar rules of a language: how statements must be written.
High-level language
A language close to human language, which hides the details of the hardware.
Library
A collection of ready-made, reusable functions you can import.
Script
A Python program saved in a file ending in .py.

Types

Python is a multi-paradigm language: it lets you write programs in several styles. These are the main styles you will use, often mixed in one program.

1. Procedural (imperative) style

Writing a program as a sequence of statements and functions that change data step by step.

  • How it works: You describe exactly what to do in order: read input, loop, update variables, call functions, print results.
  • Example: A script that reads marks, adds them in a loop and prints the average.
  • When to use it: Use it for short scripts, automation and learning, where a clear sequence of steps is easiest to follow.

2. Object-oriented style

Organising a program around objects that bundle data (attributes) with behaviour (methods).

  • How it works: You define classes as blueprints, create objects from them and let objects call each other's methods; everything in Python is in fact an object.
  • Example: A Student class with name and marks, and a method average() that each student object can call.
  • When to use it: Use it for larger programs with many related things, such as games, apps and machine-learning models in scikit-learn.

3. Functional style

Building programs from functions that take inputs and return outputs without changing anything else.

  • How it works: You pass functions to other functions, use map, filter, lambda and comprehensions, and avoid changing shared variables.
  • Example: squares = [x * x for x in numbers] or sorted(students, key=lambda s: s[1]).
  • When to use it: Use it for data transformations, where small pure functions are easy to test and combine.

4. Scripting and glue style

Using Python to connect other programs, files, websites and tools together.

  • How it works: Short programs call libraries, operating-system commands or web services to automate tasks that would be slow by hand.
  • Example: A script that renames 500 photos by date or downloads daily weather data into a spreadsheet.
  • When to use it: Use it to automate repetitive work and to connect data sources before analysis or model training.

Explained in detail

A short history

Python was created by the Dutch programmer Guido van Rossum, who began work on it in late 1989 and released it in 1991. He named it after the British comedy group Monty Python, not the snake. Python 3, released in 2008, cleaned up the language in ways that were not fully compatible with Python 2, and Python 2 officially reached the end of its life in January 2020. Everything in this course uses Python 3. Today Python is one of the most widely used programming languages in the world, and the most popular language for data science and artificial intelligence.

Why Python dominates AI

ReasonWhat it means for you
Readable syntaxCode looks close to plain English, so you can focus on ideas
Huge library ecosystemNumPy, pandas, scikit-learn, PyTorch and TensorFlow are all used from Python
Interactive useNotebooks let you run small pieces and see results immediately
Large communityAnswers, tutorials and examples exist for almost every question
Free and open sourceAnyone can install and use it on any major operating system

How Python runs your program

When you run a Python file, the interpreter reads it from top to bottom. Behind the scenes, the standard interpreter, called CPython because it is written in the C language, first translates your text into a simpler form called bytecode, then executes the bytecode one instruction at a time. If it meets a line it cannot understand, it stops with a syntax error before running anything. If a problem appears while running, such as dividing by zero, it stops at that line with an exception and reports where it happened.

The trade-off

Python is slower than compiled languages such as C for raw number crunching, because it does more work at run time. AI libraries solve this cleverly: the heavy calculations inside NumPy or PyTorch are written in fast languages like C, C++ and CUDA, while you write the easy, readable Python that calls them. So you get both speed and simplicity, which is a big reason Python became the language of AI.

Python is also used far beyond AI: websites such as Instagram have run large parts of their systems on it, scientists use it to analyse telescope and genome data, and many everyday automation scripts are written in it.

How it works

  • Interpreted: Python runs your code line by line, so you see results immediately.
  • Readable: indentation (spaces at the start of a line) shows structure, instead of brackets.
  • Batteries included: a large standard library for maths, files, dates and more.
  • Huge ecosystem: free libraries for data, AI, the web and science.

In code

Your first program: it prints a greeting and does a small calculation.

first_program.py
print("Hello, AI Pathshala!")
print("Python can do maths:", 7 * 6)
print("Minutes in a week:", 7 * 24 * 60)

Output

Hello, AI Pathshala!
Python can do maths: 42
Minutes in a week: 10080

Try it yourself

Predict the output

Match each line of Python to what it prints. Then run the code above to check.

  • print("Hi")
  • print(3 + 4)
  • print("3" + "4")
  • print(2 * 5)

Common mistakes

  • Forgetting the quotes around text: print(Hello) is an error.
  • Using capital letters wrongly: Print is not print.
  • Thinking Python is only for AI. It is used for websites, automation, science and games too.

Check yourself

Question 1

1. How does Python run a program?

Question 2

2. What does print(7 * 6) show?

Question 3

3. Why is Python popular for AI?

Summary

  • Python is a readable, interpreted, general-purpose language.
  • Programs run line by line from top to bottom.
  • Its libraries make it the main language of AI.

Finished reading?

Sources: The Python Tutorial (python.org) · W3Schools Python Tutorial