Learn Python For Beginners

Python Operators

This article is a continuation of a series of articles made for learning python for beginners. If you haven’t completed part 1, do that now. Python, like all other programming languages, has the capability of calculating operations on data using python operators. This will make up a large portion of your time when writing software so it’s important to understand how it works. Let’s start with addition and subtraction.

Python Operators

Let’s take what we learned in our last lesson and make it a little more interesting. Take the ‘Hello World!’ value out of the enclosed parentheses from the print statement including the quotation marks. Instead, enter a simple addition problem with any two numbers just like you would on paper. I chose 16 and 12 so my new print statement would look like this:

Learning Python For Beginners
Learning Python For Beginners – Operations

Again, click the run button and read the output in the integrated terminal to ensure everything works correctly. Mine looks like this:

Learning Python For Beginners – Operations

As you can see our operation ran just fine. Notice that we had to remove the quotation marks. That is because python interprets different values based on the data type. A data type is a fundamental idea in software development which we will discuss later, but know that it is a way to notify python how to deal with the data. The first value we had in the print statement was a string data type, which you can think of as a word. In a mathematical operation, we don’t want to use this data type because python would just give us a string back instead of the data type we really want which is an integer. Don’t worry if none of this is making sense let me try to demonstrate what I mean. Python knows that you want to do string operations if you enclose the value in quotation marks. Let’s see what happens if we add those back in. Go to your print statement and enclose your mathematic operation in quotes again like this:

Learning Python For Beginners – Operations

Now, if we click run again, we might get something different then what you’d expect. Something like this:

Learning Python For Beginners – Operations

As you can see what’s happening when we put a value in quotes is that we are telling python to treat the value as a word which it does differently than a number. This doesn’t mean that you can’t do math with words! Let’s try something even crazier.

String Operations

Let’s go back to our print statement and this time lets only wrap the individual numbers with quotation marks, like this:

Python Operators
Learning Python For Beginners – String Operations

After clicking run you should see something like this in your terminal

Python Operators
Learning Python For Beginners – String Operations

What happened? We simply told python to add two words or better yet strings together and it knew exactly how! This is called concatenation and is an extremely helpful tool when dealing with strings. This is interesting, but let’s see what else we can do with python operators.

More Math

Python can of course do multiplication, division, and many other mathematical operations as well. And just like in real life, you can give python an order of operations. Let’s go back yet again to our print statement and change things around some more. Try something like this:

Learning Python For Beginners – Operations

Try to predict the outcome before you run the program. Take into account the order of operations. You should get something like this:

Python Operators
Learning Python For Beginners – Operators

We do end up getting the desired output. In this case it is 78 because 13 x 6 is 78. But where did that decimal place come from? When you use division in python you end up with a float data type instead of an int data type like with simple addition. Don’t worry, we will be discussion floats more in the future. Regardless, you can see how you can do math with python and how easy it is to adapt from the math you already know. There are many other types of operations that are useful but won’t be covered here, so here is a list of all available operators

Learning Python For Beginners – Operations

You can find more information on python operators in the python documentation. Now that you have a good understanding of how math works with python, we can move on to more exciting things like python variables.

Leave a Comment

Your email address will not be published. Required fields are marked *