Download presentation
Presentation is loading. Please wait.
1
CISC101 Reminders Quiz 1 marking underway.
Winter 2019 CISC101 4/23/2019 CISC101 Reminders Quiz 1 marking underway. Assignment 2 due a week from Friday. You have all the lecture material you need for this assignment. Style will be graded this time. Quiz 2 next week, as well. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
2
Quiz 2 All Python. Up to and including this Friday’s lecture:
Expressions Console I/O Conditionals while Loops for Loops Does not include the “turtle”. Same format & rules as for Quiz 1. Winter 2019 CISC101 - Prof. McLeod
3
Today Continue Iteration or “Loops”. Winter 2019
Winter 2019 CISC101 4/23/2019 Today Continue Iteration or “Loops”. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
4
while loop - Cont. while loop syntax:
while boolean_expression : line1 line2 … As long as boolean_expression is True, the statements in the loop continue to execute. Winter 2019 CISC101 - Prof. McLeod
5
Summing Numbers Demo Obtain any number of numbers from the user, sum them up and then display the average of the numbers. (Ignore the possibility of non-numeric input.) How do we stop such a process? See SumNums.py Winter 2019 CISC101 - Prof. McLeod
6
SumNums.py Example, Cont.
Shows a different way of stopping a loop that does not use a incrementing variable. Is this the only way to control the loop in this case? Are there other conditions that you could use? Also demonstrates the use of an if statement inside a loop. The if statement is executed once for every iteration of the outer loop. Winter 2019 CISC101 - Prof. McLeod
7
Four More Loop Keywords
break, continue, else, pass else and pass are used in other places too. Should not be used very often! Winter 2019 CISC101 - Prof. McLeod
8
Use of break and continue With Loops
CISC101 Use of break and continue With Loops Don’t use these keywords unless you feel it makes your code easier to read and debug! break exits a loop immediately continue jumps to the next iteration immediately. See BreakContinueDemo.py Does break kick you out of a nested loop? Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
9
Use of else and pass With Loops
else with a loop gives you a chance to see how the loop exited - if a normal exit, the else is executed. See LoopElseDemo.py pass is used when a statement is required, but you don’t have anything you want to do! (Or you have not yet written the code.) See PassDemo.py Winter 2019 CISC101 - Prof. McLeod
10
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 2019 CISC101 - Prof. McLeod
11
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 2019 CISC101 - Prof. McLeod
12
Demo – Computing Natural Logs
Here is a series to compute natural logs: or: Using x = 1/3 will provide ln(2), for example. Winter 2019 CISC101 - Prof. McLeod
13
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 2019 CISC101 - Prof. McLeod
14
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 2019 CISC101 - Prof. McLeod
15
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 2019 CISC101 - Prof. McLeod
16
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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
17
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 2019 CISC101 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.