python_选择结构

>>> if 3>2:print('ok')

ok
>>> if True:print(3);print(5)

>>> chTesst=['1','2','3','4','5']
>>> if chTesst:
print(chTesst)
else:
print('Empty')

['1', '2', '3', '4', '5']
>>> a=5

>>>
>>>

>>> import math
>>> x=math.sqrt(9) if 5>3 else random.randint(1,100)
>>> x
3.0
>>> x=3

>>> (1 if x>2 else 0) if x>5 else ('a' if x<5 else 'b')
'a'
>>> def func(score):
if score > 100:
return 'wrong score.must<=100.'
elif score >=90:
return 'A'
elif score >=80:
return 'B'
elif score >=70:
return 'C'
elif score >=60:
return 'D'
elif score >=0:
return 'E'
else:
return 'wrong score.must >0'

>>> func(120)
'wrong score.must<=100.'
>>> func(-10)
'wrong score.must >0'
>>> def func1(score):
degree='DCBAAE'
if score>100 or score<0:
return 'wrong score.must between 0 and 100.'
else:
index=(score-60)//10
if index>=0:
return degree[index]
else:
return degree[-1]

>>> func(10)
'E'
>>> func(70)
'C'
>>> func(100)
'A'

>>> func(90)
'A'
>>> age=24
>>> subject='computer'
>>> college='non-key'
>>> if (age>24 and subject=='computer') or (college=='key point' and subject=='computer') or (age<=28 and subject=='computer'):
print("Congratulations, you have my company's interview")
else:
print("I'm sorry, you do not meet the requirements of the interview")

Congratulations, you have my company's interview
>>> numbers=[]

>>> if year%400==0 or (year%4==0 and year%100!=0):
day_month[1]=29
if month==1:
print(day)
else:
print(sum(day_month[:month-1])+day)

>>> if year%400==0 or (year%4==0 and year%100!=0):
day_month[1]=29
if month==1:
print(day)
else:
print(sum(day_month[:month-1])+day)

>>>
>>> date
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=23, tm_hour=20, tm_min=14, tm_sec=48, tm_wday=1, tm_yday=143, tm_isdst=0)
>>> import datetime
>>> Today=datetime.date.today()
>>> Today
datetime.date(2017, 5, 23)

上一篇:第五篇 SQL Server安全架构和安全


下一篇:剑指offer-面试题11.数值的整数次方