Python for AILesson 2
Lesson 28 min
Variables & Loops
The core Python you need: variables, types, loops, and functions.
What you will learn
- ✓Variables and basic types
- ✓for loops and f-strings
- ✓Writing a simple function
Explanation
Python uses indentation (4 spaces) instead of braces to group code — this trips up newcomers from other languages.
Variables need no type declaration: name = 'Mirza', score = 95. Loops and f-strings make output easy.
Functions are defined with def and keep your code reusable. Master these basics and most AI tutorials become readable.
Code Example
python
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
Real-world use
Most AI scripts are mostly ordinary Python — loops, functions, and data wrangling — with a few model calls sprinkled in.
Common mistakes
- • Mixing tabs and spaces, which causes IndentationError.
Practice
Write a function that takes a list of names and prints a greeting for each.
Knowledge check
0/1 answered1. How does Python group a block of code?
Answer all questions to check.