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

  • 相关阅读:
    【洛谷P1119】灾后重建
    【洛谷P1462】通往奥格瑞玛的道路
    【洛谷P1991】无线通讯网
    poj 2892(二分+树状数组)
    hdu 1541(树状数组)
    hdu 5059(模拟)
    hdu 5056(尺取法思路题)
    poj 2100(尺取法)
    hdu 2739(尺取法)
    poj 3320(尺取法)
  • 原文地址:https://www.cnblogs.com/0909/p/9873812.html
Copyright © 2011-2022 走看看