See our full tutorial at
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.
Nguồn:https://truonggiabinh.com/
Xem thêm Bài Viết:
- Mách bạn cách cách xóa header nhanh chóng
- Review cách phân trang trong excel và bỏ phân trang
- Hướng dẫn các bước đảo ngược vùng chọn trong Photoshop
- Cuốn cẩm nang hướng dẫn tạo USB cài XP+Windows 7+Hiren’s Boot đầy đủ nhất
- Đánh Giá & Trải Nghiệm Máy Bơm Lốp Xiaomi 70mai MiDrive – Bơm BWM 320i Một Cách Dễ Dàng!
Thank you, your explanation helped me better to understand this.
Man this is a great video thanks man!!! This really helped me you rock!!!!!!
how to use boolean operator in a program ??
Thank you so much great video !!!
Question,
How do you deal with the statement
not not True or not not False
Quick and straight to the point. Great video!
True and True is True
True and False is False
False and True is False
False and False is False
True or True is True
True or False is True
False or True is True
False or False is False
Not True is False
Not False is True
what do you hit after last letter of line that the lower line gives you true or false. I hit enter and nothing is hapening?
Thank you! This was very clear.
Thank you for making this video, that helped me to understand booleans better.
Great video.