Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.

Similar presentations


Presentation on theme: "CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm."— Presentation transcript:

1 CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Winter 2018 CISC101 11/27/2018 CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Back to Building Expressions: Iteration or “Loops”. Punctuation
Keywords Iteration or “Loops”. Winter 2018 CISC101 - Prof. McLeod

3 CISC101 Punctuation All those “things” that are not operators, keywords, literals or variables. Some you see and some you don’t! Many symbols have a different meaning depending on context. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

4 Whitespace Punctuation
The ones you don’t see (ie. non-printing): Spaces Tabs Carriage return Line feed Spaces are used as delimiters in expressions, for indentation and to improve readability. CR/LF is known as a “Newline”. Used at the end of lines and to put empty lines in a program (for better readability, again). Winter 2018 CISC101 - Prof. McLeod

5 Tabs and Indentation Press the <tab> key to get an indent in your code, if you need one. Use the <Backspace> key to get out of an indentation. Don’t type spaces for indents! Indents are very important in Python, they are not just “whitespace”! IDLE starts indenting automatically, especially after you type a : Winter 2018 CISC101 - Prof. McLeod

6 Indentation, Cont. Indentation indicates “containment” of blocks of code. A block can be one or many lines of code. For example, code could be contained: In a function definition In an if statement In a loop As soon as you “de-dent” a line of code, the subsequent code will no longer be contained in whatever block you were defining. Winter 2018 CISC101 - Prof. McLeod

7 Punctuation You Can See
' " # \ ' and " for string literals. # used to start a comment. \ is used to continue a long line of code onto the next line. Winter 2018 CISC101 - Prof. McLeod

8 More Punctuation You Can See
( ) [ ] { } , : . ; @ These are all multi-use symbols and their meaning depends on context. Probably better to learn about them in that context! Winter 2018 CISC101 - Prof. McLeod

9 Keywords Without keywords you cannot have a programming language.
CISC101 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: Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

10 Python Keywords False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise Winter 2018 CISC101 - Prof. McLeod

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

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

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

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

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

16 CISC101 Repetition 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: if true if false etc. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

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

18 Repetition - Cont. A simple example - suppose we wanted the loop to execute only 20 times: i = 1 if true i < 21 if false i = i+1 etc. Winter 2018 CISC101 - Prof. McLeod

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

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

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

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

23 while loop A while loop can be used to code the structure shown in the flowchart above (the “increment” one on slide 18): i = 1 while i < 21 : print(i) i = i + 1 Winter 2018 CISC101 - Prof. McLeod

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

25 Exercise 4 You should be ready to work on Exercise 4, which has more loop exercises and a couple of turtle exercises (more on the turtle later). Winter 2018 CISC101 - Prof. McLeod

26 Factorial Calculation Demo
CISC101 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 Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

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

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


Download ppt "CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm."

Similar presentations


Ads by Google