Arithmetic operators include the standard four functions, integer division, modulo remainder, and exponents.
- [Instructor] Python has a full set of arithmetic operators and they're listed here. Addition, subtraction, multiplication, division integer division, remainder, exponent and the unary negative and positive operators. Here in Komodo, I've opened a working copy of arithmetic.py from chapter five of the exercise files. And you see we have a couple of variables and then we perform arithmetic on them and display the result.
So, first we have the addition operator and when I save and run this, you see the result is 5.3 equals eight. And we have the subtraction operator. So, five minus three, when I save and run is two. And the multiplication operator. My editor does weird things when I do that. I save and run and the result is 15. And the division operator.
When I save and run that, you notice that the result is no longer an integer even though the two operands are integers. When I divide, I will always get a floating point. This is new behavior in Python 3. Python 2 does not work this way so it's worth taking note of. If I want to get the integer division then I use two slash marks for the operator and when I save and run, I get a result of one which is five divided by three without the remainder.
If I want the remainder, I use the percent sign which will give me the remainder or the modulus. And when I save and run you notice I get that remainder of two. There is also unary operators for negating and leaving an operand positive. And so, if I say Z equals minus Z then I get a result here of minus two. And if I say Z equals plus Z then I get the two by itself.
And even if the number was already a negative I say Z equals plus Z, it simply doesn't negate it. So, that's just there for decoration. It doesn't really do anything to the operand. So, these are the arithmetic operators in Python. 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
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.