zoukankan      html  css  js  c++  java
  • Python 基础学习之if语句

    cars=['audi','xiali','bwm','benz',]
    ##根据要求bmw全部为大写,其他的首字母为大写
    for car in cars:
        if car=='bmw':
            print(car.upper())
        else:
            print(car.title())

    输入结果为:

    Audi
    Xiali
    Bwm
    Benz

    2.知识点2,检查特定值是否包含在列表中。

    使用关键字in

    例如:

    exampleList=['aaa','bbb','ccc']
    'aaa' in exampleList
    True
    'ddd' in exampleList
    False
    news=['NewYork Time','Router','Wind','xinhuashe']
    
    baoshe='renminribao'
    
    if baoshe not in news:
        print(baoshe.title()+",try try")

    返回结构为:Renminribao,try try

    3. and 和 or 

    age_1=10
    age_2=100
    if age_1>9 and age_2>90:
        print("age_1: "+str(age_1)) ## int to string  使用str(int),相反 string to int 使用 int(string)
        print("age_2: "+str(age_2))
        
    结果是:

    age_1: 10
    age_2: 100

     

    4.elif=else if

    ##测试5,使用 if-elif-else  其中elif=else if
    age=12;
    if age<12:
        print("erron");
    elif age==12:
        print("right")
    else:
        print("go")

    返回结果为:right

    5.使用多个列表

    example_begin=['a',b','c','d','e']
    example_end=['a','g','e']
    
    for list in example_begin:
        if list in example_end:
            print("包含:"+str(list))
        else:
            print("不包含:"+str(list))


    返回结果为:

    包含:a
    不包含:b
    不包含:c
    不包含:d
    包含:e

  • 相关阅读:
    回旋矩阵
    Python学习手记——了解Python
    My first Qt program
    10种使你的C程序更加高效的方法
    GCC指令
    缓冲思想
    Python学习手记——Whetting your appetite
    分割视图
    MSN Messenger 中的漏洞可能导致信息泄露 (838512)
    ASP.NET Popup Control
  • 原文地址:https://www.cnblogs.com/0909/p/9873812.html
Copyright © 2011-2022 走看看