# Chalk Python SDK Reference: Logical Functions
source: https://docs.chalk.ai/api-docs#section-logical-functions

## `between(value, low, high, inclusive)`
source: https://docs.chalk.ai/api-docs#between

Check if a value is between two bounds (inclusive).

**Parameters**

- `value`: `Underscore | Any`
  The value to check.
- `low`: `Underscore | Any`
  The lower bound.
- `high`: `Underscore | Any`
  The upper bound.
- `inclusive`: `'both' | 'left' | 'right' | 'neither'` = `'both'`
  Specify if `"both"` (default), the `"left"` (lower), the `"right"` (upper), or `"neither"` bound(s) should be treated as inclusive.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class RiskAssessment:
   id: str
   score: float
   is_moderate_risk: bool = F.between(_.score, 0.3, 0.7, inclusive="left")
```

## `cast(expr, dtype)`
source: https://docs.chalk.ai/api-docs#cast

Cast an expression to a different type.

**Parameters**

- `expr`: `Any`
  The expression to cast.
- `dtype`: `pa.DataType | type[Any]`
  The type to cast the expression to.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   user_id: "User.id"
   merchant_id: "Merchant.id"
   user_merchant_id: "UserMerchant.id" = (
       F.cast(_.user_id, str) + "_" +
       F.cast(_.merchant_id, str)
   )
```

## `coalesce(vals)`
source: https://docs.chalk.ai/api-docs#coalesce

Return the first non-null entry

**Parameters**

- `vals`: `Any` = `()`
  Expressions to coalesce. They can be a combination of underscores and literals, though types must be compatible (ie do not coalesce int and string).

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   nickname: str | None
   name: str | None
   name_or_nickname: str = F.coalesce(_.name, _.nickname, "")
```

## `distinct_from(value1, value2)`
source: https://docs.chalk.ai/api-docs#distinct_from

Check if two values are distinct from each other (including null semantics).

**Parameters**

- `value1`: `Underscore | Any`
  First value to compare.
- `value2`: `Underscore | Any`
  Second value to compare.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class DataComparison:
   id: str
   current_value: float | None
   previous_value: float | None
   has_changed: bool = F.distinct_from(_.current_value, _.previous_value)
```

## `is_null(expr)`
source: https://docs.chalk.ai/api-docs#is_null

Check if a value is null.

**Parameters**

- `expr`: `Any`
  The value to check for nullity.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   nickname: str | None
   missing_nickname: bool = F.is_null(_.nickname)
```

## `if_then_else(condition, if_true, if_false)`
source: https://docs.chalk.ai/api-docs#if_then_else

Create a conditional expression, roughly equivalent to:

` if condition: return if_true else: return if_false `

Unlike a Python if/else, all three inputs `(condition, if_true, if_false)` are evaluated in parallel for all rows, and then the correct side is selected based on the result of the condition expression.

**Examples**

```python
from chalk import _
from chalk.features import features
@features
class Transaction:
   id: int
   amount: int
   risk_score: float = _.if_then_else(
     _.amount > 10_000,
     _.amount * 0.1,
     _.amount * 0.05,
   )
```

## `recover(vals)`
source: https://docs.chalk.ai/api-docs#recover

Return the first valid entry. Functions like coalesce, but allows recovering from an upstream failure

**Parameters**

- `vals`: `Any` = `()`
  Expressions to recover. They can be a combination of underscores and literals, though types must be compatible (ie do not recover int and string).

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   potentially_error_causing_name: str | None
   fallback_name: str
   name: str = F.recover(_.potentially_error_causing_name, _.fallback_name)
```

## `when(condition)`
source: https://docs.chalk.ai/api-docs#when

Build a conditional expression.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   age: float
   age_group: str = (
     F.when(_.age < 1)
      .then("baby")
      .when(_.age < 3)
      .then("toddler")
      .when(_.age < 13)
      .then("child")
      .when(_.age < 18)
      .then("teen")
      .otherwise(F.cast(F.cast(F.floor(_.age / 10), int), str) + "0s")
  )
```
