StudyAIStudyAI
Pro
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
def greet(name):
2
    return f'Hello, {name}!'
3
 
4
for i in range(3):
5
    print(greet(f'Lesson {i + 1}'))
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 answered

1. How does Python group a block of code?

Answer all questions to check.