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

# Python Basics

> Learn Python syntax, variables, data types, operators, and control flow

Python is a **high-level, interpreted, object-oriented** programming language known for its simple syntax and readability.

## Python Fundamentals

Some important characteristics of Python:

* Python is **dynamically typed** (no need to declare variable types).
* Python uses **indentation** instead of braces (`{}`) to define blocks.
* Everything in Python is an **object**.
* Variables store references to objects.
* Python follows the **PEP 8** style guide.

## Variables

Variables store references to objects.

```python theme={null}
name = "Alice"
age = 20
price = 99.99
is_active = True
```

### Multiple Assignment

```python theme={null}
x, y, z = 10, 20, 30
```

### Variable Swapping

```python theme={null}
a, b = 10, 20

a, b = b, a
```

### Object Identity

Two variables may reference the same object.

```python theme={null}
a = [1, 2]
b = a

print(a == b)   # True
print(a is b)   # True
```

Use `==` to compare values and `is` to compare object identity.

### Everything is an Object

In Python, every value is an object, including numbers, strings, lists, dictionaries, functions, and classes.

```python theme={null}
x = 10

print(type(x))
print(id(x))
```

### Small Integer Caching

Python caches small integers (typically **-5 to 256**) to improve performance.

```python theme={null}
a = 100
b = 100

print(a is b)      # True

x = 1000
y = 1000

print(x is y)      # May vary
```

> Never use `is` to compare numbers or strings. Use `==` instead.

### Floating-Point Precision

Floating-point numbers are stored in binary, so some decimal values cannot be represented exactly.

```python theme={null}
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
```

Output:

```text theme={null}
0.30000000000000004
False
```

Use `round()` or `math.isclose()` when comparing floating-point numbers.

## Data Types

### Numeric

* `int`
* `float`
* `complex`

### Boolean

* `bool`

### Text

* `str`

### Sequence

* `list`
* `tuple`
* `range`

### Mapping

* `dict`

### Set

* `set`
* `frozenset`

### Binary

* `bytes`
* `bytearray`

### Special

* `None`

```python theme={null}
print(type(10))
print(type(3.14))
print(type("Hello"))
print(type([1, 2, 3]))
print(type({"name": "Alice"}))
```

## Type Conversion

```python theme={null}
int("10")
float("3.14")
str(100)
list("Python")
```

## Operators

### Arithmetic Operators

### Arithmetic Operators

| Operator | Description         | Example             |
| -------- | ------------------- | ------------------- |
| `+`      | Addition            | `10 + 3` → `13`     |
| `-`      | Subtraction         | `10 - 3` → `7`      |
| `*`      | Multiplication      | `10 * 3` → `30`     |
| `/`      | Division            | `10 / 3` → `3.3333` |
| `//`     | Floor Division      | `10 // 3` → `3`     |
| `%`      | Modulus (Remainder) | `10 % 3` → `1`      |
| `**`     | Exponent (Power)    | `2 ** 3` → `8`      |

Example:

```python theme={null}
a = 10
b = 3

print(a + b)   # 13
print(a - b)   # 7
print(a * b)   # 30
print(a / b)   # 3.3333333333333335
print(a // b)  # 3
print(a % b)   # 1
print(a ** b)  # 1000
```

Example:

```python theme={null}
a = 10
b = 3

print(a + b)
print(a ** b)
print(a // b)
```

### Comparison Operators

### Comparison Operators

Comparison operators compare two values and return either `True` or `False`.

| Operator | Description              | Example             |
| -------- | ------------------------ | ------------------- |
| `==`     | Equal to                 | `10 == 3` → `False` |
| `!=`     | Not equal to             | `10 != 3` → `True`  |
| `>`      | Greater than             | `10 > 3` → `True`   |
| `<`      | Less than                | `10 < 3` → `False`  |
| `>=`     | Greater than or equal to | `10 >= 10` → `True` |
| `<=`     | Less than or equal to    | `3 <= 10` → `True`  |

Example:

```python theme={null}
a = 10
b = 3

print(a == b)   # False
print(a != b)   # True
print(a > b)    # True
print(a < b)    # False
print(a >= b)   # True
print(a <= b)   # False
```

