Login
📚 Python for Introductory Statistics
Chapters ▾

1.2 Python Arithmetic Operators

NameOperatorExampleResult
Addition + 5+6 11
Subtraction - 5-6 -1
Multiplication * 5*6 30
Division / 5/6 0.83333
Exponentiation ** 5**2 52 =25

To Write Your Third Python Code

7 × 95

  1. Click + Code on a notebook to add a code cell
  2. Type 7*95
  3. Press the play button to execute the code
  4. 665 is the result
Colab screenshot: three completed cells — print("Hello, world!") with its output, 10+10 with output 20, and 7*95 with output 665.

To Save Your Colab Notebook

Watch demo video

  1. Click Untitled.ipynb at the top of the notebook
  2. Delete the title and replace it by typing python1
  3. Press the enter key
  4. The file python1 is now saved in your Google Drive
  5. Click File and then Save
  6. Close your browser
Colab screenshot: the notebook renamed python1 (highlighted next to the Drive icon) with the File menu open and Save (Ctrl+S) highlighted.

To Open Your python1 Notebook

  1. Click this link http://colab.research.google.com#create=true or go to your Google Drive.
  2. Click python1 from the list on the pop-up window
  3. The notebook will open

OR click the File menu then Open Notebook

Colab screenshot: the saved python1 notebook reopened, showing its three cells and their outputs — Hello, World!, 20 and 665.

Note: For the examples below:

  1. Add a new code cell on a Colab notebook
  2. Type the code based on the statement of the example
  3. Execute the code

Example: Multiply 5×6

5*6
Show expected output
30

Example: Divide 10÷3

10/3
Show expected output
3.3333333333333335

Example: Exponentiate 52

5**2
Show expected output
25

Example: Add and then divide

( 2 + 3 + 5 + 4 ) ÷ 4

(2+3+5+4)/4
Show expected output
3.5

Note: If you want to add the numbers in the numerator 2+3+5+4 first and then divide it by the denominator, 4, you will need to save the sum of the numerator first. We will cover how to save the sum in a variable name in the next section.

Example: Add 12+13+25+10

1/2+1/3+2/5+10
Show expected output
11.233333333333334

Note: Adding parentheses to the fractions makes it clearer (see below)

(1/2)+(1/3)+(2/5)+10
Show expected output
11.233333333333334

Example: Perform the operations 6÷3×76.7+3.5

6/3*7-6.7+3.5
Show expected output
10.8

Example: Perform the operations 43+(85)3÷(3452)

43+(8-5)**3/(34-5**2)
Show expected output
46.0

Example: Perform the operation 1312+2.4×1.42

13+1/2+2.4*1.4**2
Show expected output
18.204

Note: Radicals in Rational Exponents Watch demo video

If x is a real number, a is integer and b is positive integer then

x a b = x a b

For example:

8 = 8 1 / 2

8 3 = 8 1 / 3

8 3 4 = 8 3 / 4

Example: Evaluate 7=71/2

7**(1/2)
Show expected output
2.6457513110645907
7**.5
Show expected output
2.6457513110645907

Note: 1/2 =.5

Example: Evaluate 73

7 3 = 7 1 / 3

7**(1/3)
Show expected output
1.912931182772389

Example: Find 524

Note: 524=(524)1/2

(5**2-4)**(1/2)
Show expected output
4.58257569495584

Example: Follow standard math order of operations to evaluate:

5 2 4 × 2 × ( 3 )

Note: 524×2×(3)=(524×2×(3))1/2

(5**2-4*2*(-3))**(1/2)
Show expected output
7.0

Example: Follow standard math order of operations to evaluate:

2 + 5 2 4 × 2 × ( 3 )

Caution: Everything under the square root is inside a parenthesis raised to 1/2. That is 524×2×(3) is raided to 1/2. You can do this in two steps using variables in the next section

-2+(5**2-4*2*(-3))**(1/2)
Show expected output
5.0

Example: Evaluate:

2 + 5 2 4 × 2 × ( 3 ) 2 × 2

Notice how paired parentheses are used to enforce order of operations.

(-2+(5**2-4*2*(-3))**(1/2))/(2*2)
Show expected output
1.25

Scientific Notations

Example: Divide 1186000

1/186000
Show expected output
5.376344086021505e-06

Note: The Python output 5.376344086021505e-06 is in scientific notation. 5.376344086021505e06=5.376344086021505×106

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.