Conditional Statements use the keywords if
, then
, and else
to perform different logic depending on the statements that are evaluated. Below are a couple examples:
A=5
if A==1:
print('I see one')
elif A==2:
print('I see two')
else:
print("I don't know")
I don't know
if True:
print('yep')
else:
print('nope')
yep
You can evaluate objects directly to make conditional decisions based on their contents. Some things typically register as False
, for example, such as empty objects.
if False:
print('yep')
else:
print('nope')
nope
if []:
print('yep')
else:
print('nope')
nope
if None:
print('yep')
else:
print('nope')
nope
if ():
print('yep')
else:
print('nope')
nope
if {}:
print('yep')
else:
print('nope')
nope
While non-empty objects usually register as True
.
if [1,2,3]:
print('yep')
else:
print('nope')
yep
if [None,None,None]:
print('yep')
else:
print('nope')
yep
And so forth…