Getting Started With Python: Numbers and Operators

In my article on learning the fundamentals of Windows PowerShell, I mentioned that I am in the progress of learning Python. When learning a new programming language, one of the first things you’ll do, besides writing Hello World, is learning about the operators, variables, datatypes and so on and so forth. So naturally, when I started with Python, I did the same!


Installing Python and choosing a text editor

Before I get into numbers and operators, I figured it might be a good idea to quickly go over how you can start using Python yourself.

First, visit the official Python website and download the latest 3.X.X version of Python. The installation is very straightforward, but there is one thing you must do when you’re going through the installation process. That is checking the “Add Python 3.X.X to PATH” checkbox. Adding Python to PATH allows PowerShell as well as IDE’s like PyCharm to access Python and identify that it is installed on your computer.

Python Installer and the Add to PATH option

After installation, you also need a text editor to write your code in. My editor of choice is Sublime Text, which is free to use and very powerful and versatile. I have used and recommended it for HTML5 and CSS as well.

To verify that Python was installed correctly, open PowerShell. In PowerShell, type Python and hit enter. If it gives you a message starting with “Python 3.X.X”, installation was successful. If not, something went wrong and you might have to try again.

After entering Python as a command, you also have instant access to the Python Interpreter, which means you can run Python code in PowerShell. Try using the print command and enter Hello World!

print("Hello World!")

Integers and Floats

There are a few datatypes in Python specifically for numbers. The most important ones are Integers and Floats. Ints are whole numbers (5, 205, 61087, -10). Floats are decimal point numbers (2.5, 6937.295 and even 1.0). If you want to know more about all the other datatypes, the best resource is Python’s official documentation.


Numbers and operators

Now that we’ve got all that stuff out of the way, let’s get into the operators. The operators in Python are very similar to those in other programming languages. You have a set of basic operators and some more advanced mathematical operators. Since we are just starting out, I will be covering only the basic operators in this post.

[eckosc_tab_container] [eckosc_tab_item title=”Addition (+)”]You add two or more numbers together. When using the Python Interpreter, you can enter any addition without additional syntax, and it will give you the answer. This goes for all the operators, by the way.[/eckosc_tab_item] [eckosc_tab_item title=”Subtraction (-)”]You can subtract any number of numbers from your initial value. Like addition and multiplication, it doesn’t really need an explanation.[/eckosc_tab_item] [eckosc_tab_item title=”Multiplication (*)”]Multiply any number of values. For example: 2 * 5 * 5 = 50.[/eckosc_tab_item] [eckosc_tab_item title=”Float Division (/)”]In Python, when using the standard division operator, the result will always be a float, even when the resulting number would normally be a whole number. For example, 4 / 2 would equal 2 normally, but equals 2.0 in Python.[/eckosc_tab_item] [eckosc_tab_item title=”Integer Division (//)”]If you ever want to divide two numbers, but want the result to be a whole number, you can use integer division. For example, normally 1 / 2 would result in 0.5. However, when using integer division and doing 1 // 2, the result will be 0.[/eckosc_tab_item] [eckosc_tab_item title=”Exponentiation (**)”]Exponentiation is multiplying a number an X amount of times to the power of another. For example 4 ** 2 is the same as 4 * 4 * 4, which equals 64.[/eckosc_tab_item] [eckosc_tab_item title=”Remainder (%)”]The remainder operator checks how many times one value fits into another value, and then gives you the remaining number as a result. For example, 20 % 3 = 2. That’s because 3 fits into 20 a total of 6 times, which equals 18 (3 * 6). The remaining 2 is the result.[/eckosc_tab_item] [/eckosc_tab_container]

Operator order of precedence

A last important thing to note is the order of precedence when combining multiple operators. When combining multiple operators, certain operators take precedence over another. If you don’t know this order, a calculation might yield a different result then you expected. I recommend you check this page that has a full table for the order of precedence, as well as some examples.


Wrapping up

So that was a quick overview of using numbers and operators within Python. Hopefully you’ll be able to try some calculations within PowerShell and even within some basic Python programs. Don’t forget to use the Python documentation if you need a refresher on the operators and datatypes!

Let me know if this was helpful or if you would like to see more of these types or articles in the future and as always, if you have questions or concerns, feel free to comment below and make sure to share the article if you liked it!

Using the Input Function To Get User Input in Python Control Your Code: Conditional Logic in Python HTML: Finishing the Basics and Practicing
View Comments
There are currently no comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.