class 5 retrieving a previous statement in Thonny or IDLE // % // % order of operations computing ones, tens, and hundreds range(_), range(_,_), range(_,_,_) accumulating a sum math library trig review preview of Objects Graphics intro
retrieving previous statements in the shell. In Thonny, up and down arrow keys. In IDLE: mac: control p (previous), control n (next) windows: alt p, alt n
Given a number of days (days) how many full weeks is it? Put the answer in weeks. How many days left over? Put the answer in extra_days. Example: 18 days comes out to 2 weeks and 4 extra days. 2 R 4 7 18 18 // 7 = 2 18 % 7 = 4
18 % 7 put in rows of 7, number in last NOT FULL row (might be 0 ): X X X X X X X X X X X 4
18 // 7 put in rows of 7, number of full rows X X X X X X X 2 X X X X
14 % 7 put in rows of 7, number in last NOT FULL row (might be 0 ): X X X X X X X
14 // 7 put in rows of 7, number of full rows X X X X X X X 2
Order of Operations: parentheses ** * and / and // and % + and - Example: x = 12.5 - 3.5 + 4.7 Left to right since + and - are the same “level” 9.0 13.7 NOT x = 12.5 - 3.5 + 4.7 8.2 4.3 WRONG!!
Compute the following expressions 5 // 10 * 2 5 / 10 * 2
Compute the following expressions 2**3**0 (2**3)**0
What is the output? a = 725 b = a%10 c = a//10 d = c%10 e = a//100 print(a) print(e, d, b)
What is the output? >>>list( range(3,7))
What is the output? >>>list( range(7,1))
What is the output? >>>list( range(4,10,2))
What is the output? >>>for decimal in range(4,5,.1): print(decimal, end=", ")
How could we do it, get 4.0, 4.1, 4.2 etc? >>>for k in range( ): print( , end=", ")
What is the output? for k in list[77, 3.14, "hot dog"]: print(k, end=" ")
What is the output? for count in range(3): num = eval(input("Enter a number: ")) print(num+1, end=', ')
What is the output? sum = 0 for val in range(5, 50, 10): sum=sum + val print(sum)
Accumulating a sum sum = 0 #initialize accumulator for val in range(5, 50, 10): sum=sum+val print("partial sum is", sum) print("final sum is", sum) #out of loop
accumulating a sum If we do just the loop again do we get the same answer?
accumulating a sum If we do just the loop again do we get the same answer? NO Need to reset sum to 0.
What is the output? for j in range (1, 4): sum = 0 sum = sum + j print(sum)
What is the output? mystery = 100 for k in range ( 5 ): mystery=mystery-10 print(mystery)
Observe mystery = 100 for k in range ( 5 ): mystery=mystery-10 print(mystery) k isn't even used inside the loop! Just for counting.
From Lab 4: add 1/1 + 1/2 + 1/3 +... sum = 0 for k in range (1,101,1): sum=sum + 1/k print(sum)
Using the Math Library Python Mathematics English pi An approximation of pi e An approximation of e sqrt(x) The square root of x sin(x) sin x The sine of x cos(x) cos x The cosine of x tan(x) tan x The tangent of x asin(x) arcsin x The inverse of sine x acos(x) arccos x The inverse of cosine x atan(x) arctan x The inverse of tangent x Python Programming, 3/e
Using the Math Library Python Mathematics English log(x) ln x The natural (base e) logarithm of x log10(x) The common (base 10) logarithm of x exp(x) The exponential of x ceil(x) The smallest whole number >= x floor(x) The largest whole number <= x Python Programming, 3/e
Trig Review: Definition of sine and cosine (Unit circle, radius=1) (cos sin ) (cos sin ) From Wikipedia, modified
cos(0) = sin(0) = 1.0 0.0 (cos(0), sin(0))
cos(PI/2) = sin(PI/2) = 0.0 1.0 (cos(PI/2), sin(PI/2))
cos(PI) = sin(PI) = -1.0 0.0 (cos(PI), sin(PI))
Preview of Objects Data type we have seen: int, float, string We can make "instances" of each: age=10 #int price = 57.23 #float They have data – their values. They can do things: + * print
Preview of Objects Object Oriented Programming: We can make "data types" of our own – called Objects, and package them with functions – things they can do. We will build our own objects later.
Preview of Objects There are packages of predesigned Objects. We will get used to the idea by working with a package for graphics.
Introduction to graphics.py Some of the objects we can use: GraphWin (windows for drawing in) Circles Polygons Points
Introduction to graphics.py Each Circle we make is an "instance" of a Circle. Each instance of a Circle has its own data: its center its color its radius ...
Introduction to graphics.py Each Circle we make is an "instance" of a Circle. Each instance of a Circle can do things: draw itself in a window change color report the x coordinate of its center clone itself ...
The Graphics Window Made up of pixels – picture elements. (0,0) is upper left corner. x increases to the right y increases down
The Graphics Window