if 'one' < 'two': print('one < two')
if 'one' < 'two': print('one < two') else: print('one >= two')
a = 1 b = 1 if a < b: print('a < b' ) elif a > b: print('a > b' ) else: print('a == b')
… ? … : …
, as found in C related languages, can be simulated in Python with … if … else …
: val = 42 result = 'yes' if val == 42 else 'no' print(result) val = 99 result = 'yes' if val == 42 else 'no' print(result)
if
statement: def half(n): r = n/2 return r if int(r) == r else None def F(n): if h := half(n): print('half({}) = {}'.format(n,h)) else: print('{} is odd'.format(n)) F(42) F(99)
if
can also be used in list comprehensions to select the elements in a list that meet the given condition: result = [ expr(x) for x in someList if criterion(x)]
if
or while
statement. The rules if an object is considered to be True
or False
are described here. else
clause