Python 3x Pandas Django

If ... Else conditional logic

The if-else statement in python used to do the operations based on some specific conditions. The operations specified in if block executes when if condition satisfied otherwise else block executes.

Syntax

if (condition):
    .....
    .....
else:
    .....

Flow Diagram

image missing

There are the following variants of if statement in python language.

  1. If statement
  2. If-else statement
  3. If elif ladder

1. If statement syntax

if (condition):
    .....
    .....

Code Snippet

if (4 < 5):
    print("5 is greater than 4")  #Output: 5 is greater than 4

2. If-else statement

else: block is optional, this block will execute when if block is not satisfied

if (condition):
    .....
    .....
else:
    .....

Code Snippet

if (4 < 5):
    print("5 is greater than 4")  #Output: 5 is greater than 4
else:
    print("4 is lesser than 5")

3. If elif ladder

else: block is optional, this block will execute when none of the above conditions satisfied

if (condition):
    .....
    .....
elif(condition):
    .....
elif(condition):
    .....
else:
    ......

Code Snippet

mark = 70
if (mark == 70):
    print("D Grade")  #Output: D Grade
elif(mark == 80):
    print("C Grade")
elif(mark == 90):
    print("B Grade")
elif(mark == 100):
    print("A Grade")

1. If statement syntax

if (condition):
    .....
    .....

Code Snippet

if (4 < 5):
    print("5 is greater than 4")    #Output: 5 is greater than 4

If ... Else conditions with 'and' operators

a = 200
b = 33
c = 500
if (a > b) and (c > a):
  print("Both conditions are True")  #Output: Both conditions are True

If ... Else conditions with 'or' operators

a = 200
b = 33
c = 10
if (a > b) or (c > a):
  print("Both conditions are True")  #Output: Both conditions are True


Truthy and Falsey

In Python, every value will be evaluated to either True or False.

The basic rules are:

1. Values that evaluate to False are considered Falsy.

2. Values that evaluate to True are considered Truthy.

According to the Python Documentation:

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below (and, or, not).

All values are considered "truthy" except for the following, which are "falsey":

1. None
2. False
3. 0
4. 0.0
5. 0j
6. Decimal(0)
7. Fraction(0, 1)
8. [] - an empty list
9. {} - an empty dict
10. () - an empty tuple
11. '' - an empty str
12. b'' - an empty bytes
13. set() - an empty set
14. an empty range, like range(0)
15. obj.__bool__() returns False
16. obj.__len__() returns 0

How to determine a value is Truthy or Falsy ? by using bool() built-in Python function

Example

from decimal import Decimal
from fractions import Fraction

#************************* Falsey Values ***************************

print(bool(None))
print(bool(False))
print(bool(0))
print(bool(0.0))
print(bool(0j))
print(bool(Decimal(0)))
print(bool(Fraction(0, 1)))
print(bool([]))
print(bool({}))
print(bool(()))
print(bool(''))
print(bool(b''))
print(bool(set()))
print(bool(range(0)))

#************************* Truthy Values ***************************

print(bool(True))
print(bool(1))
print(bool(23.3))
print(bool(2j))
print(bool(Decimal(3)))
print(bool(Fraction(1, 1)))
print(bool([1, 2, 3]))
print(bool({3, 2, 1}))
print(bool((1, 2, 3)))
print(bool('any letter'))
print(bool(b'any letter'))
print(bool(set([4,2])))
print(bool(range(1)))

Try it yourself

Walrus Operator :=

Walrus Operator assigns values to variables as part of a larger expression.

If block

a = "Hellooooooooooooooo"
if len(a) > 10:
   print(f"List is too long {len(a)} elements")   #Output: List is too long 19 elements

In the above example, we have used len(a) expression for finding the length of the element in multiple places.

By using the walrus operator, we can assign this expression to variables.

a = "Hellooooooooooooooo"
if (str_len := len(a)) > 10:
   print(f"List is too long {str_len} elements")   #Output: List is too long 19 elements


If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!