概述
python关键字和保留字
Python或关键字 (Python or keyword)
or is a keyword (case-sensitive) in python, it is a logical operator actually, it is used to validate more than one conditions. It is similar to Logical OR (||) operator in C, C++ programming. It requires a minimum of two conditions and returns True – if one or more condition(s) is/are True.
还是 python中的一个关键字(区分大小写),实际上它是一个逻辑运算符,用于验证多个条件。 它类似于C , C ++编程中的逻辑OR(||)运算符。 它至少需要两个条件并返回True –如果一个或多个条件为True 。
Truth table for "or" keyword/operator
“或”关键字/运算符的真值表
Condition1 Condition2 (Condition1 or Condition2)
True True True
True False True
False True True
False False False
Syntax of or keyword/operator:
或关键字/运算符的语法:
condition1 or condition2
Example:
例:
Input:
a = 10
b = 20
# conditions
print(a>=10 or b>=20)
print(a>10 or b>20)
Output:
True
False
或运算子的Python范例 (Python examples of or operator)
Example1: Take two numbers and test the conditions using or operator
例1:取两个数字并使用或运算符测试条件
# python code to demonstrate example of
# or keyword/operator
a = 10
b = 20
# printing return values
print(a>=10 or b>=20)
print(a>10 or b>20)
print(a==10 or b==20)
print(a==10 or b!=20)
Output
输出量
True
False
True
True
Example 2: Input a number and check whether it is divisible by 2 or 3
示例2:输入一个数字并检查它是否可以被2或3整除
# Input a number and check whether
# it is divisible by 2 or 3
# input
num = int(input("Enter a number: "))
# checking num is divisible by 2 or 3
if num%2==0 or num%3==0:
print("Yes, ", num, " is divisble by 2 or 3")
else:
print("No, ", num, " is not divisble by 2 or 3")
Output
输出量
First run:
Enter a number: 66
Yes, 66 is divisble by 2 or 3
Second run:
Enter a number: 5
No, 5 is not divisble by 2 or 3
Example 3: Input a string and check whether it is "Hello" or "World"
示例3:输入一个字符串并检查它是“ Hello”还是“ World”
# Input a string and check
# whether it is "Hello" or "World"
# input
string = input("Enter a string: ")
# checking the conditions
if string=="Hello" or string=="World":
print("Yes, "", string, "" is either "Hello" or "World"")
else:
print("No, "", string, "" is neither "Hello" nor "World"")
Output
输出量
First run:
Enter a string: Hello
Yes, " Hello " is either "Hello" or "World"
Second run:
Enter a string: World
Yes, " World " is either "Hello" or "World"
Third run:
Enter a string: IncludeHelp
No, " IncludeHelp " is neither "Hello" nor "World"
翻译自: https://www.includehelp.com/python/or-keyword-with-example.aspx
python关键字和保留字
最后
以上就是谨慎墨镜为你收集整理的python关键字和保留字_或带有Python示例的关键字的全部内容,希望文章能够帮你解决python关键字和保留字_或带有Python示例的关键字所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复