Comparison operators are used for comparing values. Python provides operators for less than, greater than, less than or equal, greater than or equal, equal and not equal.
- [Instructor] These are Python's comparison operators, and there's a complete set of them: less than, greater than, less than or equal, greater than or equal, equal, and not equal. They compare the value of two operands. Let's take a look at how this works. Here in Komodo I've opened a working copy of comparison.py from chapter five of the exercise files, and you notice that it's pretty simple. I have two variables, and I have an if else. Then there's a comparison operator in the if right there.
So in this case it's using the less than comparison operator. So the condition will be true if the left-hand operand is less than the right-hand operand. So in this case, it is. X is less than Y, 42 is less than 73. It should print comparison is true. And when I save and run this you see it says comparison is true. Now if I change this operator to a greater than operator and save and run, you see that the comparison is now false.
There's also less than or equal to, so if I use less than or equal to and save and run, it is true. That's also true if I change the Y to a 42. I still get a comparison is true. The same is true of greater than or equal to. If I save and run this, you see that is also true. Whereas if I make this 73 again, it is now false. There's also the double equal comparison operator.
This checks if the values are the same, and so when I save and run this, it says false. But if make this a 42 and save and run, now the comparison is true. And there's the not equal operator. That's with an exclamation point and an equal sign. If I save and run this you notice that is false, but if I change 42 back to 73, and save and run, now the comparison is true. So these are the comparison operators in Python.
They're pretty straightforward, and we'll cover more operators in the rest of this chapter.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.