As a Calculator

When you use the Python shell, your commands are executed as you write. It is hence possible to use the Python shell as a calculator.

Start IDLE to enter the Python shell.

The arithmetic operators in Python 3 are:

+ - * /
make a wild guess!
**
to the power of
//
the quotient when doing integer division
%
the remainder when doing integer division

Exercise 1

Calculate following expressions, only use brackets when you need it

  1. formula
  2. formula
  3. formula
  4. formula
  5. formula
  6. formula
  1. >>> 4.2/(7.3+0.26)
    0.5555555555555556
  2. >>> 4.2/7.3/0.26
    2.212855637513172
  3. >>> 7.3*0.26/4.2
    0.45190476190476186
  4. >>> 3*-5
    -15
  5. >>> 200**0.5
    14.142135623730951
  6. >>> 200**(5/7)
    44.01420420561975

Exercise 2

How many hours and minutes are there in 500 minutes?

>>> 500//60
8
>>> 500%60
20

Exercise 3

From the code

>>> 22//5
4
>>> 22%5
2
	

you can conclude that 22 = 5 · 4 + 2.

Make an educated guess of the result when typing -22//5 and -22%5 in Python. Check your answer in IDLE.

>>> -22//5
-5
>>> -22%5
3

Exercise 4

The operators * and / have higher precedence (they will be calculated first) than the operators + and -. What precedence does the operator ** have? Find it out using IDLE by considering the fact that

formula

 

>>> 2*9**1/2
9.0

** has higher precedence, it is calculated first.

further info:

In 5.15 Summary there is a table of the operator precedences in Python: http://docs.python.org/reference/expressions.html

by Malin Christersson under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Sweden License

1234
Join EFF!