Hướng dẫn dùng not operator python
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Using the Python not Operator Show
Python’s Using the In this tutorial, you’ll learn:
You’ll also code a few practical examples that will allow you to better understand some of the primary use cases of the Working With Boolean Logic in PythonGeorge Boole put together what is now known as Boolean algebra, which relies on true and false values. It also defines a set of Boolean operations:
In Python, the Boolean type, >>>
This type has two possible values, >>>
Python internally implements its Boolean values as Python provides three Boolean or logical operators:
With these operators, you can build expressions by connecting Boolean expressions with each other, objects with each other, and even Boolean expressions with objects. Python uses English words for the Boolean operators. These words are keywords of the language, so you can’t use them as identifiers without causing a syntax error. In this tutorial, you’ll learn about Python’s Getting Started With Python’s not OperatorThe If you apply >>>
The
With
In this tutorial, you’ll find examples that cover all these use cases. To kick things off, you’ll start by learning how the A Boolean expression always returns a Boolean value. In Python, this kind of expression returns
>>>
The expression You can also use >>>
In each example, This built-in function internally uses the following rules to figure out the truth value of its input:
Once Now that you know how Using the not Operator in Boolean ContextsLike the other two logical operators, the
These two structures are part of what you’d
call control flow statements. They help you decide a program’s execution path. In the case of the if StatementsYou can use the The syntax for an
In this example, If A common situation is one where you use a predicate or
Boolean-valued function as a >>>
In this example, You can also use this function in a negative conditional statement to approach those situations where you want to work with composite numbers only: >>>
Since it’s also possible that you need to work with composite numbers only, you can reuse Another common situation in programming is to find out if a number is inside a specific numeric interval. To determine if a number >>>
In the first example, you use the You may also face the need to check if a number is outside of the target interval. To this end, you can use the >>>
This >>>
In this example, you reuse the expression you originally coded to determine if a number is inside a target interval. With while LoopsThe second Boolean context in which you can use the Say you want to code a small Python game to guess a random number between 1 and
10. As a first step, you decide to use Fire up your code editor or IDE and
create a new
In The Now you can continue with your game by writing the code to provide the guessing functionality. You can do it by yourself, or you can expand the box below to check out a possible implementation. The second part of the game should allow the user to enter a number from 1 to 10 as their guess. The game should compare the user’s input with the current secret number and take actions accordingly. Here’s a possible implementation:
You use an infinite As an exercise, you can restrict the number of attempts before the user loses the game. Three attempts could be a nice option in this case. How was your experience with this little game? To learn more about game programming in Python, check out PyGame: A Primer on Game Programming in Python. Now that you know how to use Using the not Operator in Non-Boolean ContextsSince the Suppose you need to perform two different actions alternatively in a loop. In that case, you can use a flag variable to toggle actions in every iteration: >>>
Every time this loop runs, you check the truth value of You can use the >>>
Now the highlighted line alternates the value of Using the Function-Based not OperatorUnlike the >>>
To use Using the >>>
In this example, you have an initial list called Working With Python’s not Operator: Best PracticesWhen you’re working with the You’ll also learn how negative logic can impact the readability of your code. Finally, you’ll learn about some handy techniques that can help you avoid unnecessary negative logic, which is a programming best practice. Test for MembershipMembership tests are commonly useful when you’re determining if a particular object exists in a given container data type, such as a list, tuple, set, or dictionary. To perform this kind of test in Python, you can use the
>>>
The Sometimes you may need to check if an object is not in a given container. How can you do that? The answer to this
question is the There are two different syntaxes to check if an object is not in a given container in Python. The Python community considers the first syntax as bad practice because it’s difficult to read. The second syntax reads like plain English: >>>
The first example works. However, the leading The second example is much clearer. The Python documentation refers to the syntax in the second example as the Now it’s time to revisit the examples where
you checked if a number was inside or outside a numeric interval. If you’re working with integer numbers only, then the >>>
The first example checks if When you’re working with integer numbers, this small trick about where exactly you use the Check the Identity of ObjectsAnother common requirement when you’re coding in Python is to check for an object’s identity. You can determine an object’s identity using The practical way to check for identity is to use the >>>
The In this case, the question is: how do you check if two objects don’t have the same identity? Again, you can use two different syntaxes: >>>
In both examples, you check if Avoid Unnecessary Negative LogicThe Using negative logic correctly can be tricky because this logic is difficult to think about and understand, not to mention hard to explain. In general, negative logic implies a higher cognitive load than positive logic. So, whenever possible, you should use positive formulations. Here is an example of a >>>
This function takes a number as an argument and returns its absolute value. You can achieve the same result by using positive logic with a minimal change: >>>
That’s it! Your You can find many similar examples in which changing a comparison operator can remove unnecessary negative
logic. Say you want to check if a variable >>>
In this example, you remove the However, sometimes negative logic can save you time and make your code more concise. Suppose you need a conditional statement to initialize a given file when it doesn’t exist in the file system. In that case, you can use
The Now think of how to turn this negative conditional into a positive one. Up to this point, you don’t have any action to perform if the file exists, so you may think of using a
Even though this code works, it violates the “You aren’t gonna need it” (YAGNI) principle. It’s an especially determined attempt to remove negative logic. The idea behind this example is to show that sometimes using negative logic is the right way to go. So, you should consider your specific problem and select the appropriate solution. A good rule of thumb would be to avoid negative logic as much as possible without trying to avoid it at all costs. Finally, you should pay special attention to avoiding double negation. Say you have a constant called Now say you need to check if a given character is a numeric value. Since you already have
This code looks odd, and you probably won’t ever do something like this in your career as a programmer. However, doing something similar can sometimes be tempting, such as in the example above. This example uses double negation. It relies on ConclusionPython’s You can use the In this tutorial, you learned how to:
To these ends, you coded a few practical examples that helped you understand some of the main use cases of the Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Using the Python not Operator |