# Conditions#

Python supports the usual logical conditions from mathematics:

• Equals: `a == b`

• Not Equals: `a != b`

• Less than: `a < b`

• Less than or equal to: `a <= b`

• Greater than: `a > b`

• Greater than or equal to: `a >= b`

Example:

``` 1#!/usr/bin/env python3
2
3def main():
4    a = 33
5    b = 200
6    if b > a:
7        print("b is greater than a")
8
9
10if __name__ == "__main__":
11    main()
```

Indentation

``` 1#!/usr/bin/env python3
2
3def main():
4    a = 33
5    b = 200
6    if b > a:
7    print("b is greater than a") # you will get an error
8
9
10if __name__ == "__main__":
11    main()
```

## if-then-elif#

Example:

``` 1#!/usr/bin/env python3
2
3def main():
4    a = 33
5    b = 33
6    if b > a:
7        print("b is greater than a")
8    elif a == b:
9        print("a and b are equal")
10
11
12if __name__ == "__main__":
13    main()
```

## if-then-else#

Example:

``` 1#!/usr/bin/env python3
2
3def main():
4    a = 200
5    b = 33
6    if b > a:
7        print("b is greater than a")
8    else:
9        print("b is not greater than a")
10
11
12if __name__ == "__main__":
13    main()
```

Example:

``` 1#!/usr/bin/env python3
2
3def main():
4    a = 200
5    b = 33
6    if b > a:
7        print("b is greater than a")
8    elif a == b:
9        print("a and b are equal")
10    else:
11        print("a is greater than b")
12
13
14if __name__ == "__main__":
15    main()
```

## Nested if-then-else#

Example:

``` 1#!/usr/bin/env python3
2
3def main():
4    x = 41
5    if x > 10:
6        print("Above ten,")
7    if x > 20:
8        print("and also above 20!")
9    else:
10        print("but not above 20.")
11
12
13if __name__ == "__main__":
14    main()
```

## Logical operators#

And

Example:

``` 1#!/usr/bin/env python3
2
3def main():
4    a = 200
5    b = 33
6    c = 500
7    if a > b and c > a:
8        print("Both conditions are True")
9
10
11if __name__ == "__main__":
12    main()
```

Or

Example:

``` 1#!/usr/bin/env python3
2
3def main():
4    a = 200
5    b = 33
6    c = 500
7    if a > b or a > c:
8        print("At least one of the conditions is True")
9
10
11if __name__ == "__main__":
12    main()
```

## Shorthand and Conditional Expressions#

Example:

``` 1#!/usr/bin/env python3
2
3def main():
4    a = 2
5    b = 330
6    if a > b: print("a is greater than b")
7
8
9if __name__ == "__main__":
10    main()
```

Example:

``` 1#!/usr/bin/env python3
2
3def main():
4    a = 2
5    b = 330
6    print("A") if a > b else print("B")
7
8
9if __name__ == "__main__":
10    main()
```

Ternary Conditional Operator

Example:

``` 1#!/usr/bin/env python3
2
3def main():
4    a = 2
5    b = 330
6    print("A") if a > b else print("B")
7
8
9if __name__ == "__main__":
10    main()
```

Example:

```1#!/usr/bin/env python3
2
3def main():
4    b = 330
5    print("A") if a > b else print("=") if a == b else print("B")
6
7
8if __name__ == "__main__":
9    main()
```