Python's if Statement general format
if <test1>: <do something>elif: <do something>else: <do something>
- The condition of the IF statement can be expressed by > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than or equal) to indicate its relationship
Some examples
Example 1:
# -*- coding: UTF-8 -*-flag = Falsename = ‘luren‘if name == ‘python‘: flag = True print ‘welcome boss‘ else: print name 输出结果为:>>> luren
Example 2:
# -*- coding: UTF-8 -*-num = 5 if num == 3: print ‘boss‘ elif num == 2: print ‘user‘elif num == 1: print ‘worker‘elif num < 0: print ‘error‘else: print ‘roadman‘输出结果为:>>> roadman
Example 3:
# -*- coding: UTF-8 -*-num = 9if num >= 0 and num <= 10: print ‘hello‘else: print ‘undefine‘输出结果为:>>> hello
If-else Multi-wording
Cases:
a, b = 1, 21.常规if a>b: c = aelse: c = bprint c2.表达式c = a if a>b else bprint c3. 二维列表c = [b,a][a>b]print c
Python-based if statement