> ## 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 Expressions

## 1. **What is an Expression?**

* An **expression** is a combination of values, variables, operators, and function calls that evaluates to a single value.
* Expressions can be as simple as a single variable or as complex as a combination of multiple operations.

**Examples**:

```python theme={"system"}
x = 10                # Simple expression (variable assignment)
result = x + 5        # Expression with an operator
name = "Raj"          # String expression
```

## 2. **Components of Expressions**

* **Operands**: Values or variables on which operations are performed.
* **Operators**: Symbols that perform operations on operands (e.g., `+`, `-`, `*`, `/`).

**Example**:

```python theme={"system"}
a = 5
b = 10
sum = a + b  # a and b are operands, + is the operator
```

## 3. **Types of Expressions**

* **Arithmetic Expressions**: Perform mathematical operations.
  * Operators: `+`, `-`, `*`, `/`, `//`, `%`, `**`
  * Example:
    ```python theme={"system"}
    x = 10
    y = 3
    result = x ** 2 + y  # 10^2 + 3 = 103
    ```

* **String Expressions**: Combine or manipulate strings.
  * Operators: `+` (concatenation), `*` (repetition)
  * Example:
    ```python theme={"system"}
    first_name = "Raj"
    last_name = "Kumar"
    full_name = first_name + " " + last_name  # "Raj Kumar"
    greeting = "Hello, " * 3  # "Hello, Hello, Hello, "
    ```

* **Comparison Expressions**: Compare two values and return a Boolean (`True` or `False`).
  * Operators: `==`, `!=`, `>`, `<`, `>=`, `<=`
  * Example:
    ```python theme={"system"}
    age_raj = 25
    age_ram = 30
    is_raj_older = age_raj > age_ram  # False
    ```

* **Logical Expressions**: Combine Boolean values using logical operators.
  * Operators: `and`, `or`, `not`
  * Example:
    ```python theme={"system"}
    is_student = True
    is_employed = False
    can_apply = is_student and not is_employed  # True
    ```

* **Membership Expressions**: Check if a value exists in a sequence (e.g., list, string).
  * Operators: `in`, `not in`
  * Example:
    ```python theme={"system"}
    names = ["Raj", "Ram", "Anand", "Bala"]
    is_karthik_present = "Karthik" in names  # False
    ```

* **Identity Expressions**: Check if two variables refer to the same object.
  * Operators: `is`, `is not`
  * Example:
    ```python theme={"system"}
    x = 10
    y = 10
    is_same = x is y  # True (for small integers, Python reuses memory)
    ```

## 4. **Additional Examples**

* **Arithmetic Expression**:
  ```python theme={"system"}
  age_raj = 25
  age_ram = 30
  total_age = age_raj + age_ram  # 55
  ```

* **String Expression**:
  ```python theme={"system"}
  first_name = "Suresh"
  last_name = "Kumar"
  full_name = first_name + " " + last_name  # "Suresh Kumar"
  ```

* **Comparison Expression**:
  ```python theme={"system"}
  marks_anand = 85
  marks_bala = 90
  is_anand_higher = marks_anand > marks_bala  # False
  ```

* **Logical Expression**:
  ```python theme={"system"}
  is_karthik_student = True
  is_karthik_employed = False
  can_karthik_apply = is_karthik_student and not is_karthik_employed  # True
  ```

* **Membership Expression**:
  ```python theme={"system"}
  names = ["David", "Kannan", "Siva", "Ramesh"]
  is_sujatha_present = "Sujatha" in names  # False
  ```

* **Identity Expression**:
  ```python theme={"system"}
  x = "Sathish"
  y = "Sathish"
  is_same_person = x is y  # True (strings are interned in Python)
  ```

## 5. **Order of Evaluation (Precedence)**

* Python evaluates expressions based on operator precedence.
* Use parentheses `()` to override the default order.

**Example**:

```python theme={"system"}
result = 10 + 5 * 2  # 20 (multiplication is evaluated first)
result = (10 + 5) * 2  # 30 (parentheses override precedence)
```

## 6. **Best Practices**

* Use parentheses to make complex expressions more readable.
* Break down complex expressions into smaller, meaningful parts.
* Use descriptive variable names to make expressions self-explanatory.

**Example**:

```python theme={"system"}
# Less readable
total = (x + y) * (a - b) / (c ** 2)

# More readable
sum_xy = x + y
diff_ab = a - b
square_c = c ** 2
total = (sum_xy * diff_ab) / square_c
```
