This is the 6th article on Python, focusing on logical judgments and operators.
(i)
Logical judgment:
Logical judgments are essential if a complex functional program is to be implemented. The most basic criterion for logical judgment: the Boolean type.
Boolean types have only two values: True and False, which correspond to 1 and 0 in the Python language.
Enter the following code in Pycharm and run it to get True or False feedback.
print(1 > 2) print('m' in 'member') print(7 >= 7) print(3 != 3)
Here, we will be able to return a boolean value of the expression is called a boolean expression. Boolean expression can be expressed in a variety of ways, the main introduction to the following kinds.
(ii)
1. Comparison operators:
Returns True if the comparison holds; False if it does not.
Common comparison operators are listed below:
In addition to simple two-value comparisons, comparison operators support more complex comparisons.
(1) Multi-conditional comparisons:
You can assign a value to variable a and then do a multiconditional comparison.
a = 3 print(1 < a < 5)
(2) Comparison of variables:
Assign separate values to variable a and variable b for comparison.
a = 3 b = 3 print(a != b) s1 = 'duwangdan' s2 = 'DuWangDan' print(s1 == s2)
(3) Comparison of function results:
print(abs(-1) > len('duwagndan')) # abs():Returns the absolute value of the input parameter
There are a few little things to look out for in comparative arithmetic:
Objects of different types cannot be compared with ">, >=, <, <=", but can be compared with "==, ! ==" to make comparisons.
print(21 == len('duwangdan'))
At the beginning of the article, it was said that True corresponds to 1 and False corresponds to 0. Look at the following example, False+True is actually equivalent to 0+1:
print(False + True > False + False)
2. Membership operators:
The keyword of the membership operator is "in", which determines whether an element is in a certain list. Run the following program to get feedback.
a = 1 b = 'beautiful' album = [1,'beautifully',False,7] # Create a list, name the list album print(a in album) print(b in album
When album=[], it means the list is empty.
If you want to add new content to the album, you can use the append method to do so. After adding, the added content will be displayed at the end of the list.
album = [1,'beautifully',False,7] ('Wow') # Add new content to the album with the append() method print(album)
3. Identity operators:
The identity operator is used to compare two objects to see if they are the same object, whereas the "==" operator is used to compare two objects to see if their values are equal.
Identity operators are mainly determined by "is, is not".
a = 'duwangdan' b = 'duwangdan' print(a is b) print(a is not b)
4. Boolean operators:
There are 3 boolean operators in Python: and, or, and not.
The following examples return the following results: False, True, and True.
print(1 > 2 and 1 > 0) print(1 > 2 or 1 > 0) print(not False)
That's it for the main part of this post; conditional control will follow.
Operating environment: Python version, 3.6; PyCharm version, 2016.2; Computer: Mac
Above this Python Beginner_A Brief Introduction to Logical Judgments and Operators is all I have to share with you, I hope it will give you a reference, and I hope you will support me more.