>>> a = 5 >>> print(6) if a>3 else print(5) 6 >>> print(6 if a>3 else 5) 6 >>> b=6 if a>13 else 9 >>> b 9 >>> x = math.sqrt(9) if 5>3 else random.randint(1,100)# 还未导入math和random模块 Traceback (most recent call last): File "", line 1, in <module> NameError: name ‘math‘ is not defined >>> import math >>> x = math.sqrt(9) if 5>3 else random.randint(1,100) >>> x = math.sqrt(9) if 2>3 else random.randint(1,100) Traceback (most recent call last): File "", line 1, in <module> NameError: name ‘random‘ is not defined >>> import random >>> x = math.sqrt(9) if 2>3 else random.randint(1,100) >>> x 89
>>> funcDict = {‘1‘:lambda:print(‘You input 1‘),‘2‘:lambda:print(‘You input 2‘),‘3‘:lambda:print(‘You input 3‘)} >>> x = input(‘Input an integer to call different function:‘) Input an integer to call different function:3 >>> func = funcDict.get(x,None) >>> if func: ... func() ... else: ... print(‘Wrong integer‘) ... You input 3