博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python if else elif
阅读量:2533 次
发布时间:2019-05-11

本文共 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 ifelseelif 。 之前,我们了解了 。

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

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

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 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-

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🙂

翻译自:

转载地址:http://chlzd.baihongyu.com/

你可能感兴趣的文章
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>