> ## Documentation Index
> Fetch the complete documentation index at: https://fastapi2day.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# What Is Python and Why Is It Used for AI Development?

> Discover what Python is, how dynamic typing and the interpreter work, and why Python has become the dominant language for AI and machine learning.

Python is a high-level, interpreted programming language designed with one goal in mind: making it as easy as possible for humans to write instructions that computers can execute. Rather than wrestling with low-level memory management or verbose syntax, you focus on expressing your ideas clearly — Python handles the rest. That philosophy has made it the dominant language for data science, machine learning, and AI, and it is exactly what you will use throughout this course to build intelligent APIs with FastAPI.

## What is programming?

Programming is writing step-by-step instructions for a computer to follow. Computers are powerful, but they are not smart — they need precise, unambiguous directions for everything. A programming language is the bridge between human thinking and machine execution:

1. **You write** code in Python — syntax that looks close to plain English.
2. **Python translates** your code into lower-level instructions the runtime understands.
3. **The computer executes** those instructions and produces a result.

## How Python's interpreter works

Unlike compiled languages (such as C or Go), Python is **interpreted**. When you run a Python file, the Python interpreter reads your source code line by line, translates each line into bytecode, and executes it immediately. You do not need a separate compile step before running your program.

```bash theme={null}
# Run a Python file directly — no compilation needed
python3 hello.py
```

This makes iteration fast: change your code, run it again, and see the result instantly. It is one reason Python is so popular for exploratory data work and AI prototyping.

## Dynamic typing

Python uses **dynamic typing**, which means you do not declare the type of a variable before using it. Python figures out the type at runtime based on the value you assign.

```python theme={null}
# No type declarations needed — Python infers the type
name = "Alice"          # str
score = 98.6            # float
is_active = True        # bool
items = [1, 2, 3]       # list

# Types can even change as you reassign (though this is rarely good practice)
value = 42              # int
value = "forty-two"     # now a str
```

<Tip>
  Even though Python doesn't require type annotations, modern Python supports optional type hints. FastAPI relies heavily on them — you'll learn to use them throughout this course.
</Tip>

## What Python code looks like

Here is a taste of Python syntax. Notice how readable it is compared to many other languages:

```python theme={null}
# Variables and formatted strings
name = "Sarah"
age = 25
print(f"Hello, my name is {name} and I am {age} years old.")

# Conditional logic reads almost like English
if age >= 18:
    print("I can vote!")
else:
    print("I'm not old enough to vote yet.")

# Looping over a list
languages = ["Python", "JavaScript", "Rust"]
for lang in languages:
    print(f"I know {lang}")
```

```text theme={null}
Hello, my name is Sarah and I am 25 years old.
I can vote!
I know Python
I know JavaScript
I know Rust
```

Indentation (the spaces at the start of each line) is not just style in Python — it is how the language defines code blocks. You will get used to it quickly.

## Why Python is great for AI

Python's position as the leading AI language is not an accident. Several factors combine to make it the right tool for the job:

* **Readable syntax** — Data scientists and researchers can focus on algorithms, not boilerplate code.
* **Rich ecosystem** — Libraries like NumPy, Pandas, TensorFlow, PyTorch, and scikit-learn are all Python-first.
* **Interactive exploration** — Jupyter notebooks let you mix code, output, and documentation in one place.
* **FastAPI** — The modern web framework you'll use in this course is Python-native and built for building AI-powered APIs.
* **Massive community** — Millions of developers means abundant tutorials, Stack Overflow answers, and open-source packages.

```python theme={null}
# A tiny example of what's possible with Python's AI ecosystem
import random

# Simulate a simple prediction score
def predict_sentiment(text: str) -> dict:
    score = round(random.uniform(0, 1), 2)
    label = "positive" if score > 0.5 else "negative"
    return {"text": text, "score": score, "label": label}

result = predict_sentiment("Python is amazing for AI!")
print(result)
# {'text': 'Python is amazing for AI!', 'score': 0.87, 'label': 'positive'}
```

## Python versions

There are two major Python version lines you may encounter:

* **Python 3** (current) — This is what you will install and use. All course material targets Python 3. See the [official version support schedule](https://devguide.python.org/versions/) for details.
* **Python 2** (end-of-life) — Support ended in January 2020. If you come across a tutorial using Python 2, find a newer one.

<Warning>
  Always install Python 3. If a tutorial or code sample uses `print "hello"` without parentheses, it is Python 2 and out of date.
</Warning>

## Ready to install?

<Card title="Continue to Installing Python" icon="arrow-right" href="/getting-started/installing-python">
  Choose your operating system and get Python running on your machine.
</Card>
