Learn Python The Hard Way learning (28)-Boolean operation exercises

Source: Internet
Author: User

The logical combination in the previous section is called a Boolean logical expression. Boolean logic is used in many places of a program. It is the foundation of computer computing. Learning about it is just like learning music to understand scales.

Read the following question, write the answer, and verify the answer in the python parser.
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 ")
Below I will give some methods to clarify complex logic.

When you see a logical judgment statement, you can solve it through the following process:
Find = or! =, Judge whether they are true or false.
Judge whether the brackets are true or false.
Locate not to determine whether it is true or false.
Finally, find and or to determine whether it is true or false.
After completing these steps, you can solve the problem.
Let's take a look at the above 20th questions:
3! = 4 and not ("testing "! = "Test" or "Python" = "Python ")
Now we install the above steps and then write the results of each step:
Search for = and! = Symbol, which becomes True and not (True or True) after judgment)
True and false in brackets: True and not True
Not: True and False
Finally: False
Warning: At the beginning, you will think this expression is very complicated. Don't be discouraged. These exercises are just a preliminary understanding. As long as we don't miss some mistakes, we will gradually become familiar with these operations.

Running result
Root @ he-desktop :~ # Python
Python 2.6.5 (r265: 79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> True and True
True
>>> False and True
False
>>> 1 = 1 and 2 = 1
False
>>> "Test" = "test"
True

Extra score exercise
1. There are many similar python! = And =, and try to find some equivalent operators, such as <or <=.
Operation Meaning Notes
<Strictly less
<= Less than or equal
> Strictly greater
> = Greater than or equal
= Equal
! = Not equal (1)
Is object identity
Is not negated object identity
2. Write the name of the equivalent operator.
The above table is available.

3. test the new Boolean operator in python.

4. Drop the paper in section 3 and we will no longer need it.

Author: lixiang0522
 

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.