Product Manager learn Python: logical judgment and operators, product manager python
This is the 6th article on Python, which mainly introduces logical judgment and operators.
To implement a complex functional program, logical judgment is essential. The most basic criterion for logical judgment: boolean type.
The boolean type has only two values: True and False, which correspond to 1 and 0 in Python.
Enter the following code in Pycharm. After running the command, the feedback is True or False.
1 print(1 > 2)2 print('m' in 'member')3 print(7 >= 7)4 print(3 != 3)
Here, the expression that can return a Boolean value is called a Boolean expression. There are many boolean expressions, which are described below.
1. Comparison operators:
If the comparison is True, True is returned. If the comparison is not True, False is returned.
Common comparison operators are as follows:
In addition to comparing simple two values, comparison operators also support relatively complex comparisons.
(1) Multi-condition comparison:
You can assign a value to variable a and then compare multiple conditions.
1 a = 32 print(1 < a < 5)
(2) Comparison of variables:
Assign values to variables a and B for comparison.
1 a = 32 b = 33 print(a != b)4 5 s1 = 'duwangdan'6 s2 = 'DuWangDan'7 print(s1 == s2)
(3) Comparison of function results:
1 print (abs (-1)> len ('duwagndanc') 2 # abs (): returns the absolute value of the input parameter
There are some minor issues to be aware of in comparison operations:
Different types of objects cannot be compared using ">, >=, <, <=", but can use "= ,! =.
1 print(21 == len('duwangdan'))
As stated at the beginning of the article, True corresponds to 1, and False corresponds to 0. In the following example, False + True is actually equivalent to 0 + 1:
1 print(False + True > False + False)
2. member operators:
The keyword of the member operator is "in" to determine whether an element is in a list. After running the following program, you can get feedback.
1 a = 12 B = 'betiful '3 album = [1, 'beauulully', False, 7] 4 # create a list named album5 print (a in album) 6 print (B in album
When album = , the list is empty.
If you want to add content to album, you can use the append method. The added content is displayed at the end of the list.
1 album = [1, 'betifully ', False, 7] 2 album. append ('wow') 3 # add content 4 print (album) to album using append () method)
3. Identity operators:
The identity operator is used to compare whether two objects are the same object, while "=" in the previous comparison operator is used to compare whether the values of the two objects are equal.
Identity operators are mainly determined by "is, is not.
1 a = 'duwangdan'2 b = 'duwangdan'3 print(a is b)4 print(a is not b)
4. boolean operator:
There are three boolean operators in Python: and, or, and not.
In the following example, the returned results are: False, True, and True.
1 print(1 > 2 and 1 > 0)2 print(1 > 2 or 1 > 0)3 print(not False)
The main content of this article is here, and we will introduce conditional control in the future.
Operating Environment: Python version, 3.6; PyCharm version, 2016.2; Computer: Mac
----- End -----
Author: du wangdan, Public Account: du wangdan, Internet product manager.