CISC101 Reminders All assignments are now posted.

Slides:



Advertisements
Similar presentations
Introduction to Python
Advertisements

Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Software Development. Software Development Loop Design  Programmers need a solid foundation before they start coding anything  Understand the task.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Python Let’s get started!.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - 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.
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.
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:
Software Development.
Last Time Unary operators: Other Assignment operators
CS1022 Computer Programming & Principles
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Introduction to Python
CS 115 Lecture 8 Structured Programming; for loops
Variables, Expressions, and IO
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Winter 2018 CISC101 11/9/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Assn 3 due Friday, this week.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Selection CIS 40 – Introduction to Programming in Python
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
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
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
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.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Computing Fundamentals
Topics Introduction to Functions Defining and Calling a Void Function
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CISC124 Labs start this week in JEFF 155. Fall 2018
CMPE212 – Reminders The other four assignments are now posted.
Winter 2019 CISC101 4/8/2019 CISC101 Reminders
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
Winter 2019 CISC101 4/14/2019 CISC101 Reminders
CISC101 Reminders Quiz 1 marking underway.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
CISC101 Reminders All assignments are now posted.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
Software Development Techniques
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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

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

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: 45678 and c is: 21. Get more info from Exercise 2. Winter 2019 CISC101 - Prof. McLeod

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

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

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 = 15.45789 >>> 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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