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

# Python Statements

## 1. **What is a Statement?**

* A **statement** is a unit of code that performs an action or a task.
* Statements can include variable assignments, function calls, loops, conditionals, and more.
* Every line of code in Python is typically a statement.

**Examples**:

```python theme={"system"}
x = 10                # Assignment statement
print("Hello")        # Function call statement
if x > 5:            # Conditional statement
    print("x is greater than 5")
```

## 2. **Types of Statements**

Python statements can be categorized into the following types:

### **1. Assignment Statements**

* Used to assign a value to a variable.
* The `=` operator is used for assignment.

**Examples**:

```python theme={"system"}
name = "Raj"          # Assign a string to a variable
age = 25              # Assign an integer to a variable
is_student = True     # Assign a Boolean value
```

* **Multiple Assignments**:
  ```python theme={"system"}
  a, b, c = 1, 2, 3    # Assign multiple values at once
  x = y = z = 10       # Assign the same value to multiple variables
  ```

### **2. Expression Statements**

* Any expression becomes a statement when it is used on its own.
* Examples include arithmetic operations, function calls, etc.

**Examples**:

```python theme={"system"}
10 + 20               # Arithmetic expression (but not useful as a standalone statement)
print("Hello, Ram!")  # Function call statement
```

### **3. Conditional Statements (if, elif, else)**

* Used to make decisions based on conditions.
* Keywords: `if`, `elif`, `else`.

**Examples**:

```python theme={"system"}
age = 20
if age >= 18:
    print("Ram is eligible to vote.")
elif age >= 16:
    print("Ram can get a learner's license.")
else:
    print("Ram is too young.")
```

### **4. Loop Statements (for, while)**

* Used to repeat a block of code multiple times.
* Keywords: `for`, `while`.

**Examples**:

* **For Loop**:
  ```python theme={"system"}
  names = ["Raj", "Ram", "Anand", "Bala"]
  for name in names:
      print(f"Hello, {name}!")
  ```

* **While Loop**:
  ```python theme={"system"}
  count = 0
  while count < 5:
      print(f"Count: {count}")
      count += 1
  ```

### **5. Function Definition Statements (def)**

* Used to define reusable blocks of code.
* Keyword: `def`.

**Example**:

```python theme={"system"}
def greet(name):
    print(f"Hello, {name}!")

greet("Karthik")  # Function call statement
```

### **6. Import Statements**

* Used to import modules or specific functions/classes from modules.
* Keyword: `import`.

**Examples**:

```python theme={"system"}
import math                     # Import the entire math module
from datetime import date       # Import only the date class from datetime
```

### **7. Control Flow Statements (break, continue, pass)**

* Used to control the flow of loops or conditionals.
* Keywords: `break`, `continue`, `pass`.

**Examples**:

* **Break**:
  ```python theme={"system"}
  for i in range(10):
      if i == 5:
          break  # Exit the loop when i is 5
      print(i)
  ```

* **Continue**:
  ```python theme={"system"}
  for i in range(10):
      if i % 2 == 0:
          continue  # Skip even numbers
      print(i)
  ```

* **Pass**:
  ```python theme={"system"}
  if x > 10:
      pass  # Placeholder for future code
  ```

### **8. Return Statements**

* Used in functions to return a value to the caller.
* Keyword: `return`.

**Example**:

```python theme={"system"}
def add(a, b):
    return a + b

result = add(5, 10)  # result = 15
```

### **9. Exception Handling Statements (try, except, finally)**

* Used to handle errors and exceptions in code.
* Keywords: `try`, `except`, `finally`.

**Example**:

```python theme={"system"}
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero!")
finally:
    print("Execution complete.")
```

## 3. **Compound Statements**

* Statements that contain other statements, such as loops, conditionals, and function definitions.
* They are often indented to define blocks of code.

**Examples**:

```python theme={"system"}
if x > 10:
    print("x is greater than 10")  # Block of code under if
    print("This is part of the if block")
```

## 4. **Simple vs Compound Statements**

* **Simple Statements**: Single-line statements that perform a single task.
  ```python theme={"system"}
  x = 10
  print("Hello")
  ```
* **Compound Statements**: Multi-line statements that contain blocks of code.
  ```python theme={"system"}
  if x > 10:
      print("x is greater than 10")
      print("This is a compound statement")
  ```

## 5. **Additional Examples**

* **Assignment Statement**:
  ```python theme={"system"}
  name = "Suresh"
  age = 28
  ```

* **Conditional Statement**:
  ```python theme={"system"}
  if age >= 18:
      print(f"{name} is eligible to vote.")
  else:
      print(f"{name} is not eligible to vote.")
  ```

* **Loop Statement**:
  ```python theme={"system"}
  names = ["Sujatha", "Kavitha", "Sathish"]
  for name in names:
      print(f"Hello, {name}!")
  ```

* **Function Definition Statement**:
  ```python theme={"system"}
  def greet_person(name):
      print(f"Good morning, {name}!")

  greet_person("Ramesh")
  ```

## 6. **Best Practices**

* Use meaningful variable names to make statements clear and readable.
* Keep lines short and avoid overly complex statements.
* Use comments to explain the purpose of complex statements.
