Stupid way to learn Python (28)

Source: Internet
Author: User

Exercise 28: Boolean expression exercises

The formal name of the logical combination you learned in the previous section is "Boolean logical expression". In programming, Boolean logic can be said to be ubiquitous. They are the basis and important part of computer operations, and mastering them is just as important as learning music to master scales.

In this exercise, you will use the logical expressions learned in the previous section in python . Write the answer for each of the following logic questions, either True or False for each question. When you're finished, you'll need to run python and enter these logic statements to make sure you're writing the correct answer.

  1. True and true
  2. False and True
  3. 1 = = 1 and 2 = = 1
  4. "Test" = = "Test "
  5. 1 = = 1 or 2! = 1
  6. True and 1 = = 1
  7. False and 0! = 0
  8. True or 1 = = 1
  9. "Test" = = "Testing"
  10. 1! = 0 and 2 = = 1
  11. "Test"! = "Testing"
  12. "Test" = = 1
  13. Not (True and False)
  14. Not (1 = = 1 and 0! = 1)
  15. Not ( = = 1 or + = = )
  16. Not (1! = Ten or 3 = = 4)
  17. Not ("testing" = = "Testing" and "Zed" = = "Cool guy ")
  18. 1 = = 1 and not ("testing" = = 1 or 1 = = 0)
  19. "chunky" = = "Bacon" and not (3 = = 4 or 3 = = 3)
  20. 3 = = 3 and not ("testing" = = "Testing" or "Python" = = "Fun")

At the end of this section I will give you a technique to clarify complex logic.

All Boolean logical expressions can get results using the following simple process:

    1. Find the part of equal judgment (= = or! =) and overwrite it with its final value (True or False).
    2. Find the and/or in parentheses and calculate their values first.
    3. Find each not and figure out their inverse value.
    4. Find the rest of the and/or and solve their values.
    5. When you are done, the rest of the results should be True or False.

Let's demonstrate this in #20 logical expressions:

 and  not ("testing""test"or"Python " " Python ")

Next you will see how this complex expression is progressively solved as a single result:

  1. Solve each of the equivalence judgments:
    1. 3 != 4   for  true :  true and not  ("testing"  !=  "test"  or  ==  "python")
    2. "Testing"! = "Test" is true: True and not (True or "Python " = = " Python ")
    3. "python" = = "python": True and not (True or true)
  2. Find each of the and/or in parentheses:
    1. (True or true) is true: true and not (True)
  3. Find each of the not and reverse it:
    1. not (true) is false: true and false
  4. Find the rest of the and/or and solve their values:
    1. True and false to False

So that we can solve it, the final value is False.

Warning

Complex logical expressions may seem hard to you at first. And you may have hit the wall, but don't lose heart, these "logical gymnastics" training just lets you get used to it, so you can easily deal with something cooler in the programming behind. As long as you persist, do not let go of the wrong place. It doesn't matter if you don't understand for the time being, it always comes when you understand.

The results you should see

The following is the result of a conversation with python after you have guessed the result yourself:

$ pythonpython 2.5.1 (r251:54863, Feb  6, 19:02:12)[GCC 4.0.1 (Apple Inc. build 5465)] on DarwinType " Help "," copyright "," credits "or" license "for more information. True    true2True

Bonus points Exercise
    1. Python also has many operators similar to! = , = = . Try to list as many of the equivalent operators in Python as possible. For example, < or <= is.
    2. Write out the name of each equivalent operator. For example! = is called "Not equal (not equal to)".
    3. Test the new Boolean operation in python . You need to shout out the result before hitting enter. Do not think, by their own first sense can be. Write down the expression and the result with a pen and then hit enter, and finally see how much you do right, how much wrong.
    4. Throw away the paper on Exercise 3 and you won't have to query it anymore.

Stupid way to learn Python (28)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.