Class 6 using the math library using sin and cos – converting from degrees to radian. factorial – accumulating a product float vs int Preview of Objects graphics.py the graphics window Point, Circle, ...
From Lab 5: I.B. angleInRadians = angleInDegree * π/180 math.cos() and math.sin() use angles in radians. If angle in degrees, must convert to radians to use with sin and cos. angleInRadians = angleInDegree * π/180
Why math. ? import math math.sin(), math.cos(), math.pi I don't have to worry about using a variable or function with the same name but a different meaning.
Why math. ? import math math.sin(), math.cos(), math.pi (If we had said from math import * we wouldn't need the math. Could say sin(theta), pi ...)
From Lab 5: I.B. print(deg, sin(deg*π/180), cos(deg*π/180)) or nicer formatting print(deg, "\t", sin(deg*π/180), "\t", cos(deg*π/180))
From Lab 5: I.B. arithmetic is not exact: π ≠ math.pi Computer does calculations to compute sin(theta) – not exact.
computing factorial: accumulating a product. prod=1 for i in range(1,11): prod=prod*i print(prod) 1 2 6 24 120 720 5040 40320 362880 3628800
computing factorial: print in the loop prod=1 for i in range(1,11): prod=prod*i print(prod) 1 2 6 24 120 720 5040 40320 362880 3628800
computing factorial: print after the loop prod=1 for i in range(1,11): prod=prod*i print(prod) 3628800
difference in float and int of 100! prod=1 for i in range(1,101): prod=prod*i print(prod) fprod=1.0 fprod=fprod*float(i) print(fprod)
What is the digit after ...441? 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 9.33262154439441e+157
What is the digit after ...441? 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 9.3326215443944100000000000000000....
Preview of Objects Data types 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
Downloading .py files from my website trouble downloading some .py files instead, download .txt file, change the extension to .py note this doesn't work for all kinds of files. Can't just change .docs to .txt. Can save a word file: "Save as" plain text, .txt
Preview of Objects Object Oriented Programming: We can make "data types" of our own – called Objects, and package them with functions (methods)– 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
Sample code #SampleWindow2.py #Baruch #Demonstrate some graphics objects # and what they can do. #Import the graphics library, graphics.py. #Don't need to use graphics. prefix. from graphics import *
Sample code #Create a graphics window with the GraphWin constructor sw=GraphWin("Sample Window",600, 400) #Create and display a Point dot=Point(50,100) #Constructor - builds a Point object dot.draw(sw) #The Point draws itself to the sw window. #Wait for a mouse click sw.getMouse() #The window waits for a mouse click
Sample code #Create and display a circle circ=Circle(dot,30) #Construct a circle circ.setFill("red") #Change its color circ.draw(sw) #The circle draws itself. #Wait for a mouse click sw.getMouse() #The window waits for a mouse click sw.close() #close the window
Things to try. Change the circle to blue and redraw it. Create another circle rtCirc=Circle(Point(500,300),60) Make it green. Do they both turn green?