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

  • 相关阅读:
    HDU 3746 Cyclic Nacklace 环形项链(KMP,循环节)
    13 python 常用的内置方法介绍
    12、反射方法
    11 绑定方法与非绑定方法
    10 python 封装----@property的用法
    9 python 多态与多态类
    8 python 抽象类
    7 python 类的组合
    4.1、内建函数
    6 python 继承与派生
  • 原文地址:https://www.cnblogs.com/0909/p/9873812.html
Copyright © 2011-2022 走看看