本文共 4567 字,大约阅读时间需要 15 分钟。
Python if else and elif are keywords for conditional logic in the program. In this tutorial we are going to learn about python if
, else
and elif
. Previously we learned about .
Python if else和elif是程序中条件逻辑的关键字。 在本教程中,我们将学习python if
, else
和elif
。 之前,我们了解了 。
Well, so far we dealt with a static decision program. That means in our program we didn’t have to choose between any options. But what if we have to make our program behave differently in different conditions. That’s where we will use conditional logic. So conditional logic is how we can make a logical decision in a program.
好了,到目前为止,我们已经处理了一个静态决策程序。 这意味着在我们的程序中,我们不必选择任何选项。 但是,如果我们必须使我们的程序在不同条件下表现不同,该怎么办。 那就是我们将使用条件逻辑的地方。 因此,条件逻辑是我们如何在程序中做出逻辑决策的方式。
To implement conditional logic, Python’s keywords are if
, else
and elif
.
为了实现条件逻辑,Python的关键字是if
, else
和elif
。
Suppose we want to write a program, that will determine whether a number is odd or even. If the number is odd, we want to print – “the number is odd” and if the number is even we want to print – “the number is even”. We can write this program using if
keyword.
假设我们要编写一个程序,该程序将确定数字是奇数还是偶数。 如果数字是奇数,我们要打印–“数字是奇数”;如果数字是偶数,我们要打印–“数字是偶数”。 我们可以使用if
关键字编写该程序。
n=input() #take a input from usern=int(n) #typecast the raw input into integer#check if n is odd or even#logic for odd/even is-#if we divide an even number by 2, the remainder will be zero#if we divide an odd number by 2, the remainder will be one#we can perform this logic with modulus operator (%)if n%2==0: #(n%2) is the remainder.Check if it's zero print("the number is even")if n%2==1: #Check the remainder is one print("the number is odd")
If we execute this program and give input 2, the output will be like the below image.
如果执行该程序并输入2,则输出将类似于下图。
Also, if we run the program again and give input 3, the output will be like below.
另外,如果我们再次运行该程序并输入3,则输出将如下所示。
Pretty cool, right? As if we have made an intelligence 😉
很酷吧? 仿佛我们已经有了智慧😉
Well, in the above scenario, the condition we have put, n%2
, which has only two possible outcome. Either it’s zero or one. So here we can use else
for the second condition. In that case we don’t have to write the second condition manually. We can write the first condition using a if
and use else
for other case.
好了,在上述情况下,我们提出的条件n%2
仅有两个可能的结果。 它是零或一。 因此,在这里我们可以将else
用于第二个条件。 在这种情况下,我们不必手动编写第二个条件。 我们可以使用if
编写第一个条件if
并在其他情况下使用else
。
n=input() #take a input from usern=int(n) #typecast the raw input into integer#check if n is odd or even#logic for odd/even is-#if we divide an even number by 2, the remainder will be zero#if we divide an odd number by 2, the remainder will be one#we can perform this logic with modulus operator (%)if n%2==0: #(n%2) is the remainder.Check if it's zero print("the number is even")else: #this will consider every other case without the above-mentioned condition in if print("the number is odd")
What if we have to write a program that will have to handle three or more conditions. Suppose, you have to take a number from the user and consider these three cases.
如果我们必须编写一个必须处理三个或更多条件的程序该怎么办。 假设您必须从用户那里获取一个号码并考虑这三种情况。
So, in this scenario, we have to use if
for the first condition and else
for the last condition. That much we know till now. So what about the other two? We will use elif
to specify the other condition just like if
.
因此,在这种情况下,我们必须将if
用于第一个条件,将else
用于最后一个条件。 到目前为止,我们还不知道这些。 那么其他两个呢? 我们将使用elif
来指定其他条件,就像if
一样。
n=input() #take a input from usern=int(n) #typecast the raw input into integer#Check If the number is between 1 to 10if n>=1 and n<=10: print("too low");#Check If the number is between 11 to 20elif n>=11 and n<=20: print("medium"); #Check If the number is between 21 to 30elif n>=21 and n<=30: print("large");#Check if the number is greater than 30 else: print("too large")
If we run this program for values 3, 15, 23, 45 respectively, the output will be like this-
如果我们分别为值3、15、23、45运行此程序,则输出将如下所示:
So, that’s about conditional logic in Python. Make sure you run every piece of code on your own. Also, it is a better practice to make some problems on your own and do those.
#happy_coding 🙂因此,这与Python中的条件逻辑有关。 确保自己运行每段代码。 另外,最好自己动手做一些问题。
#happy_coding🙂翻译自:
转载地址:http://chlzd.baihongyu.com/