Python learning entry to master: bool data type

Python learning entry to master: bool data type

1. Concept explanation

The bool data type has only two values, namely True and False.

In the real world, true, correct, right, and positive. This group of words expresses a kind of affirmative meaning, and the corresponding ones are false, wrong, wrong, and negative. In the python language, True is equivalent to true, correct, correct, positive, and False is equivalent to false, wrong, wrong, negative.

Follow my code in the interactive interpreter

>>> 4> 3
>>> 4> 6
>>> type(True)

4 is greater than 3, which is obvious, so 4> 3 is correct, and the result is True, 4> 6 ​​is obviously wrong, so the result is False.

You can see through the type function that the types of True and False are bool.

2. Type conversion

So far, you have learned the three data types of int, float, and bool. They can be converted to each other. Follow me to operate in the interactive interpreter.

>>> bool(1)
>>> bool(0)
>>> int(True)
>>> int(False)
>>> float(True)
>>> float(False)

Here is a little trick that you need to remember. When using the built-in function bool to do data type conversion, the results of bool(0) and bool(0.0) are False, and all of them are True. Obviously, 0 is considered and false. Wrong things with the same concept.

3. Practice after class

Please directly output the execution result of the following code

1. 0 == 0
2. 4> 4
3. (3 + 4)> 5

Remember your answer and verify it in the interpreter

>>> 0 == 0
>>> 4> 4
>>> (3 + 4)> 5

Through this exercise, you have learned two new knowledge points

  1. == Determine whether two data are equal
  2. The content in parentheses is treated as a whole
  3. expression

Two equal signs (==) are used to judge whether two data are equal. If they are equal, the result is True, otherwise, it is False. This operator will be explained in the comparison operator tutorial. Today is just a preliminary understanding.

In number calculations, the role of parentheses is the same as what you learned in elementary school, indicating priority calculations, and the content in parentheses will be treated as a whole.

The three examples in the exercises are all expressions. Regarding expressions, there will be a special article to explain them later. Today is just a preliminary understanding.

I will briefly explain some concepts in advance, so that when you formally study, you already have a certain perceptual understanding. You have to adapt to the rhythm of this learning, repeated contact, repeated training, and improve your proficiency. This will help. To speed up your learning progress.

Reference: Python learning entry to proficient: bool data type-Cloud + Community-Tencent Cloud