Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.

Similar presentations


Presentation on theme: "Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1."— Presentation transcript:

1 Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1

2 Keywords Without keywords you cannot have a programming language. They are the “glue” that holds a program together. They allow you to give structure to your program so that it does what you want. You cannot use a keyword as a variable name. Python prides itself in having a relatively small set of keywords: CISC101 - Prof. McLeod2Winter 2016

3 Python Keywords False class finally is return None continue for lambda try True def from nonlocalwhile and del global not with as elif if or yield assert else import pass break except in raise CISC101 - Prof. McLeod3Winter 2016

4 Python Keywords, Cont. We have seen a few of these already: False class finally is return None continue for lambda try True def from nonlocalwhile and del global not with as elif if or yield assert else import pass break except in raise CISC101 - Prof. McLeod4Winter 2016

5 Python Keywords, Cont. We have used conditional keywords: False class finally is return None continue for lambda try True def from nonlocalwhile and del global not with as elif if or yield assert else import pass break except in raise CISC101 - Prof. McLeod5Winter 2016

6 Python Keywords, Cont. Next are loops: False class finally is return None continue for lambda try True def from nonlocalwhile and del global not with as elif if or yield assert else import pass break except in raise CISC101 - Prof. McLeod6Winter 2016

7 Looping Structures Loops add some serious power to a program. A loop allows a program to execute a series of instructions many, many times in a completely predictable way. No boredom involved!! CISC101 - Prof. McLeod7Winter 2016

8 Introducing Loops Two kinds of loops in python – while loops and for loops. while loops are simpler and for loops are more powerful. They are often interchangeable. Start out with while loops. Do some examples. Then some more serious examples doing some numerical calculations. Use the turtle for more examples? CISC101 - Prof. McLeod8Winter 2016

9 CISC101 - Prof. McLeod9 Repetition, Iteration or “Loops” Suppose we combine a conditional test with some kind of structure that allows us to branch back up to an earlier piece of code: etc. if trueif false Winter 2016

10 CISC101 - Prof. McLeod10 Repetition - Cont. The conditional test determines when to stop the repetition - as long as the condition is true, the loop keeps going. Something inside the looping part must affect what is tested in the condition - right? What if it did not - what would happen? Winter 2016

11 CISC101 - Prof. McLeod11 Repetition - Cont. A simple example - suppose we wanted the loop to execute only 20 times: etc. if trueif false i = 1 i < 21 i = i+1 Winter 2016

12 CISC101 - Prof. McLeod12 Repetition - Cont. The number of repetitions is controlled by changing the limit value for the loop counter – i, in the example on the previous slide. That example had i increasing by one each time. The loop counter was being incremented by one. It could have been incremented by some other value, 2, 3, or whatever. You could use something like “ i = i * 2 ” to increment the counter. If the counter is decreased in value each time, it is being “decremented”. Winter 2016

13 CISC101 - Prof. McLeod13 Repetition - Cont. There are lots of other ways of stopping a loop. Sometimes you don’t need to count iterations at all. But you still need a conditional expression that is affected by something inside the loop. Winter 2016

14 CISC101 - Prof. McLeod14 Repetition - Cont. Suppose, in the previous example, i was decremented by one instead. What would happen? etc. if trueif false i = 1 i < 21 i = i - 1 Winter 2016

15 CISC101 - Prof. McLeod15 Repetition - Cont. The dreaded “infinite loop”! The interpreter will not prevent you from coding a loop like the one shown - it will run! And run, and run, and run, and run, and run, and run, and run, and run, and run, and run… As a programmer, you must be “on guard” for such logic errors in your code. Winter 2016

16 CISC101 - Prof. McLeod16 while loop A while loop can be used to code the structure shown in the flowchart above (the “increment” one): i = 1 while i < 21 : print(i) i = i + 1 Winter 2016

17 CISC101 - Prof. McLeod17 while loop syntax: while boolean_expression : line1 line2 … As long as boolean_expression is True, the statements in the loop continue to execute. while loop - Cont. Winter 2016

18 Loop Demos You should be ready to work on Exercise 4, which has more loop exercises and a couple of turtle exercises (later). Let’s do a couple in class. CISC101 - Prof. McLeod18Winter 2016

19 Factorial Calculation Demo Write a program that displays all the values of n! (or “n factorial”) for 2! up to the user supplied value of n. For example, 5! = 5 * 4 * 3 * 2, which is 120. See FactorialDemo.py CISC101 - Prof. McLeod19Winter 2016

20 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 CISC101 - Prof. McLeod20Winter 2016

21 SumNums.py Example, Cont. Shows a different way of stopping a loop that does not use a incrementing variable. Also demonstrates the use of an if statement inside a loop. The if statement is executed once for every iteration of the outer loop. CISC101 - Prof. McLeod21Winter 2016


Download ppt "Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1."

Similar presentations


Ads by Google