Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 6 using the math library

Similar presentations


Presentation on theme: "Class 6 using the math library"— Presentation transcript:

1 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, ...

2 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

3 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.

4 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 ...)

5 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))

6 From Lab 5: I.B. arithmetic is not exact: π ≠ math.pi Computer does calculations to compute sin(theta) – not exact.

7 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

8 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

9 computing factorial: print after the loop
prod=1 for i in range(1,11): prod=prod*i print(prod)

10 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)

11 What is the digit after ? e+157

12 What is the digit after ?

13 Preview of Objects Data types we have seen: int, float, string We can make "instances" of each: age=10 #int price = #float They have data – their values. They can do things: + * print

14 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

15 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.

16 Preview of Objects There are packages of predesigned Objects. We will get used to the idea by working with a package for graphics.

17 Introduction to graphics.py
Some of the objects we can use: GraphWin (windows for drawing in) Circles Polygons Points

18 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 ...

19 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 ...

20 The Graphics Window Made up of pixels – picture elements. (0,0) is upper left corner. x increases to the right y increases down

21 The Graphics Window

22 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 *

23 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

24 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

25 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?


Download ppt "Class 6 using the math library"

Similar presentations


Ads by Google