龙空技术网

零基础入门学习Python(四):短路逻辑和运算符优先级

LearningYard学苑 63

前言:

如今小伙伴们对“python短路”大致比较关注,小伙伴们都需要剖析一些“python短路”的相关文章。那么小编在网摘上搜集了一些对于“python短路””的相关文章,希望朋友们能喜欢,大家一起来学习一下吧!

分享兴趣,传播快乐,

增长见闻,留下美好!

亲爱的您,这里是LearningYard新学苑。

今天小编为大家带来的是

零基础入门学习Python(四):

短路逻辑和运算符优先级

Share interest,

spread happiness,

increase knowledge,

and leave beautiful.

Dear, this is the LearingYard Academy!

Today, the editor brings the

“Introduction to Python (4):

Short Circuit Logic and Operator Priorities”.

Welcome to visit!

一、思维导图

此推文关于零基础入门学习Python系列的内容主要如下:

The main content of this tweet about the exercise series is as follows:

本次推文通过语法知识和案例的结合来为大家讲解Python,但需要注意的是代码还是需要经常敲才能熟练。

This tweet combines grammar knowledge and case studies to explain Python to everyone, but it should be noted that the code still requires frequent typing to become proficient.

二、布尔类型

需要注意的是只有空字符串结果才为False,只有等值于0结果才为False.请看下面示例:

Note that only the empty string result is false, and only the result equivalent to 0 is false. See the following example:

在这里False先充当字符串,所以第一个结果为True.

False serves as the string first, so the first result is True

下面给出结果为False的示例:

Below is an example where the result is False:

and与or为逻辑运算符,要知道的是运行结果不一定为False或者True,还可能是数字。

And and or are Logical connective. It is important to know that the running result may not be false or true, but may be a number.

三、短路逻辑

and需要判断两侧(第一个值为True,第二个值影响结果;第一个值为False,则直接抛出结果)。

And needs to determine both sides (the first value is True, the second value affects the result; if the first value is False, the result is thrown directly).

or只要第一个为True就为True(第一个为True,直接抛出结果;第一个为False,则第二个影响结果)。

As long as the first one is true, it is true (if the first one is true, the result is directly thrown; if the first one is false, the second one affects the result).

四、运算符优先级

Python运算符优先级顺序从高到低依次为算术运算符>位运算符>比较运算符>赋值运算符>布尔运算符。在算术运算符中,乘除法优先于加减法,而取余、取模、求幂次方这几个特殊的运算符优先级是一致的。Python的逻辑运算符则是有三个,它们的运算符优先级顺序从低到高分别为or、and、not。最后的赋值运算符只有一个等号,所以永远最后执行。

The priority order of Python operators from high to low is arithmetic operators>bitwise operators>comparison operators>assignment operators>Boolean operators. In arithmetic operators, multiplication and division have priority over addition and subtraction, while the special operators of remainder, modulus, and power have the same priority. There are three Logical connective in Python, and their operator priority order from low to high is or, and, and not. The final assignment operator only has an equal sign, so it is always executed last.

今天的分享就到这里了。

如果您对今天的文章有独特的想法,

欢迎给我们留言,

让我们相约明天,

祝您今天过得开心快乐!

That's all for today's sharing.

If you have a unique idea about the article,

please leave us a message, and let us meet tomorrow.

I wish you a nice day!

翻译:百度翻译

参考:百度、哔哩哔哩、小红书

声明:本文由LearningYard新学苑原创,若有侵权请联系删除

文案&排版:易春秀

审核:许江越

标签: #python短路