Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 used for output

2 2 Printing in Python The function used in Python is print. It can have as many arguments as you wish Syntax: print ( arguments ) The argument list can use variables, expressions, constants The argument list can also be empty print()

3 3 Semantics of print When the print function is called, it evaluates the arguments and displays them on the shell window It displays them starting wherever the cursor is at the time If there is more than one argument, there will be one space put between them on the screen

4 4 Semantics of print When it finishes displaying them, it will output a “newline” character (also called a carriage return) This moves the cursor to the left side of the screen and down to the next line. print() is one way to output just a newline character

5 5 Extra arguments for print To give the programmer a little more control over the exact positioning of the outputs, there are some extra arguments you can use in the parentheses end= will specify what the print function should output when the list of arguments to output is finished

6 6 Extra arguments for print sep= will specify what should be put in between the arguments to separate them (note that this is just between arguments, not at the end of the line) These have string values Example: print(“abc”, “def”, sep=“++”) outputs abc++def on one line

7 7 Example of end= print(“joe”, end=“ “ ) that’s a space between the quotes Would output joe on the screen wherever the cursor was at that time and then a space and then the cursor would stay on that line. It would NOT move to the next line.

8 8 tab character (\t) Example: Print (“a”, “b”, sep = “\t”) Output a b in one line Think about the output format in program 1

9 9 loops in programming Many programs need to process huge amounts of data Each piece of data needs to go through the same instructions One option is to write out the instructions over and over again That’s not very practical or flexible What if I wrote a program to process invoices for 100 customers and I got some more customers? Rewrite the program?? No! A loop lets me specify what needs to be repeated and how many times it needs to be repeated

10 10 Two kinds of loops Definite You know that you want to execute the loop a certain number of times “there are 10 students’ grades” “Draw a standard chess board” (always 8 x 8 squares) “Quiz the user about the capitals of the 50 states” “do this for every character in the string” “do this for every item in the list” “do this for every line in the file”

11 11 Two kinds of loops Indefinite You don’t know how many times you want to execute the loop but you know there is a condition that is the stopping point “do this until the user wants to stop” “let the user enter numbers until they type -1” “do this until the user wins the game” “repeat this until this number reaches a certain level”

12 12 For loop – a definite loop The syntax of the for loop statement for variable in some list of values to assign to the variable : (colon!) Follow this line by all statements that should be repeated with an extra indent - This is how the interpreter knows what is in the loop and what’s not

13 For loop – a definite loop The semantics of the for loop statement At the start of the loop check to see if the loop should execute If not, skip the body and continue with next statement after loop If it should execute, Assign variable with the first item in the list Execute the body Repeat the test at the top of the loop to see if loop should execute again

14 Controlling the loop You don’t want a loop that doesn’t stop! Python uses several ways to control a loop The range function (gives integers) The contents of a list of values (gives the elements whatever type) The contents of a string (gives characters) The contents of a file (gives strings)

15 What is an accumulator? An accumulator is not new syntax, it is just a new way to using the assignment operator It is a logical concept that is used in most languages The general idea is that a variable is given the job of holding the “total” of several values. “Total” is in quotes because it is not always an arithmetic sum, it can be done using many different operations. An accumulator could hold the sum of 10 numbers, the concatenation (“sticking together”) of 5 strings, the product of 15 numbers, etc. An accumulator starts at a known value (like zero), and is changed by “adding” on a different value. The result is stored right back in the same variable.

16 Sometimes you need to know how many times something happens The number of numbers read in An accumulator is useful here too You can add a constant term to an accumulator variable If you do, that is called a counter A counter is a specialized form of an accumulator Just changes the accumulator by a constant value Num_count = Num_count + 1 DozenCount = DozenCount + 12 A Counter – a special form of an accumulator

17 Example Write a loop to calculate the sum from 1 to 50.

18 Examples Problem is to ask the user for some information about pizzas sold and report the total. The desired behavior is How many pizzas sold on Monday? 23 How many pizzas sold on Tuesday? 32 How many pizzas sold on Wednesday? 4 How many pizzas sold on Thursday? 35 How many pizzas sold on Friday? 15 Total is 109 pizzas


Download ppt "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."

Similar presentations


Ads by Google