zoukankan      html  css  js  c++  java
  • Python if语句

    1 简单示例

    colors = ("red", "blue", "yellow", "orange", "white", "pink", "brown")
    for color in colors:
        if color == "blue":
            print("我喜欢蓝色")
        else:
            print(color.upper())

    2 相等,不想等运算符

    str1 = "abc"
    str2 = "ABC"
    print(str1 == str2)
    print(str1 != str2)

    3 与和或用and和or

    str1 = "abc"
    str2 = "ABC"
    print(str1 == "abc" and str2 == "ABC")
    print(str1 == "abc" or str2 == "BBC")

    4 检查特定值是否包含或不包含在列表中

    colors = ("red", "blue", "yellow", "orange", "white", "pink", "brown")
    print("red" in colors)
    print("green" not in colors)

    5 if-elif-else

    colors = ("red", "blue", "yellow", "orange", "white", "pink", "brown")
    for color in colors:
        if "red" == color:
            print(color + " 我喜欢")
        elif "blue" == color:
            print(color + " 我最喜欢")
        elif "green" == color:
            print(color + " 是春天的颜色")
        else:
            print(color + " 没感觉")

    6 判断列表是否为空

    # 列表中如果有元素,if语句为true
    colors = []
    if colors:
        print("列表中有元素")
    else:
        print("列表中没有元素")
  • 相关阅读:
    20151224--
    20151223--联系人项目
    20151222--Ajax三级无刷新
    20151221--三级有刷新联动
    20151220--导航前四问已解答
    递归
    Request和Response详解
    无刷新三级联动查询
    20151219--导航自己制作的一部分
    151030
  • 原文地址:https://www.cnblogs.com/liyunfei0103/p/10153045.html
Copyright © 2011-2022 走看看