Exercises for Lesson 3
Exercise 1: Operations on ints and floats
For this exercise, predict the output and then type each of the following commands in VS Code.
1a: basic operations
print(7 + 3.0)
print(7.0 - 3.0)
print(7 * 3)
print(7 ** 3)
print(7 / 3) # regular division
print(7.0 / 3)
print(7 // 3) # integer division
print(7 % 3) # modulo (remainder)1b: type conversions
int(2.718)
round(2.718)
float(2.718)
float(round(2.718)) # order of operations?
float("2.718")
int("2.718")
float("2,718")
Exercise 2: Playing with range()
2a: generating a sequence of numbers
What inputs to range() would give each of the following sequences?
a) 0, 1, 2, 3, 4, 5, 7, 8
b) 12, 13, 14
c) 0, 4, 8, 12
d) -3, -1, 1, 3, 5, 7
e) 5, 4, 3, 2, 1
f) 1, 3, 5, 7, 9, 112b: generating an alternating sequence
Using range() and a for loop, write a program to print out the values 1, -1, 1, -1, 1, .... The program should prompt the user for n (how many numbers to print) and then print each number.
Here is an example interaction:
What is n? 5
Results:
1
-1
1
-1
12c: putting it all together
Write a program to print out the following sequence: 1, -3, 5, -7, 9, -11, … . The program should prompt the user for n (how many numbers to print) and then print each number.
Here is an example interaction:
What is n? 3
Results:
1
-3
5Hint: Think carefully about how the values you get from range() relate to the values you need to print.
Exercise 3: Summing by accumulation
Write a program to find the sum of the first n natural numbers, where the value of n is provided by the user. (Hint: you should use the accumulator pattern.)
Here is an example interaction:
What is n? 10
The sum of 1 to 10 is: 55Exercise 4: Approximating pi
Write a program that approximates the value of pi by summing the terms of this series: 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + … . The program should prompt the user for n (the number of terms to sum) and then output the sum of the first n terms of this series. Have your program subtract the approximation from the value of math.pi to see how accurate it is.
Here is an example interaction:
What is n? 3
The sum of the first 3 terms of 4/1 - 4/3 + 4/5 + ... is: 3.466666666666667And another:
What is n? 10000000
The sum of the first 10000000 terms of 4/1 - 4/3 + 4/5 + ... is: 3.1415925535897915To compare to math.pi:
import math
print(math.pi) # prints 3.141592653589793Exercise 5: Trying out graphics.py
Read through and experiment with this program, which draws a circle and a point to the
screen using the graphics.py module.
Note that you’ll want the file graphics.py either saved in the same folder as where you save simpleShapes.py or any other file that imports it, or you’ll want graphics.py saved somewhere that is on your system path (if you’re not familiar with that or don’t want to, it’s easy enough to always copy it to the folder where you’re saving your code).
# File: simpleShapes.py
# Purpose: A simple graphics program.
# Author: Tanya Amert
from graphics import *
def main():
# Create a point object
point = Point(50, 100)
# Print out some information about that point
x = point.getX()
y = point.getY()
print("The point is at ({0},{1})".format(x, y))
# Create a circle object
radius = 20
circle = Circle(point, radius)
# Print out some information about that circle
circleLoc = circle.getCenter()
cx = circleLoc.getX()
cy = circleLoc.getY()
r = circle.getRadius()
print(f"The circle is at ({cx},{cy}) and has radius {r}")
# ^ documentation for format strings: https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings
# Give each a color
point.setFill("white")
circle.setFill("blue")
# Create a window object
win = GraphWin("My window for simple shapes")
# Draw the circle and the point in the window
circle.draw(win)
point.draw(win)
## Questions:
# - Does anything change if you swap the order of the draw statements?
# - How can you change the size of the circle?
# - How can you change the color of the circle?
# - How big is a Point?
# - Which direction has increasing x values? What about increasing y values?
# Wait for the user to click, then exit
win.getMouse()
win.close()
main()