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

How to use Boolean expressions in Python

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

Share

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

Editor to share with you how to use Boolean expressions in Python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

1. The concept of Boolean expression

Both conditional statements and loop statements use Boolean expressions as conditions. Boolean values are true or false, represented by False and True. Boolean expressions are often used to compare two values, such as: while x > = 0

2. Demonstration of logic problems

True and True

False and True

1 = 1 and 2 = 1

"test" = = "test"

1 = 1 or 2! = 1

True and 1 = = 1

False and 0! = 0

True or 1 = = 1

"test" = = "testing"

1! = 0 and 2 = = 1

"test"! = "testing"

"test" = 1

Not (True and False)

Not (1 = = 1 and 0! = 1)

Not (10 = = 1 or 1000 = = 1000)

Not (1! = 10 or 3 = = 4)

Not ("testing" = = "testing" and "Zed" = = "Cool Guy")

1 = 1 and (not ("testing" = = 1 or 1 = = 0))

"chunky" = "bacon" and (not (3 = = 4 or 3 = = 3))

3 = = 3 and (not ("testing" = = "testing" or "Python" = = "Fun"))

All Boolean logical expressions can be obtained using the following simple process:

(1) find the equal judgment part (= = or! =) and rewrite it to its final value (True or False).

(2) find the and/or in parentheses and calculate their values first.

(3) find each not and calculate their reverse value.

(4) find the rest of the and/or and solve their values.

(5) when you are done, the remaining result should be True or False.

Let's demonstrate with a logical expression of 20 lines:

3! = 4 and not ("testing"! = "test" or "Python" = = "Python")

Next you'll see how this complex expression is solved step by step into a single result:

1. > work out each equivalence judgment:

> a. 3! = 4 is True: True and not ("testing"! = "test" or "Python" = = "Python") b.

"testing"! = "test" is True: True and not (True or "Python" = = "Python") c.

"Python" = "Python" is True: True and not (True or True)

1. > find each and/or in parentheses:

> a. (True or True) is True: True and not (True)

1. Find each not and reverse it: > a. Not (True) is False: True and False

1. Find the rest of the and/or and solve their values: > > a. True and False is False

In this way, we find that its final value is False.

3. The skills of sorting out complex logic

Here is a shortcut to determine the value of a Boolean expression. Any and expression that contains a False result is False, and any or expression has a True result that is True, and you can get the result here, but make sure you can handle the whole expression, because this is a useful skill later.

These are all the contents of the article "how to use Boolean expressions in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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