python范例
Python if关键字 (Python if keyword)
if is a keyword (case-sensitive) in python, it is used to define an "if statement" block that executes, if a given condition is true. if keyword is a part of conditional statements in python programming languages and it is used with the various type of if-else conditional statements.
if是python中的一个关键字(区分大小写),如果给定条件为true,则用于定义执行的“ if语句”块。 if关键字是python编程语言中条件语句的一部分,它与各种类型的if-else条件语句一起使用。
Syntax of if keyword
if关键字的语法
if test_condition:
statement(s)
Here, if test_condition is True, then statement(s) will be executed.
在这里,如果test_condition为True ,则将执行语句 。
Example:
例:
Input:
num = 0
# condition
if num==0:
print("Yes! ", num, " is equal to 0")
Output:
Yes! 0 is equal to 0
if关键字的Python示例 (Python examples of if keyword)
Example 1: Check whether a given number is equal to 0.
示例1:检查给定数字是否等于0。
# python code to demonstrate example of
# if keyword
# Check whether a given number is equal to 0
# number
num = 0
if num==0:
print("Yes! ", num, " is equal to 0")
Output
输出量
Yes! 0 is equal to 0
Example 2: Check whether a given number is positive, negative or zero (using if keyword only).
示例2:检查给定数字是正数,负数还是零(仅在关键字中使用)。
# python code to demonstrate example of
# if keyword
# Check whether a given number is
# positive, negative or zero
# number
num = 20
if num>0:
print(num, " is positive")
if num<0:
print(num, " is negavtive")
if num==0:
print(num, " is zero")
Output
输出量
20 is positive
翻译自: https://www.includehelp.com/python/if-keyword-with-example.aspx
python范例