Download presentation
Presentation is loading. Please wait.
1
CISC101 Reminders Quiz 2 this week.
Winter 2018 CISC101 12/8/2018 CISC101 Reminders Quiz 2 this week. Next Assignment (Assn 2) due Friday, the 16th. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
2
Today Demo: Calculating natural logs. An advanced example.
The for loop. Intro to the slice operator, and the len() and range() BIFs. Intro to the turtle? Winter 2018 CISC101 - Prof. McLeod
3
Natural Logs Demo Uses a numerical technique to estimate the value of natural logs. Explain what natural logs are first, and then look at the infinite series (a Taylor’s series) used for the calculation. How to stop the calculation loop? You are not responsible for the math or the code in this demo Winter 2018 CISC101 - Prof. McLeod
4
Another Demo - Natural Logs (Math!)
Consider log10 first: This is the power of 10 that gives you a certain number. For example log10(100) is 2 since 102 is 100. Natural logs don’t use base 10, they use base e, where e is The natural log function is usually called “ln()” (but in Python it is math.log(). Base 10 log is called as math.log10().) Winter 2018 CISC101 - Prof. McLeod
5
Demo – Computing Natural Logs
Here is a series to compute natural logs: or: Using x = 1/3 will provide ln(2), for example. Winter 2018 CISC101 - Prof. McLeod
6
Computing Natural Logs, Cont.
To calculate the natural log of any number, y, where y is greater than one, you can first calculate x using: As you can see, x will always be less than 1. Winter 2018 CISC101 - Prof. McLeod
7
Computing Natural Logs, Cont.
The natural log is not defined for values of y less than or equal to zero. But if y is between zero and one – a fractional value – calculate the log of 1/y and negate it: Winter 2018 CISC101 - Prof. McLeod
8
Natural Logs Demo Write a program to estimate the natural log of a supplied number, y, where the value must be greater than zero. Display the result and the number of iterations required. Compare your estimated value against math.log(). Winter 2018 CISC101 - Prof. McLeod
9
CISC101 Natural Logs Demo, Cont. The input part is just like what we have done before, but how does the calculation part work? Since you can’t sum to infinity, how do you decide when to stop? (Note that this is a pretty advanced example!!) See ComputingLogs.py Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
10
Demo Summary The demo showed how a loop can be used to carry out a serious calculation. And we saw another way to stop a loop which works because floats are limited in size. You should be able to write any of the code in the demo – no advanced code was used, but you do not need to understand the math! Winter 2018 CISC101 - Prof. McLeod
11
The Other Loop… The other kind of looping syntax uses the for keyword.
It can be more powerful, particularly with collections – producing very compact code. But you could almost always use a while loop instead – it would just take more code to carry out the same operation. We’ll make more use of the for loop later in the course. Winter 2018 CISC101 - Prof. McLeod
12
for variable_name in iterable :
CISC101 The for Loop Syntax: for variable_name in iterable : You make up variable_name iterable can be a collection, such as a string, list, tuple, set or dict. It can also be an object produced by certain BIFs such as reversed() or range(). It can even be a file! Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
13
The for Loop, Cont. It is still interchangeable with a while loop. These two “snippets” act in the same way: testString = "I like watching videos!" i = 0 while i < len(testString) : print(testString[i]) i = i + 1 for aChar in testString : print(aChar) Winter 2018 CISC101 - Prof. McLeod
14
Some New Syntax Two new things snuck their way into the code on the previous slide: len( ) BIF is used with collections and gives you the number of elements in the collection. For a string, it gives you the length of the string. The square brackets: [ ] is called the “slice operator” and returns a single element (or character) or even a range of elements or characters from a collection. Winter 2018 CISC101 - Prof. McLeod
15
Aside - Slice Operator It is placed after a collection (a list, tuple, string, etc.) or a variable that is a collection type. Used as in: [ # ] or [ # : # ] The first way gives you the element at the location specified. The other way gives you all elements between the first location and one less than the last location. We will look at this operator again later. Winter 2018 CISC101 - Prof. McLeod
16
The range() BIF This BIF generates an iterable and is often used with a for loop. Remember our while loop code to display 1 to 20?: i = 1 while i < 21 : print(i) i = i + 1 See RangeForLoopsDemo.py Winter 2018 CISC101 - Prof. McLeod
17
Turtle Graphics The “Turtle” is an easy way of drawing to the screen.
CISC101 Turtle Graphics The “Turtle” is an easy way of drawing to the screen. One use is to illustrate how loops work. See section 24.1 in the Python Standard Library. To start you’ll need to import the turtle module: from turtle import * Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
18
A Few turtle Commands To make the turtle look like a “turtle”:
shape(“turtle”) To set the line thickness and line colour: pensize(5) pencolor(“green”) The turtle starts out at the “home” position, (0, 0), facing right or “east”. To draw, just issue commands to move the turtle. Winter 2018 CISC101 - Prof. McLeod
19
A Few More turtle Commands
Turning: right(num_degrees) # clockwise left(num_degrees) # counter-clockwise Moving in the direction he is pointing; forward(length) Changing speed: speed(5) # 0 is no animation, 1 slow 10 fastest See TurtleDemo1.py Winter 2018 CISC101 - Prof. McLeod
20
Filling Commands Use begin_fill(), then draw, followed by end_fill()
to fill a closed, drawn area. See TurtleDemo2.py See TurtleDemo3.py to see how to draw a filled circle. Winter 2018 CISC101 - Prof. McLeod
21
Regular Polygons Modify how circle() is invoked to draw regular polygons instead of circles. See TurtleDemo4.py Draw text, too! See TurtleDemo5.py Fun! Winter 2018 CISC101 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.