zoukankan      html  css  js  c++  java
  • python_逻辑运算符

    python_逻辑运算符

    在程序开发中,通常 在判断条件时,会需要同时判断多个条件
    只有多个条件都满足,才能够执行后续代码,这个时候需要使用到 逻辑运算符
    逻辑运算符,可以把 多个条件 按照 逻辑 进行 连接,变成 更复杂的条件
    python中的 逻辑运算符 包括: 与and/ 或or/ 非not 三种

    --------------------------------------------

    and
    条件1 and 条件2
    两个条件同时满足时,返回 true
    只要有一个条件不满足时,返回 false

    1 #练习1:定义一个整数变量 age,编写代码判断年龄是否正确
    2 #要求:人的年龄在 0-120 之间
    3 
    4 age = int(input("请输入年龄:"))
    5 
    6 if age >=0 and age <= 120:
    7     print("年龄正确")
    8 else:
    9     print("年龄非法")

    --------------------------------------------

    or
    条件1 or 条件2
    两个条件只要有一个满足,返回 true
    两个条件都不满足,返回 false

     1 #练习2:定义两个整数变量 python_score、c_score,编写代码判断成绩
     2 #要求:只要有一门成绩 > 60分就算合格;
     3 
     4 python_score = int(input("请输入python成绩:"))
     5 c_score = int(input("请输入C语言成绩:"))
     6 
     7 if python_score >= 60 or c_score >= 60:
     8     print("及格")
     9 else:
    10     print("不及格")

    --------------------------------------------

    not
    not 条件 //其实就是取反
    非/不是

    1 #练习3:定义一个布尔型变量 is_employee,编写代码判断是否是本公司员工
    2 #要求:如果不是提示 不允许入内
    3 
    4 is_employee = True
    5 
    6 if not is_employee:
    7     print("非本公司人员,请勿入内!")
    8 else:
    9     print("请进入!")
  • 相关阅读:
    LeetCode "Jump Game"
    LeetCode "Pow(x,n)"
    LeetCode "Reverse Linked List II"
    LeetCode "Unique Binary Search Trees II"
    LeetCode "Combination Sum II"
    LeetCode "Divide Two Integers"
    LeetCode "First Missing Positive"
    LeetCode "Clone Graph"
    LeetCode "Decode Ways"
    LeetCode "Combinations"
  • 原文地址:https://www.cnblogs.com/shao-null/p/9174307.html
Copyright © 2011-2022 走看看