Arithmetic in Python

1-20-2021

Arithmetic is one of the most basic concepts which a computer can perform. In Python, performing arithmetic is extremely simple, and quite similar to how you may normally write arithmetic. However, a program is able to perform even ludicrous arithmetic that would take a human hours in seconds!


Basic Operators

Addition


Addition can be performed in Python the same way we do it in writing, with a plus sign. Here's an example of that.

print(4+7)

11

By using the print statement, we can have the result of this arithmetic printed in the console. We can also save values in variables, which you can read about here. For example, we can write:

number=4
print(number+7)

11

We can see this works the same way, and can also work if both numbers are variables:

number1=4
number2=7
print(number1+number2)

11

Lets use the same ideas for the other operators

Subtraction


print(7-5)

2

number=7
print(number-5)

2

Division


Division will work as expected when dividing two integers by numbers that divide evenly.

print(8/4)

2

number=8
print(number/4)

2

However, if we divide two numbers that do not divide evenly, it will print out a float, or decimal number:

print(5/2)

2.5

Multiplication


print(7*5)

35

number=7
print(number*5)

35

Modulo or Remainder


One operator in Python that is not as common in written mathematics is the modulo operator. This is an operator thay returns the remainder when two numbers are divided. It is written with the percent symbol. For example:


print(13%4)

1

Here, the result is two because after 4 fits into 13 three times, there is 1 left over.


Storing Values

You can use arithmetic and variables in tandem in order to save the results of some arithmetic into a new variable. For example:


number1=5
number2=6
number3=number1*number2
print(number3)

30

Assignments


We can also directly perform arithmetic on a variable and alter it's value in one line of code with assignments. Essentially, we can tell Python that we want a variable equal to itself multiplied by, added with, subtracted from, or divided by some value. We can even use the reminader operator. This is done by writing the variable you are modifying, then the operator of your choice, followed by an equal sign, and then the value you are applying to the variable with your chosen operator. For example:


number=5
number*=5
print(number)

25

number=10
number+=5
print(number)

15

number=10
number/=5
print(number)

2

number=16
number%=5
print(number)

1

Using Floats

For these basic forms of arithmetic, floats, or decimals, can be used in place of integers and the operators will still work correctly. You can even use one float and one integer, which will usually return the result as a float.


Type Errors

One thing which programmers need to be careful of is making sure your values are the correct type. When performing arithmetic, you need to assure your values are either integers or floats. If they are written as strings, such as "5" or "3.8" with quotation marks, you will likely either produce a type error or accidentally perform string concatenation. If you need a run down on data types, you can check it out here. String concatenation will be discussed in the future.

Conclusion

Now that you have learned about arithmetic, I encourage you to try and write a small program using these ideas. You may be limited in what you create, but implementing what you learn in anyway will be great for the development of your skills.