See our full tutorial at http://learnpythontutorial.com/boolea...
Boolean Operators in Python
Couple Python tutorials ago we looked a booleans in this tutorial we are going to take a closer look at Boolean operators in Python. Boolean operators are typically used in conjunction with comparison operators which we looked at in previous tutorials. There are three boolean operators which evaluate if a statement is true or false. The three Boolean operators are "and", "or" and "not". Take a look at some key points that we must know before we jump into examples of Boolean operators.
Key points
All objects have a boolean value either True or False.
Empty strings, tuples, list and dictionaries return False.
Non-empty strings, tuples, list and dictionaries return True
Number zero returns False.
Any number other than zero returns True.
Boolean operators stop running as soon as they know the answer which is sometimes referred to as short circuit.
You may want to come back after completing this tutorial to better understand these bulletpoints.
and Boolean Operator
The "and" Boolean will return True if the statement on left and right are True. If one of the statements is False either right or left then Python will return False. Let's look at a couple examples of the "and" Boolean operator.
Examples of and Boolean Operator
True and True
True
True and False
False
False and True
False
#What happens when we compare numbers? We do not get the simple True or False return rather we get the number on right if both are true or the number on left if number on left if it's zero. Now we probably will not use this in practice but just proves that each object has a True or False value.
4 and 7
7
0 and 6
0
#Same as above with a string. If both are true we get the object on the right and if one on the left False Python will return the left.
"tv" and "xbox"
'xbox'
"" and "xbox"
''
or Boolean Operator
The "or" Boolean Operator returns True if one of statements is True either right or left. Let's take a look at some examples.
Example Of "or" Boolean Operator
True or True
True
True or False
True
False or True
True
False or False
False
not Boolean Operator
The "not" Boolean operator is like opposite day from when you were a kid. When using not if the statement in question is True and Python will return False and if the statement in question is False then Python will return True.
Examples Of "not" Boolean Operator
Do not overthink Boolean operators they are fairly simple. If you have any questions about Boolean operators leave a comment below and we will help you out.
En esta página del sitio puede ver el video en línea Python Tutorial: Boolean Operators In Python #39 de Duración hora minuto segunda en buena calidad , que subió el usuario Code master 15 septiembre 2015, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 22,945 veces y le gustó 207 a los espectadores. Disfruta viendo!