Python as a Sophisticated Calculator
The resources on this page were originally created by Dr. Aaron Bradley of Summit Middle School. I've done some reformatting to add clarity but that is about it. Enjoy!
|
Lines that start with >>> or ... indicate what you should type into the interpreter on the bottom of the Portable Python window. Subsequent lines show the result after you press "Enter." Remember, nothing you type into the interpreter will save. If you want the information to save, you will have to create a new module.
Basic Calculations
1. >>> 42 * 179
7518
2. >>> (7 - 2) - (3 - 5)
7
3. >>> 2**7
128
What does the ** operator do?
4. >>> a = 5
>>> b = 7
>>> a*3 + b
22
What does a = 5 do? I read it as, "a is assigned value 5."
5. >>> a = 42 * 179
>>> b = (7 - 2) - (3 - 5)
>>> a + b
7525
6. >>> def f(x):
... return 5*x + 7
>>> f(5)
32
>>> f(1.3)
13.5
To define the function: f(x), type "def f(x):" , then "Enter," then type "return 5*x + 7", and then hit "Enter" again.
Notice that after your first "Enter" the interpreter automatically indented the line for you. Indentation is a BIG deal in Python; it's how the program knows the code belongs inside of the function.
7. >>> def g(x):
... return 2 * x**2 - 5 * x + 3
>>> g(5)
28
8. >>> f(5) + g(5)
60
9. >>> f(g(5))
147
10. >>> g(f(5))
1891
11. Define a function h(x): x^2 + 3x - 5 (you will need to reformat this into Python syntax)
12. Evaluate h(5)
13. Define a function k(x): 7x - 2
14. Evaulate h(3) * k(3)
15. Evaulate h(2.2) + k(2.2)
16. Evaluate h(k(0))
1. >>> 42 * 179
7518
2. >>> (7 - 2) - (3 - 5)
7
3. >>> 2**7
128
What does the ** operator do?
4. >>> a = 5
>>> b = 7
>>> a*3 + b
22
What does a = 5 do? I read it as, "a is assigned value 5."
5. >>> a = 42 * 179
>>> b = (7 - 2) - (3 - 5)
>>> a + b
7525
6. >>> def f(x):
... return 5*x + 7
>>> f(5)
32
>>> f(1.3)
13.5
To define the function: f(x), type "def f(x):" , then "Enter," then type "return 5*x + 7", and then hit "Enter" again.
Notice that after your first "Enter" the interpreter automatically indented the line for you. Indentation is a BIG deal in Python; it's how the program knows the code belongs inside of the function.
7. >>> def g(x):
... return 2 * x**2 - 5 * x + 3
>>> g(5)
28
8. >>> f(5) + g(5)
60
9. >>> f(g(5))
147
10. >>> g(f(5))
1891
11. Define a function h(x): x^2 + 3x - 5 (you will need to reformat this into Python syntax)
12. Evaluate h(5)
13. Define a function k(x): 7x - 2
14. Evaulate h(3) * k(3)
15. Evaulate h(2.2) + k(2.2)
16. Evaluate h(k(0))
Lists of Numbers
Depending on your time, you may want to do the following exercise in a module. However, in the module, pressing 'enter' does not get you an output; you have to tell the program to print the answer.
Number 1 below would look like this:
nums = [1, 2, 3]
print(nums)
Then you would need to run the program. The output would happen below in the interpreter.
Depending on your time, you may want to do the following exercise in a module. However, in the module, pressing 'enter' does not get you an output; you have to tell the program to print the answer.
Number 1 below would look like this:
nums = [1, 2, 3]
print(nums)
Then you would need to run the program. The output would happen below in the interpreter.
1. >>> nums = [1, 2, 3]
>>> nums
[1, 2, 3]
2. >>> nums[0]
1
3. >>> nums[1]
2
4. >>> nums[2]
3
5. >>> nums[3]
Traceback (most recent call last):
File "", line 1, in I
ndexError: list index out of range
Why?
6. >>> len(nums)
3
7. >>> [x+1 for x in nums]
[2, 3, 4]
8. >>> [n**2 for n in nums]
[1, 4, 9]
9. >>> [f(n) for n in nums]
[12, 17, 22]
10. >>> [f(2*n) for n in nums]
[17, 27, 37]
11. >>> [(2*n, f(2*n)) for n in nums]
[(2, 17), (4, 27), (6, 37)]
Why?
12. >>> alist = range(10)
13. >>> alist[0]
0
14. >>> alist[9]
9
15. >>> len(alist)
10
16. >>> [f(x) for x in range(10)]
[7, 12, 17, 22, 27, 32, 37, 42, 47, 52]
Recall that f(x) was defined above. If you are doing this exercise in a different session than the exercises above, redefine f(x).
17. >>> nums = [1, 2, 3]
>>> sum(nums)
6
18. >>> sum(alist)
45
19. >>> results = [f(x) for x in range(10)]
>>> results [7, 12, 17, 22, 27, 32, 37, 42, 47, 52]
>>> sum(results)
295
20. >>> [f(x) for x in range(5, 10)]
[32, 37, 42, 47, 52]
21. >>> [f(0.1 * x) for x in range(10)]
[7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5]
22. Evaluate the function h(x) from above on all integers between 0 and 10.
23. And on all even integers between 0 and 30.
24. Evaluate h(x) on every multiple of 0.1 between 0 (inclusive) and 1 (exclusive).
25. Evaluate 3*h(x) - 5*k(x) on all integers between 0 and and 10.
26. And on all integers between 90 and 100.
27. List the first 10 multiples of 7.
28. Find 0 + 1 + ... + 20.
29. Find 0 + 2 + 4 + ... + 20 (that is, just the evens).
30. Find the sum of the first 10 square numbers.
31. You know how to apply sum and len to lists of numbers.
i. Combine them to compute the mean ("average") of a list of numbers, such as [13, 19, 11, 7, 19].
ii. You saw how to define functions, like f(x). Write a function mean(x) that returns the mean of a list.
For example, mean([2, -1, 2]) should return 1.
More on Lists of Numbers
1. >>> a = [2*n for n in range(3)]
>>> b = [2*n for n in range(3, 8)]
>>> a
[0, 3, 4]
>>> b
[6, 8, 10, 12, 14]
>>> len(a)
3
>>> len(b)
5
>>> a + b
[0, 2, 4, 6, 8, 10, 12, 14]
>>> b + a
[6, 8, 10, 12, 14, 0, 2, 4]
>>> a + a
[0, 2, 4, 0, 2, 4]
Explain what each line accomplishes. What is the result of adding two lists together?
2. >>> 2 * a [0, 2, 4, 0, 2, 4] >>> [2*n for n in a] [0, 4, 8]
Explain the difference between the effects of the two statements. How are 2 * a and a + a related?
3. >>> b
[6, 8, 10, 12, 14]
>>> b[1:3]
[8, 10]
>>> b[2:5]
[10, 12, 14]
>>> b[-1]
14
Explain what is happening. In English, we might say that [8, 10] is a "sublist" of the list b.
4. >>> a
[0, 2, 4]
>>> a + [5]
[0, 2, 4, 5]
>>> a
[0, 2, 4]
>>> a.append(5)
>>> a
[0, 2, 4, 5]
>>> a.pop()
5
>>> a
[0, 2, 4]
What does append do? What does pop do? What is the difference between a + [5] and a.append(5)?
1. >>> a = [2*n for n in range(3)]
>>> b = [2*n for n in range(3, 8)]
>>> a
[0, 3, 4]
>>> b
[6, 8, 10, 12, 14]
>>> len(a)
3
>>> len(b)
5
>>> a + b
[0, 2, 4, 6, 8, 10, 12, 14]
>>> b + a
[6, 8, 10, 12, 14, 0, 2, 4]
>>> a + a
[0, 2, 4, 0, 2, 4]
Explain what each line accomplishes. What is the result of adding two lists together?
2. >>> 2 * a [0, 2, 4, 0, 2, 4] >>> [2*n for n in a] [0, 4, 8]
Explain the difference between the effects of the two statements. How are 2 * a and a + a related?
3. >>> b
[6, 8, 10, 12, 14]
>>> b[1:3]
[8, 10]
>>> b[2:5]
[10, 12, 14]
>>> b[-1]
14
Explain what is happening. In English, we might say that [8, 10] is a "sublist" of the list b.
4. >>> a
[0, 2, 4]
>>> a + [5]
[0, 2, 4, 5]
>>> a
[0, 2, 4]
>>> a.append(5)
>>> a
[0, 2, 4, 5]
>>> a.pop()
5
>>> a
[0, 2, 4]
What does append do? What does pop do? What is the difference between a + [5] and a.append(5)?
Let's play with numbers.
Create a new module. Save it as "Lists of Numbers" in you My PYTHON Programs folder.
1. Create a list called hare of the first 10 square numbers.
2. Extract the list starting with 9 and ending with 49.
3. Create a list called turtle with the next 10 square numbers.
4. Using hare and turtle make a list called hurdle of the first 20 square numbers.
5. Add the 21st square number to hurdle.
6. Remove the 21st square number from hurdle.
7. Compute the difference between the sum of turtle's elements and the sum of hare's elements.
8. You collect three sets of percentage data:
9. Suppose that you called the list with all the data from above alldata. Type the following:
>>> alldata.sort()
>>> alldata
What happened?
Create a new module. Save it as "Lists of Numbers" in you My PYTHON Programs folder.
1. Create a list called hare of the first 10 square numbers.
2. Extract the list starting with 9 and ending with 49.
3. Create a list called turtle with the next 10 square numbers.
4. Using hare and turtle make a list called hurdle of the first 20 square numbers.
5. Add the 21st square number to hurdle.
6. Remove the 21st square number from hurdle.
7. Compute the difference between the sum of turtle's elements and the sum of hare's elements.
8. You collect three sets of percentage data:
- Set 1: 76, 91, 85, 93, 77, 89, 91, 88
- Set 2: 56, 61, 75, 57, 63, 66, 55, 89
- Set 3: 91, 56, 88, 85, 95, 90, 83, 87
9. Suppose that you called the list with all the data from above alldata. Type the following:
>>> alldata.sort()
>>> alldata
What happened?