Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC101 Reminders All assignments are now posted.

Similar presentations


Presentation on theme: "CISC101 Reminders All assignments are now posted."— Presentation transcript:

1 CISC101 Reminders All assignments are now posted.
Winter 2019 CISC101 4/8/2019 CISC101 Reminders All assignments are now posted. Assignment 1 due today, 7pm in onQ. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today A few more techniques for formatting output (from last time).
Winter 2019 CISC101 4/8/2019 Today A few more techniques for formatting output (from last time). Finish up Building Expressions: Punctuation and Keywords. Start: Iteration or “Loops”. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

3 More Output Formatting
The str.format() method gives you complete control over how information is displayed in the console window. Here is an example from Exercise 2: >>> print("The variable a is: {0}, b is: {1} and c is: {2}.".format(a, b, c)) The variable a is: 123, b is: and c is: 21. Get more info from Exercise 2. Winter 2019 CISC101 - Prof. McLeod

4 Aside – .format() Method
.format() is like a function, but it is invoked differently. .format() is a member function of the string (or str) object. So, you must have a string object in order to invoke this method or “member function”. Don’t worry too much about this, for now… Winter 2019 CISC101 - Prof. McLeod

5 Formatted Strings in New Python Versions
This is even easier: print(f"The variable a is: {a}, b is: {b} and c is: {c}.") You can use formatting codes for each variable if you put them after a colon (:) inside the { }. Winter 2019 CISC101 - Prof. McLeod

6 Aside – the round BIF You can use the round BIF to round numbers to a certain number of digits before you display them. For example: >>> aNum = >>> print("Rounded is", str(round(aNum, 2))) Rounded is 15.46 The first argument to round() is the number to be rounded, the second argument is the number of digits you want to have after the decimal place. Winter 2019 CISC101 - Prof. McLeod

7 Back to Expressions Where an “expression” is really any line of Python Code. An expression consists of some mix of: Function or Method calls Literal Values Variables Operators Keywords Punctuation We have yet to discuss Keywords and Punctuation. Winter 2019 CISC101 - Prof. McLeod

8 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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

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

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

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

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

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

14 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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

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

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

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

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

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

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

21 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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

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

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

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

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

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

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

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

29 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

30 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. Winter 2019 CISC101 - Prof. McLeod

31 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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

32 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

33 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


Download ppt "CISC101 Reminders All assignments are now posted."

Similar presentations


Ads by Google