Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of and and or logical operators in python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the example analysis of and and or logic operators in python, the content is very detailed, interested friends can refer to, hope to be helpful to you.

I. Overview

The logical operators and and or in python, also known as lazy evaluation, do not parse the code later as long as the value is determined.

II. Usage instructions (1) and usage

Text description:

The first case (judgment statement):

Parsing from right to left, as long as the first one is False, it is False, then there is no need for operation, the whole thing is wrong, whether it is right or wrong. Remember a sentence unless all judgment statements are True, the whole sentence is True, as long as there is one False, the whole sentence is False.

Examples are as follows:

Print (1 > 2 and 0

< 1)#Falseprint (1 >

2 and 0 > 1) # Falseprint (1

< 2 and 0 < 1)#Trueprint (1 >

2 and 0

< 1)#Falseprint (1 >

2 and 0

< 1 and 2 >

0 and 5 > 1) # False

The second case (when they are all numbers):

Parsed from right to left, 0 is False,1 and True. By the same token, remember that if there is no zero in a sentence, the whole sentence is True, but if one is 0, the whole sentence is 0.

Examples are as follows:

Print (0 and 5) # 0print (1 and 5) # 5print (5 and 0) # 0print (10 and 0 and 5 and 0) # 0print (6 and 4 and 3) # 3print (1 and 4 and 2 and 6) # 6 (II) or usage

Text description:

The first case (judgment statement):

Parse from left to right, in turn to see whether the statement is True, if it is True, the whole statement is True; is not, look at the next statement, the same, if not the whole statement is False.

Examples are as follows:

Print (1 > 2 or 0

< 1) #Trueprint (1 < 2 or 0 >

1) # Trueprint (1 > 2 or 0 > 1) # Falseprint (1

< 2 or 0 >

1 or 2 > 3) # True

The second case (when they are all numbers):

Parse from left to right, find 0, look at the next number. If it is not 0, the whole statement is the value that is not 0.

Examples are as follows:

Print (0 or 10) # 10print (1 or 2) # 1print (1 or 0) # 1print (7 or 5 or 5) # 7print (9 or 5 or 6 or 0 or 8) # 9 three, two sets and and or operation

Examples are as follows:

IV. Priority issues (and > or)

Examples are as follows:

Rint (1 and 5 or 0) # first calculate 1 and 5 as 5 and then calculate 5 or 0 as 5print (1 or 5 and 0 or 6 and 0) 'first calculate 5 and 0 as 0 and 0 6 and 0 the whole statement becomes 1 or 0 or 0 and the result is 1 or 0 attached: summary of if sentences and and or in python

1 the concept of the if statement: if a value or operation is true, deal with the true, otherwise deal with the false.

2. The logical operator Boolean and and Boolean or or Boolean non-notphp use the & & which represents the logical and

| | stands for logic or! Representative logic is not

3 if you want to judge the different values of a variable in if python, use switch () {case: default:} in if: python: else: php.

4. The comparison operator = = determines whether the two values are equal.

5 judge whether it belongs to an object or not

6, Boolean value: true false

This is the end of the example analysis of the and and or logical operators in python. I hope the above can be of some help and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report