• python_选择结构


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

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

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


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

    >>> a=5
    >>> print(6)if a>3 else print(5)
    6
    >>> b=6 if a>13 else 9
    >>> b
    9

    >>> 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)


    143

    >>> year
    2017
    >>> 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)

  • 相关阅读:
    几种简单的博弈 1
    Luogu P2789 直线交点数
    搜索题简记
    并查集的妙用
    [qbzt寒假]线段树和树状数组
    [qbzt寒假]hash
    [qbzt寒假]Trie字典树
    博客阅读须知
    洛谷P1842 [USACO05NOV]奶牛玩杂技——题解
    2020SDOI游记
  • 原文地址:https://www.cnblogs.com/cmnz/p/6896129.html
Copyright © 2020-2023  润新知