Example:

```python theme={null}
print(10 > 5)
print(10 == 20)
```

### Logical Operators

Logical operators are used to combine or negate conditions. They return either `True` or `False` based on the operands.

| Operator | Description                                          | Example                     |
| -------- | ---------------------------------------------------- | --------------------------- |
| `and`    | Returns `True` if **both** conditions are true       | `5 > 2 and 10 > 3` → `True` |
| `or`     | Returns `True` if **at least one** condition is true | `5 > 10 or 8 > 3` → `True`  |
| `not`    | Reverses the result of a condition                   | `not (5 > 2)` → `False`     |

Example:

```python theme={null}
age = 20
has_id = True

print(age >= 18 and has_id)   # True
print(age < 18 or has_id)     # True
print(not has_id)             # False
```

Logical operators work with **truthy** and **falsy** values.

#### Truthy and Falsy Values

In Python, every object has a truth value.

**Falsy values** include:

* `False`
* `None`
* `0`, `0.0`
* `""` (empty string)
* `[]`
* `()`
* `{}`
* `set()`

Everything else is **truthy**, such as:

* Non-zero numbers
* Non-empty strings
* Non-empty lists, tuples, dictionaries, and sets

```python theme={null}
print(bool(0))
print(bool(""))
print(bool([]))

print(bool(10))
print(bool("Python"))
print(bool([1, 2]))
```

Unlike many programming languages, `and` and `or` return one of their operands instead of only `True` or `False`.

```python theme={null}
print(3 and 4)          # 4
print(0 and 10)         # 0

print(0 or "Hi")        # Hi
print("" or "Python")   # Python

print("Hello" and 100)  # 100
print([] or [1, 2, 3])  # [1, 2, 3]
```

* `and` returns the **first falsy value**, otherwise the **last value**.
* `or` returns the **first truthy value**, otherwise the **last value**.

A common use is providing default values.

```python theme={null}
name = "" or "Guest"

print(name)
```

### Assignment Operators

Assignment operators are used to assign values to variables. Some operators also perform an operation before assigning the result back to the variable.

| Operator | Description         | Example   |
| -------- | ------------------- | --------- |
| `=`      | Assign value        | `x = 10`  |
| `+=`     | Add and assign      | `x += 5`  |
| `-=`     | Subtract and assign | `x -= 5`  |
| `*=`     | Multiply and assign | `x *= 2`  |
| `/=`     | Divide and assign   | `x /= 2`  |
| `%=`     | Modulus and assign  | `x %= 3`  |
| `**=`    | Exponent and assign | `x **= 2` |

Example:

```python theme={null}
x = 10

x += 5
print(x)      # 15

x -= 3
print(x)      # 12

x *= 2
print(x)      # 24

x /= 4
print(x)      # 6.0

x %= 4
print(x)      # 2.0

x **= 3
print(x)      # 8.0
```

### Identity Operators

```python theme={null}
is
is not
```

```python theme={null}
a = []
b = []

print(a == b)
print(a is b)
```

### Membership Operators

```python theme={null}
in
not in
```

```python theme={null}
print("a" in "apple")
print(3 in [1, 2, 3])
```

### Bitwise Operators

### Bitwise Operators

Bitwise operators perform operations on the **binary representation** of integers. They are commonly used in systems programming, networking, embedded programming, and optimization.

| Operator | Description | Example         |
| -------- | ----------- | --------------- |
| `&`      | Bitwise AND | `5 & 3` → `1`   |
| `\|`     | Bitwise OR  | `5 \| 3` → `7`  |
| `^`      | Bitwise XOR | `5 ^ 3` → `6`   |
| `~`      | Bitwise NOT | `~5` → `-6`     |
| `<<`     | Left Shift  | `5 << 1` → `10` |
| `>>`     | Right Shift | `5 >> 1` → `2`  |

Example:

```python theme={null}
a = 5      # Binary: 0101
b = 3      # Binary: 0011

print(a & b)    # 1
print(a | b)    # 7
print(a ^ b)    # 6
print(~a)       # -6
print(a << 1)   # 10
print(a >> 1)   # 2
```

**Shift Operators**

