CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.

Slides:



Advertisements
Similar presentations
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Advertisements

Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
For Loops 2 ENGR 1181 MATLAB 9. For Loops and Looped Programming in Real Life As first introduced last lecture, looping within programs has long been.
For loops in programming Assumes you have seen assignment statements and print statements.
Spring 2006CISC101 - Prof. McLeod1 Announcements Assn 4 is posted. Note that due date is the 12 th (Monday) at 7pm. (Last assignment!) Final Exam on June.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 Today Numeric representation (or “How does binary and hexadecimal work?”). How can a CPU understand instructions written.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
CS 115 Lecture 8 Structured Programming; for loops
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Winter 2018 CISC101 11/9/2018 CISC101 Reminders
Winter 2018 CISC101 11/9/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 2 this week.
Lesson #6 Modular Programming and Functions.
CISC101 Reminders Assn 3 due Friday, this week.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Winter 2018 CISC101 11/19/2018 CISC101 Reminders
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
CISC101 Reminders Your group name in onQ is your grader’s name.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 11/27/2018 CISC101 Reminders
Winter 2018 CISC101 11/29/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
A First Book of ANSI C Fourth Edition
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 this week.
Chapter 5 Loops.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CISC124 Labs start this week in JEFF 155. Fall 2018
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Lesson #6 Modular Programming and Functions.
CMPE212 – Reminders The other four assignments are now posted.
CISC101 Reminders All assignments are now posted.
For loops Taken from notes by Dr. Neil Moore
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
CISC101 Reminders Assignment 2 due today.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CISC101 Reminders Assignment 2 due this Friday.
Winter 2019 CISC101 4/14/2019 CISC101 Reminders
Loops.
CISC101 Reminders Quiz 1 marking underway.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
Hossain Shahriar CISC 101: Fall 2011 Hossain Shahriar
Presentation transcript:

CISC101 Reminders Quiz 1 grading underway Next Quiz, next week. Winter 2018 CISC101 12/7/2018 CISC101 Reminders Quiz 1 grading underway Next Quiz, next week. Next Assignment (Assn 2) due Friday, next week – the 16th. The program we wrote last lecture is linked to the lecture notes page. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

Today Four more loop keywords. Demo: Calculating natural logs. An advanced example. Another in-class program? The for loop. Winter 2018 CISC101 - Prof. McLeod

Four More Loop Keywords break, continue, else, pass else and pass are used in other places too. Should not be used very often! Winter 2018 CISC101 - Prof. McLeod

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 completely out of a nested loop? Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

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 2018 CISC101 - Prof. McLeod

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

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 2.718281828459045 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

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

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

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

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

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

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

In-Class Problem 2 Display all positive numbers in-between two supplied numbers that divide both numbers evenly. “Even division” means zero remainder after division. Winter 2018 CISC101 - Prof. McLeod

Problem 2 Algorithm Obtain both numbers from the user. The values must be >= 1. Assume user enters an integer, but loop back if value not legal. Determine which of the two numbers is the smaller one. Loop a divisor from 1 to the smaller number: If the divisor divides both numbers evenly (use %) then display the divisor. Winter 2018 CISC101 - Prof. McLeod

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

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

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

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

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

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