Operations
Python Basics • Last Updated: 01/12/2025
Python Basics • Last Updated: 01/12/2025
When coding in Python, there's a set of operations you will find yourself using a lot. These operations on practical use look just like the ones you would use in standard math -- however, there are some operations you cannot type normally, which are then done in other ways.
When coding in Python, syntax is important, but you will sometimes find yourself needing to manipulate some values stored in other forms of data, like variables (more on that here).
All operations in Python run using what's known as the order of operations. You should already know how this works, but in short, from left to right, any content in brackets first, then indexes or other things like roots second, then multiplication and division third, and finally, addition and subtraction last. This is the exact same order that Python does its calculations.
There are a handful set of basic operations you should be familiar with; however, some might be different from your standard set as multiplication is not '× ' but '*' (asterisk) instead. Below is how you would type them in.
... Those up there are just the basics. There are more -- and they are useful at times. Oh, and the above code snippet isn't actually valid -- you have to use it with something to actually make sure Python doesn't throw a traceback error.
Now, we move on past the basics. The next three operations are modulus, exponentials, and floor division.
Modulus is essentially just a fancy way of saying 'divide then return the remainder'. An example of that is having 10 % 3, which returns 1. Below is an example of how you can use modulus.
What the above code does is it sets a variable, var, to the value 10, then sets it to the remainder of itself if it was divided by 3, then prints the result to the output window, which should be 1.
The easiest way to describe exponentials are that to take the number before the operation and bring it to the power of the second number. That in code, represented by variables a and b, representing the first and second numbers respectively would look like this:
The code above just assigns the value 2 to the variable a, and 3 to b. Then the variable result takes a and raises it to the power of b. Then the value of result is printed to the output window.
Essentially, floor division is when you take any number and divide it by another number. Except with floor division, you now have to round the result down to the nearest whole number, as implied by the word 'floor'. In this example, we'll use a as the first number, and we'll divide it by b, and that value will be held in the variable result. Then that value will be printed to the output window.
Well no, that's not it with operations. Ever felt as if var = var + 1 was a waste of characters? Well, Python has a simple fix to that issue, and that is just simply this:
And well, no. It doesn't stop there. You can do this with any operation listed above this section. Subtraction, multiplication, division, exponentialities, modulus, and floor division work too. All you have to do is have the respective symbol directly to the left of an equals sign, as shown in the example above, and that's about it.
Python also has the ability to check for inequalities, as in 1 > 0 or 4 < 5. This can be easily applied exactly like the other operations, but with a catch -- they have to be used with 'if' statements, more on that here. Logically, you would not be setting a variable to 1 > 0, unless you want it to be locked to 'true', which will explained further here.
And what about symbols like ≥ or ≤? Well typing those repeatedly without a copy-paste area existent can and will be a problem, so the natural solution is to type <= or >= instead. These counterparts to those symbols are exactly what you type, and the order matters. You put the equals sign second on every occasion for dual operations or Python will throw a syntax error, which is not what you want. Read more on Python Syntax over here.
Python has a great variety of operations you can use to complete calculations for your projects. Some of them are really basic and easy, while others are dual symbols, which have extremely specific syntax to watch out for -- but it's not as bad as other coding languages.
If you have any questions, please head to the comments section below!
Share your opinions or ask questions.
-