What does "if var" mean in python?

Viewed 27892

This is a function in python to validate a day entry.

def valid_day(day):
    if day and day.isdigit():#if day
        day = int(day)
        if day > 0 and day <= 31:
            return day

I want to know what the expression if day means (in terms of semantics).

I only know the effect of if on boolean expressions not on variables like integers or arrays.

7 Answers

As others have already provided explanations, I am just going to give some examples to make it clear.

# bool('') is False
if not '':
  print('Empty String')

# bool([]) is False
if not []:
  print('Empty list')

# bool(None) is False
if not None:
  print('None type')

# bool(0) is False
if not 0:
  print('Zero Value')

Related