Introduction to Python
Why Python is the first language in AI, and how to write your first program.
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 conditionLines 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(orpyon Windows) to open interactive mode, or run a file with the commandpython 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
-
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.
-
Name two Python libraries used in AI.
NumPy and PyTorch (or pandas, scikit-learn and TensorFlow).
Sources and references
Complete the lesson
You have completed all the lessons in the track! You can now request the track completion certificate.
Request the certificate