Python for Artificial Intelligence Beginner 10 minutes

Introduction to Python

Why Python is the first language in AI, and how to write your first program.

Your progress in the track0%

Why Python?

Python is a general-purpose, high-level, interpreted programming language. It has become the main language in AI and data science for three main reasons:

  • Clear syntax: the code is close to natural language and easy to read.
  • An ecosystem of scientific libraries: NumPy for numerical computing, pandas for tabular data, scikit-learn for machine learning, and PyTorch and TensorFlow for deep learning.
  • A large community: excellent official documentation and many examples and tutorials.

Your first program

print("Hello, artificial intelligence")
name = "Sara"
print("Welcome", name)

The function print shows values on the screen, andname is a variable that holds text.

Indentation defines blocks

x = 7
if x > 5:
    print("Greater than five")   # inside the condition
print("Done")               # outside the condition

Lines indented by four spaces belong to the body of the condition. Indentation is part of the language's rules, not optional formatting.

Where do you write code?

  • The terminal: type python (or py on Windows) to open interactive mode, or run a file with the command python app.py.
  • Jupyter notebooks: cells that combine code, results and explanation, and they are the most used in data analysis.
  • A code editor: such as VS Code with the Python extension.

Comments

A comment starts with the symbol # and is not executed by the interpreter. Use comments to explain the reason for a programming decision, not to restate what the code clearly says.

Lesson terms

Indentation
The spaces at the start of a line, which Python uses to define code blocks such as the body of a condition or a loop.
Interpreter
A program that executes Python code line by line without the need for prior compilation into an executable file.

Abstract

Python is a high-level, interpreted programming language with a clear syntax and a wide ecosystem of scientific libraries such as NumPy, pandas, scikit-learn and PyTorch. Code blocks are written with indentation instead of braces, and programs run from the terminal or from Jupyter notebooks.

Review questions

  1. Why is indentation important in Python?

    Because it defines where code blocks start and end, and a mistake in it causes an IndentationError or unintended behaviour.

  2. Name two Python libraries used in AI.

    NumPy and PyTorch (or pandas, scikit-learn and TensorFlow).

Sources and references

  1. The Python Tutorial — Python Software Foundation
  2. An Informal Introduction to Python

Complete the lesson