* `<<` shifts bits to the **left**, effectively multiplying by `2` for each shift.
* `>>` shifts bits to the **right**, effectively dividing by `2` (for positive integers) for each shift.

```python theme={null}
print(5 << 2)    # 20
print(20 >> 2)   # 5
```

> **Note:** Bitwise operators work only with integer values and are less commonly used in everyday Python programming compared to arithmetic and logical operators.

### Operator Precedence

Use parentheses to improve readability.

```python theme={null}
result = (10 + 5) * 2
```

## Control Flow

### if

```python theme={null}
age = 18

if age >= 18:
    print("Adult")
```

### if...else

```python theme={null}
if age >= 18:
    print("Adult")
else:
    print("Minor")
```

### if...elif...else

```python theme={null}
marks = 82

if marks >= 90:
    print("A")
elif marks >= 75:
    print("B")
else:
    print("C")
```

### match...case (Python 3.10+)

Python supports **Structural Pattern Matching** using `match`.

```python theme={null}
day = 2

match day:
    case 1:
        print("Monday")
    case 2:
        print("Tuesday")
    case _:
        print("Invalid Day")
```

### for Loop

```python theme={null}
for i in range(5):
    print(i)
```

Looping through a collection:

```python theme={null}
for name in ["Alice", "Bob"]:
    print(name)
```

### while Loop

```python theme={null}
count = 1

while count <= 5:
    print(count)
    count += 1
```

### break

Terminates the loop immediately.

```python theme={null}
for i in range(10):

    if i == 5:
        break

    print(i)
```

### continue

Skips the current iteration.

```python theme={null}
for i in range(5):

    if i == 2:
        continue

    print(i)
```

### pass

Acts as a placeholder when no code is required.

```python theme={null}
if True:
    pass
```

### for...else

The `else` block executes only if the loop completes without encountering a `break`.

```python theme={null}
for i in range(5):
    print(i)
else:
    print("Loop completed")
```

### while...else

Works the same way as `for...else`.

```python theme={null}
count = 1

while count <= 3:
    print(count)
    count += 1
else:
    print("Finished")
```

## Python Nuances

Some Python-specific behaviors every beginner should know:

* Everything in Python is an object.
* Variables store references to objects, not values.
* Use `==` for value comparison and `is` for identity comparison.
* Small integers (`-5` to `256`) are cached.
* Floating-point arithmetic is not always exact.
* Indentation defines code blocks.
* Lists are mutable; tuples are immutable.
* Functions are first-class objects.
* `and` and `or` return values, not just `True` or `False`.
* Truthy and falsy values simplify conditional expressions.
* Multiple assignment and unpacking are built into the language.
* Python emphasizes readability and simplicity (PEP 8).

***

## Practice & Exercises

To reinforce what you've learned in this section (variables, data types, operators, and control flow), practice with these interactive notebooks:

<CardGroup cols={2}>
  <Card title="Follow-Along Practice" icon="laptop-code">
    Practice variable assignments, swapping, object identity, working with operators (arithmetic, logical, bitwise), and implementing control flow like conditionals and loops.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/python%20material/fastapi-course/public/notebooks/basics_exercises/Python_Basics_Practice.ipynb) | [🚀 Colab](https://colab.research.google.com/github/prasad230776/python4ai/blob/master/public/notebooks/basics_exercises/Python_Basics_Practice.ipynb) | <a href="/public/notebooks/basics_exercises/Python_Basics_Practice.ipynb" download>📥 Download</a>
  </Card>

  <Card title="Practice Exercises" icon="pen-to-square">
    Test your knowledge with hands-on exercises including variable swapping/identity validation, safe float comparisons, short-circuit defaults, arithmetic/bitwise challenges, leap year calculator, and loop logic control.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/python%20material/fastapi-course/public/notebooks/basics_exercises/Python_Basics_Exercises.ipynb) | [🚀 Colab](https://colab.research.google.com/github/prasad230776/python4ai/blob/master/public/notebooks/basics_exercises/Python_Basics_Exercises.ipynb) | <a href="/public/notebooks/basics_exercises/Python_Basics_Exercises.ipynb" download>📥 Download</a>
  </Card>
</CardGroup>
