# if statement in Python (if, elif, else)

The if statement (`if ... elif ... else ...`

) is used for conditional execution in Python.

- Basics of the if statement (
`if`

,`elif`

,`else`

) - Conditions with comparison operators
- Conditions with numbers, lists, etc.
- Conditions with Boolean operators (
`and`

,`or`

,`not`

) - How to write conditional expression on multiple lines

## Basics of the if statement (if, elif, else)

The basic form of the if statement is as follows:

```
if expression 1:
Executed when expression 1 is True
elif expression 2:
Executed when expression 1 is False and expression 2 is True
elif expression 3:
Executed when expression 1, 2 are False and expression 3 is True
...
else:
Executed when all expressions are False
```

`elif`

in Python is equivalent to `else if`

in C language.

The `elif`

and `else`

clauses can be omitted if there is only one conditional expression or if there is no need to execute for `False`

.

## Conditions with comparison operators

You can specify conditions with comparison operators.

The comparison operators in Python are as follows:

`x < y`

`x <= y`

`x > y`

`x >= y`

`x == y`

`x != y`

`x is y`

`x is not y`

`x in y`

`x not in y`

For example:

```
def if_test(num):
if num > 100:
print('100 < num')
elif num > 50:
print('50 < num <= 100')
elif num > 0:
print('0 < num <= 50')
elif num == 0:
print('num == 0')
else:
print('num < 0')
if_test(1000)
# 100 < num
if_test(70)
# 50 < num <= 100
if_test(0)
# num == 0
if_test(-100)
# num < 0
```

In Python, you can write something like `a < x < b`

.

```
def if_test2(num):
if 50 < num < 100:
print('50 < num < 100')
else:
print('num <= 50 or num >= 100')
if_test2(70)
# 50 < num < 100
if_test2(0)
# num <= 50 or num >= 100
```

`==`

, `!=`

are used to compare values, and `is`

, `is not`

are used to compare the identity of objects.

For example, when comparing the integer `int`

and the floating point number `float`

, if the values are equal, `==`

returns `True`

but `is`

returns `False`

because they are different objects.

```
i = 10
print(type(i))
# <class 'int'>
f = 10.0
print(type(f))
# <class 'float'>
print(i == f)
# True
print(i is f)
# False
```

By using `in`

and `not in`

, it is possible to make a condition whether a list or a string contains a specific element or substring.

```
def if_test_in(s):
if 'a' in s:
print('a is in string')
else:
print('a is NOT in string')
if_test_in('apple')
# a is in string
if_test_in('melon')
# a is NOT in string
```

## Conditions with numbers, lists, etc.

In the conditional expression of the if statement, you can specify an object such as a number or a list.

```
if 10:
print('True')
# True
if [0, 1, 2]:
print('True')
# True
```

The following objects are considered `False`

in Python.

- constants defined to be false:
`None`

and`False`

.- zero of any numeric type:
`0`

,`0.0`

,`0j`

,`Decimal(0)`

,`Fraction(0, 1)`

- empty sequences and collections:
`''`

,`()`

,`[]`

,`{}`

,`set()`

,`range(0)`

Built-in Types — Python 3.8.5 documentation

Everything else is considered `True`

.

You can use `bool()`

to check whether an object is considered `True`

or `False`

. Note that the string except for the empty string `''`

is considered `True`

, so the string `'False'`

is also considered `True`

.

```
print(bool(10))
# True
print(bool(0.0))
# False
print(bool([]))
# False
print(bool('False'))
# True
```

You can easily write conditions such as "when the list is empty".

```
def if_test_list(l):
if l:
print('list is NOT empty')
else:
print('list is empty')
if_test_list([0, 1, 2])
# list is NOT empty
if_test_list([])
# list is empty
```

## Conditions with Boolean operators (and, or, not)

You can combine multiple conditions by using the Boolean operators (`and`

, `or`

, `not`

).

```
def if_test_and_not(num):
if num >= 0 and not num % 2 == 0:
print('num is positive odd')
else:
print('num is NOT positive odd')
if_test_and_not(5)
# num is positive odd
if_test_and_not(10)
# num is NOT positive odd
if_test_and_not(-10)
# num is NOT positive odd
```

By short-circuit evaluation, `x and y`

and `x or y`

return either `x`

or `y`

, not `True`

or `False`

. As long as it is used in the conditional expression of if statement, its value is evaluated as either `True`

or `False`

, so there is no need to worry about it.

It is possible to use `and`

and `or`

multiple times.

```
def if_test_and_not_or(num):
if num >= 0 and not num % 2 == 0 or num == -10:
print('num is positive odd or -10')
else:
print('num is NOT positive odd or -10')
if_test_and_not_or(5)
# num is positive odd or -10
if_test_and_not_or(10)
# num is NOT positive odd or -10
if_test_and_not_or(-10)
# num is positive odd or -10
```

## How to write conditional expression on multiple lines

If you use multiple conditional expressions with `and`

or `or`

, the line may become too long.

By using backslash `\`

or enclosing it in parentheses `()`

, you can write a conditional expression on multiple lines.

```
def if_test_and_backslash(num):
if num >= 0 \
and not num % 2 == 0:
print('num is positive odd')
else:
print('num is NOT positive odd')
if_test_and_backslash(5)
# num is positive odd
def if_test_and_brackets(num):
if (num >= 0
and not num % 2 == 0):
print('num is positive odd')
else:
print('num is NOT positive odd')
if_test_and_brackets(5)
# num is positive odd
```

This is not limited to if statements and can be used anywhere in Python code. See the following post for details.