Presentation is loading. Please wait.

Presentation is loading. Please wait.

class 5 retrieving a previous statement in Thonny or IDLE // %

Similar presentations


Presentation on theme: "class 5 retrieving a previous statement in Thonny or IDLE // %"— Presentation transcript:

1 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

2 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

3 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

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

5 18 // 7 put in rows of 7, number of full rows X X X X X X X 2 X X X X

6 14 % 7 put in rows of 7, number in last NOT FULL row (might be 0 ): X X X X X X X

7 14 // 7 put in rows of 7, number of full rows X X X X X X X 2

8 Order of Operations: parentheses ** * and / and // and % + and - Example: x = Left to right since + and - are the same “level” 9.0 13.7 NOT x = 8.2 4.3 WRONG!!

9 Compute the following expressions
5 // 10 * 2 5 / 10 * 2

10 Compute the following expressions
2**3** (2**3)**0

11 What is the output? a = 725 b = a%10 c = a//10 d = c%10 e = a//100
print(a) print(e, d, b)

12 What is the output? >>>list( range(3,7))

13 What is the output? >>>list( range(7,1))

14 What is the output? >>>list( range(4,10,2))

15 What is the output? >>>for decimal in range(4,5,.1): print(decimal, end=", ")

16 How could we do it, get 4.0, 4.1, 4.2 etc?
>>>for k in range( ): print( , end=", ")

17 What is the output? for k in list[77, 3.14, "hot dog"]: print(k, end=" ")

18 What is the output? for count in range(3): num = eval(input("Enter a number: ")) print(num+1, end=', ')

19 What is the output? sum = 0 for val in range(5, 50, 10): sum=sum + val print(sum)

20 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

21 accumulating a sum If we do just the loop again do we get the same answer?

22 accumulating a sum If we do just the loop again do we get the same answer? NO Need to reset sum to 0.

23 What is the output? for j in range (1, 4): sum = 0 sum = sum + j
print(sum)

24 What is the output? mystery = 100 for k in range ( 5 ):
mystery=mystery-10 print(mystery)

25 Observe mystery = 100 for k in range ( 5 ): mystery=mystery-10
print(mystery) k isn't even used inside the loop! Just for counting.

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

27 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

28 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

29 Trig Review: Definition of sine and cosine (Unit circle, radius=1) (cos sin ) (cos sin ) From Wikipedia, modified

30 cos(0) = sin(0) = 1.0 0.0 (cos(0), sin(0))

31 cos(PI/2) = sin(PI/2) = 0.0 1.0 (cos(PI/2), sin(PI/2))

32 cos(PI) = sin(PI) = -1.0 0.0 (cos(PI), sin(PI))

33 Preview of Objects Data type 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

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

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

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

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

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

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

40 The Graphics Window


Download ppt "class 5 retrieving a previous statement in Thonny or IDLE // %"

Similar presentations


Ads by Google