for while ' while ' for * for <var> in <sequence>: <body> $ %%" 0 *0



Similar documents
Python Programming: An Introduction To Computer Science

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 5 Program Control

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

The While Loop. Objectives. Textbook. WHILE Loops

While Loop. 6. Iteration

PYTHON Basics

Moving from C++ to VBA

>

Python Programming, 1/e 1

Chapter 2 Writing Simple Programs

Introduction to Matlab

Visual Logic Instructions and Assignments

Introduction to Python for Text Analysis

Computational Mathematics with Python

How do sort this list using one line? a_list.sort()

Python Lists and Loops

Introduction to: Computers & Programming: Input and Output (IO)

CRASH COURSE PYTHON. Het begint met een idee

Computational Mathematics with Python

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Enter Here -->>> Business Credit Building Course Scam or Work?

Python Basics. S.R. Doty. August 27, Preliminaries What is Python? Installation and documentation... 4

Object Oriented Software Design

Positional Numbering System

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

SNMP-1 Configuration Guide

Repetition Using the End of File Condition

Creating a Simple, Multithreaded Chat System with Java

Conditionals (with solutions)

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Adding and Subtracting Positive and Negative Numbers

VB Controls and Events. Introduc)on. Program Planning and Flowcharts Visual Basic Visual basic Interface VB Controls CreaGng a Project

Python Programming: An Introduction to Computer Science

Reading Input From A File

Character Translation Methods

Sample CSE8A midterm Multiple Choice (circle one)

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O


High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

LINEAR INEQUALITIES. less than, < 2x + 5 x 3 less than or equal to, greater than, > 3x 2 x 6 greater than or equal to,

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 6 - File IO

Compiler Construction

1. a procedure that you perform frequently. 2. Create a command. 3. Create a new. 4. Create custom for Excel.

6. Control Structures

Scientific Notation. Section 7-1 Part 2

JavaScript: Arrays Pearson Education, Inc. All rights reserved.

1 Description of The Simpletron

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

7-1 Java Au Naturel by William C. Jones 7-1

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

The Little Man Computer

Click Here -->>> Business Credit Building Course User Experience > Check Here <

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

JavaScript: Control Statements I

ALGORITHMS AND FLOWCHARTS. By Miss Reham Tufail

2 Matlab Programming, IO, and strings

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Computational Mathematics with Python

Intersection of Convex Objects: The Method of Separating Axes

Computer Programming Lecturer: Dr. Laith Abdullah Mohammed

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

Chapter 3 Writing Simple Programs. What Is Programming? Internet. Witin the web server we set lots and lots of requests which we need to respond to

SAS Macros as File Management Utility Programs

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Chapter 8 Selection 8-1

Section IV.1: Recursive Algorithms and Recursion Trees

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Math 0306 Final Exam Review

United States Naval Academy Electrical and Computer Engineering Department. EC262 Exam 1

Algorithm Design and Recursion

Identifying Invalid Social Security Numbers

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Moving from CS 61A Scheme to CS 61B Java

Package HadoopStreaming

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Reading and Writing PCD Files The PCD File Format The Grabber Interface Writing a Custom Grabber PCL :: I/O. Suat Gedikli, Nico Blodow

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Python Evaluation Rules

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Transcription:

# & for while while # * # & *, /01* for * 2 for <var> in <sequence>: <body>,var 0 2, /01* *** 0 *0 *& 4 00 *0* *

/01* 4 6, 4 * /01* Input the count of the numbers, n Initialize sum to 0 Loop n times Input a number, x Add x to sum Output average as sum/n 5 /01* # average1py # A program to average a set of numbers # Illustrates counted loop with accumulator def main: n = inputhow many numbers do you have? sum = 00 for i in rangen: x = inputenter a number >> sum = sum x print \nthe average of the numbers is, sum / n 8&99sum/n 6 /01* How many numbers do you have? 5 Enter a number >> 2 Enter a number >> 45 Enter a number >> 4 Enter a number >> 76 Enter a number >> 45 The average of the numbers is 464 7 9 # 0** * 4** 0 * for * 4 * 0* 4 0** * 46 0

