All sorts of calculations can be done using Python, by simply typing the equation and running your code. You may need to import specific modules to solve certain types of equation. For this session, we would take a look at basic calculations, that do not require specific modules. I would use “Python Shell” to run my commands.


Do you recall the “print statement” from the previous session? If so, this is the exact same thing, but more of calculations this time. We would be giving Python lots of print statements, and Python would calculate and give us the result. Later on in some other blog posts, we would delve into more advance calculations, which would involve importing specific Python modules.

Common Operators Used in Python

Let’s take a look at some operators that are used for calculations within Python, with some examples.

+ It is called “Plus”, and it is used for adding or concatenation.

 

It is called “Subtraction”, and is used for subtracting.

 

* It is called “Star”, and it is used for multiplication

 

/ It is called “Division”, and it is used for dividing

 

< It is called “Less than”, and it is used to indicate a lesser value

 

> It is called “Greater than”, and it is used to indicate a greater value

 

== It is called “Equal to”, and It is used for comparison

 

!= It is called “Not equal to”, and It is used for comparison

 

= It is called “Assignment operator”, and it is used to assign values


Data Types

In Python there are various “Data Types”, which tells the program what to do with your input.

STRING

“” is called a “String” it is used for printing sentences, any thing (letters or numbers) wrapped up in quotations cannot be calculated, the output would be a “print statement”, string is mainly used for letters and numbers that we don’t intend on calculating. For instance;

As seen in this image, Python printed the sentence that is wrapped up in the quotes.


Noticed in this example, even though we gave Python numbers it didn’t perform a calculation, rather it printed the same sentence that it was given but without the quotes.


Same as with the second example, Python didn’t perform a calculation on this as well, because it is wrapped up in quotes

Now that we know what String does, lets take a look at another Data Type.

INT

Int stands for Integer 1234…. Integers are meant to be calculated unless otherwise specified as a string. For instance;

As you’ve noticed, we told python to print 1+1 and it gave us the output of 2. Unlike the String examples with numbers, Python didn’t do a calculation of the those numbers because they were wrapped up in quotes, but here it calculated and gave us the answer, because our numbers are not guarded by quotes.


Same goes with this example, the numbers were calculated and the result displayed below our initial input.


Same as the aforementioned.

Let’s take a look at another Data Type

FLOAT

Float represents decimals 1.5, 6.8, 2.3…..

As seen in the example, float is simply decimal, so we told Python to calculated two decimal numbers, and it returned the output after calculating</>


Same as the example above, but this time we are performing a multiplication, the Star represents multiplication in Python.


Another example, but this time we are subtracting a float from an int

Let’s take a look at the last Data Type.

BOOLEAN

Boolean represents True or False. We could give Python an equation, and Python would tell us if the equation is True or False. For instance;

In this instance we as Python to calculate and tell us if one is less than 2, and the answer is true so it gave us an output of True.


In this instance we asked Python to tell us if 8 is greater than 4, and it also gave an output of True


As listed above, the == is used for comparison. We asked Python to calculate and if ten is the same as nine, and Python gave an output of False, because 10 and 9 is not the same.

Next we would take a look at Variables

Data Mrs Python, Tutorials

Leave a Reply

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