Python if else elif

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 Operators and Precedence.

Python if else和elif是程序中条件逻辑的关键字。 在本教程中,我们将学习python ifelseelif 。 之前,我们了解了Python运算符和优先级

Python否则-条件逻辑 (Python if else – conditional logic)

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的关键字是ifelseelif

Python如果 (Python if)

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 user

n=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.

Python tutorial, python if else example

另外,如果我们再次运行该程序并输入3,则输出将如下所示。

Pretty cool, right? As if we have made an intelligence 😉

很酷吧? 仿佛我们已经有了智慧😉

Python其他 (Python else)

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 user

n=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")

Python Elif (Python elif)

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.

如果我们必须编写一个必须处理三个或更多条件的程序该怎么办。 假设您必须从用户那里获取一个号码并考虑这三种情况。

  1. If the number is between 1 to 10 – print “too low”

    如果数字在1到10之间–打印“太低”
  2. If the number is between 11 to 20 – print “medium”

    如果数字介于11到20之间–请打印“中”
  3. If the number is between 21 to 30 – print “large”

    如果数字介于21到30之间–请打印“大”
  4. If the number is greater than 30 – print “too large”

    如果数字大于30 –打印“太大”

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 user

n=int(n)  #typecast the raw input into integer

#Check If the number is between 1 to 10
if n>=1 and n<=10:
    print("too low");

#Check If the number is between 11 to 20
elif n>=11 and n<=20:
    print("medium");   

#Check If the number is between 21 to 30
elif 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-

Python if else elif example

如果我们分别为值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🙂

翻译自: https://www.journaldev.com/14092/python-if-else-elif

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值