while <condition>: <body> condition,#0 if 2, 4 0* *, :,while 99 i = 0 while i <= 10: print i i = i 1 for for i in range11: print i while 2 i & 9 for while * ; i = 0 while i <= 10: print i 4*< 4 i 29*9, 9 8* i 9, 5

4 < *0 *0 6 * * 1**** 00* < count 7 9 0 4 set moredata to yes while moredata is yes get the next data item process the item ask user if there is moredata * initialize sum to 00 initialize count to 0 set moredata to yes while moredata is yes input a number, x add x to sum add 1 to count ask user if there is moredata output sum/count # average2py # A program to average a set of numbers # Illustrates interactive loop with two accumulators def main: moredata = yes sum = 00 count = 0 while moredata[0] == y: x = inputenter a number >> sum = sum x count = count 1 moredata = raw_inputdo you have more numbers yes or no? print \nthe average of the numbers is, sum / count =, >?9@A* BCBCBC Enter a number >> 2 Do you have more numbers yes or no? y Enter a number >> 45 Do you have more numbers yes or no? yes Enter a number >> 4 Do you have more numbers yes or no? yup Enter a number >> 76 Do you have more numbers yes or no? y Enter a number >> 45 Do you have more numbers yes or no? nah The average of the numbers is 464

get the first data item while item is not the sentinel process the item get the next data item *,,* 4* *9 * # averagepy # A program to average a set of numbers # Illustrates sentinel loop using negative input as sentinel def main: sum = 00 count = 0 x = inputenter a number negative to quit >> while x >= 0: sum = sum x count = count 1 x = inputenter a number negative to quit >> print \nthe average of the numbers is, sum / count 5 Enter a number negative to quit >> 2 Enter a number negative to quit >> 45 Enter a number negative to quit >> 4 Enter a number negative to quit >> 76 Enter a number negative to quit >> 45 Enter a number negative to quit >> 1 The average of the numbers is 464 * * * 7 9

4 D* = 4 > A6 initialize sum to 00 initialize count to 0 input data item as a string, xstr while xstr is not empty convert xstr to a number, x add x to sum add 1 to count input next data item as a string, xstr Output sum / count # average4py # A program to average a set of numbers # Illustrates sentinel loop using empty string as sentinel def main: sum = 00 count = 0 xstr = raw_inputenter a number <Enter> to quit >> while xstr = : x = evalxstr sum = sum x count = count 1 xstr = raw_inputenter a number <Enter> to quit >> print \nthe average of the numbers is, sum / count Enter a number <Enter> to quit >> 4 Enter a number <Enter> to quit >> 2 Enter a number <Enter> to quit >> 0 Enter a number <Enter> to quit >> 25 Enter a number <Enter> to quit >> 44 Enter a number <Enter> to quit >> 227 Enter a number <Enter> to quit >> The average of the numbers is 8 40 9< # average5py # Computes the average of numbers listed in a file def main: filename = raw_inputwhat file are the numbers in? infile = openfilename,r sum = 00 count = 0 for line in infilereadlines: sum = sum evalline count = count 1 print \nthe average of the numbers is, sum / count

E 016 4readline, readline BC line = infilereadline while line = #process line line = infilereadline *G0 < H * * readline *BIC 6JBC 5 # average6py # Computes the average of numbers listed in a file def main: filename = raw_inputwhat file are the numbers in? infile = openfilename,r sum = 00 count = 0 line = infilereadline while line = : sum = sum evalline count = count 1 line = infilereadline print \nthe average of the numbers is, sum / count 8 **** if 4 * * >A 7 9 8 ** sum = 00 count = 0 line = infilereadline while line = : #update sum and count for values in line line = infilereadline print \nthe average of the numbers is, sum/count 8,* sum count * * * sum 4count 5

