On how to use operators in Python
The world of coding takes a lot of its cues from the world of mathematics, so it’s no surprise that there’s a lot of stuff about “operators” in Python. Generally speaking, an operator is a symbol or function that tells the computer to perform certain logical or mathematical operations, such as addition, subtraction, multiplication, and division. You know what I mean: symbols such as + and – and * and /.
Even in the meager coding examples I’ve shown you so far, I’ve had to use an operator, the =, and explain how it is different from ==. But there are plenty of other operators to be aware of. Python can do all the stuff a basic calculator can do. You just have to know how to wield your operators. Here are some examples of the arithmetic operators at work.
So, you can see that if you enter 5 + 2, for example, the program tells you that equals 7. Five minus 2 is 3. Five multiplied by 2…well, you get the idea.
But there’s some weird stuff here, as well. For example, why in the heck does 5//2 give you 2? Well, it’s because it’s dividing 5 by 2 and then rounding to the nearest whole number (aka, floor division). Another weird one is 5 % 2. Why does it give you 1? Because it is giving you the remainder of 5 divided by 2. That may seem strange at first but you’ll find these operators can be quite useful for writing various programs.
If I really wanted to bore you you silly, I could systematically go through the whole list of operators, which come in a variety of flavors, including (in addition to arithmetic operators) comparison operators, assignment operators, logical operators, identity operators, and even the (sometimes mind boggling) bitwise operators.
Rather than spend pages doing that, I recommend that you go to any of the great online resources that give you the full treatment, such as the following:
- Tutorialspoint (which tells you how these work in direct English)
- w3resource (which is great because it also tells you about the “shorthand” for assignment operators, which you’ll be running into a lot in other people’s code)
- Python Programming wikibook (which is fairly self explanatory)
- Python.org (which explains things in a much less intuitive way but does tell you about related functions, which we’ll get to later)
Operators are as boring to read about as your average Congressional record, but they’re important to know. If you have a tough time remembering them, then I suggest using online resources such as Quizlet or Anki to create flashcards for yourself. In some cases, people will already have made flashcards of them (see the Quizlet link), so you may only need to find theirs through a search. My motto is to use what other people have already made as often as you can. That is, when in doubt, take the lazy way out.
Featured image: Author: Joseph A. Carr, Jersey Telecom telephone operator at switchboard, 1975, source: http://www.JoeTourist.net/, Wikimedia Commons