Skip to main content

Section 1.1 Basic Operations with Python

In this section, we will be learning about things like adding and subtracting in python. You will see "variables" or things that can take on any value. In python, you set the value of a variable with an equals sign. You can change the value by resetting it. This will make more sense once you run the code examples below.

Subsection 1.1.1 Addition, Subtraction, Multiplication, and Division

Python will do basic math operations like addition, subtraction, multiplication and division. In the following example we create a variable x, set its value to be 1, and then change its value using various operations. Comments in code start with a "#" and are ignored when the code is run. This allows us to let others know what we are doing.
Run the following code. The print command will tell you the value of x after all the operations. Try to figure out what x will be before running the code!
Notice that we did not divide. There are two ways to divide in python. For now, we will concern ourselves with standard division. But if we take an integer and divide, a "float" or decimal number will be returned. This is not important now, but will be later.

Subsection 1.1.2 Powers and Roots

You may be used to typing an exponent as something like 4^2 = 16. In python, we write this as 4**2.
Recall from mathematics that roots can be represented by powers.
\begin{equation*} x^{\dfrac{1}{n}} = \sqrt[n]{x} \end{equation*}
One way to find roots then would be to take to fractional exponents. This is not the only way though. Another way is to use a library, which is something we explore later. For now, if we need to find a root, we will use an exponent as below.
Once again, a float is returned. For numbers that are not perfect squares, we might not want all the decimal places python will show us. We can use the round() function to get the number of decimal places we would like to see. To do this, we have to actually change x. That is why we have "x =" before the round() function.
In this section, you learned about operations on numbers, but you also learned about working with variables, floats (decimals) vs integers, and the round() function. The exercises after each section will help you practice the material presented. If you have not installed python on your machine, you can write this code from any computer connected to the internet using google colab: https://colab.research.google.com/.

Exercises 1.1.3 Exercises

1.

Set a variable called "number" equal to 5. Add 10 to "number", multiply "number" by 5, and subtract 7. Finally, print number. What is the value of number?
Solution.
Remember at each stage, you must reassign the value to number. This is done with the equals sign. Finally, print() will print the variable you pass to it. NOtice that the operations do not have to be on one line. Python will follow order of operations, so you can multiply by 5 and subtract 7 on the same line.
number = 5
number = number + 10
number = 5*number - 7
print(number)

2.

Find the value of:
\begin{equation*} 3(1+4)^{6}-15 \end{equation*}
Solution.
Since python follows order of operations, you can set a variable equal to the expression and print. There needs to be a * in front of the parentheses to represent the multiplication.
x = 3*(1+4)**6 - 15
print(x)

3.

Find one solution to
\begin{equation*} 2x^2-x+5=0 \end{equation*}
and round to two decimal places.
Hint 1.
The quadratic formula will be helpful.
Hint 2.
The quadratic formula is: \(x = \dfrac{-b \pm \sqrt{b^{2}-4ac}}{2a}\) where \(a\) is the coefficient of \(x^2\text{,}\) \(b\) is the coefficient of \(x\text{,}\) and \(c\) is the constant. You can just use "+" from "\(\pm\)" to get one root.
Solution.
Using the quadratic formula, we can find one root. While you could type it all in one line, it is easier to break up into two parts. For the negative in the formula, you must multiply by \(-1\text{.}\)
x = -1*(-1) + ((-1)**2-4*2*5) #top of fraction
x = x/(2*2)
x = round(x,2)
print(x)