8 for xstr in stringsplitline,,: sum = sum evalxstr count = count 1 8for line* 8 # average7py # Computes the average of numbers listed in a file # Works with multiple numbers on a line import string def main: filename = raw_inputwhat file are the numbers in? infile = openfilename,r sum = 00 count = 0 line = infilereadline while line = : for xstr in stringsplitline,,: sum = sum evalxstr count = count 1 line = infilereadline print \nthe average of the numbers is, sum / count 8 while for 4, 8 ** * * * if while,,true alse *G, * >while x >= 0A,, **, 2 2 5

if p1getx == p2getx: if p1gety == p2gety: # points are the same else: # points are different else: # points are different G*0**,6 G0 andornot and or *, <expr> and <expr> <expr> or <expr> 7 9 and *,, *, 4 / / /,,* P and Q or *,*, / / or *, or *,G** BC 7

not, not, 4 0,,, a or not b and c :*< * notandor 2 a or not b and c G& * *and if p1getx == p2getx and p2gety == p1gety: # points are the same else: # points are different * * 5 * 2 :*,< scorea == 15 or scoreb == 15 4,, 4* H0 6 while notscorea == 15 or scoreb == 15: #continue playing 7 9 9

2 *5 while notscorea == 15 or scoreb == 15 or \ scorea == 7 and scoreb == 0 or scoreb == 7 and scorea == 0: #continue playing 0 * * * * # 9 a >= 15 and a b >= 2 or b >= 15 and b a >= 2 a >= 15 or b >= 15 and absa b >= 2 *, 0, * M9J9 MJ L9J JJ JJ JJ and or 0 1 or* a or true == true and or a or b and c == a or b and a or c a and b or c == a and b or a and c notnot a == a E G* nota or b == not a and not b nota and b == not a or not b 4, while notscorea == 15 or scoreb == 15: #continue playing 0B4 C E G* while not scorea == 15 and not scoreb == 15: #continue playing

while scorea = 15 and scoreb = 15 # continue playing G<B4 C * * * not E G* *2, 5 if while, *** 0 7 59 repeat get a number from the user until number is >= 0 4 *, * * while 5 5

4*, number = 1 while number < 0: number = inputenter a positive number: number ;*, break break, break,* 00 5 5 * break while True: number = inputenter a positive number: if x >= 0: break # Exit loop if number is valid *, True *0 06 4, break,* if :6 4G * **< 5 5 while *0* number = 1 while number < 0: number = inputenter a positive number: if number < 0: print The number you entered was not positive 4G 0* 6 * break else while True: number = inputenter a positive number: if x >= 0: break # Exit loop if number is valid else: print The number you entered was not positive 55 5

: * while True: number = inputenter a positive number: if x >= 0: break # Loop exit print The number you entered was not positive :, ** : * while True: get next data item if the item is the sentinel: break process the item 6 57 9 : : break 26 0 0* **,, * 0 * >0 A * while response[0] == y or response[0] == Y: 6H 0 while response[0] == y or Y: 4G*0< bool 9True alse 0== *bool

:** > A& alse True >>> bool0 alse >>> bool1 True >>> bool2 True >>> boolhello True >>> bool alse >>> bool[1,2,] True >>> bool[] alse 2 alse *2 0True 0, and, or not,,, *,, *, True *alse 5, and, *0*, **G,,,, *,* 7 79

* 0* and *, or*,*, response[0] == y or Y * :G2, response[0] == y or Y or,true?9@2bcbhc* 7 7 ** <Enter> ans ans = raw_inputwhat flavor fo you want [vanilla]: if ans: flavor = ans else: flavor = vanilla #<Enter>ans * * 7 7 46 ans = raw_inputwhat flavor fo you want [vanilla]: flavor = ans or vanilla 1* True,* 6 flavor = raw_inputwhat flavor do you want [vanilla]: or vanilla &N0 0 * 6 7 7