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.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

While loops.
Python Basics: Statements Expressions Loops Strings Functions.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python November 14, Unit 7. Python Hello world, in class.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Introduction to C Programming
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
Lists in Python.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Input, Output, and Processing
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Visual Basic Programming
For loops in programming Assumes you have seen assignment statements and print statements.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Xiaojuan Cai Computational Thinking 1 Lecture 8 Loop Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
REPETITION CONTROL STRUCTURE
Loops BIS1523 – Lecture 10.
Introduction To Repetition The for loop
Chapter 2 - Introduction to C Programming
CS 115 Lecture 8 Structured Programming; for loops
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Accumulators in Programming
While Loops in Python.
Chapter 2 - Introduction to C Programming
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Arrays, For loop While loop Do while loop
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
CISC101 Reminders All assignments are now posted.
CHAPTER 6: Control Flow Tools (for and while loops)
For loops Taken from notes by Dr. Neil Moore
While Loops in Python.
Class code for pythonroom.com cchsp2cs
Introduction to Computer Science
Presentation transcript:

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 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 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 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 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 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 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 tab character (\t) Example: Print (“a”, “b”, sep = “\t”) Output a b in one line Think about the output format in program 1

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

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

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)

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.

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

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

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