Streams Using Python ByTim Walsh. What is a stream? Called a “generator” in Python Similar to a list First made available in Python 2.2, all newer versions.

Slides:



Advertisements
Similar presentations
Why python? Automate processes Batch programming Faster Open source Easy recognition of errors Good for data management What is python? Scripting programming.
Advertisements

PSEUDOCODE & FLOW CHART
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
COMP 4—Power Tools for the Mind1 PowerTools What’s in the Box? Turing 1: An Introduction to Programming You will learn elementary computer programming.
Guide To UNIX Using Linux Third Edition
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
SIMULATION. Simulation Definition of Simulation Simulation Methodology Proposing a New Experiment Considerations When Using Computer Models Types of Simulations.
An Overview of Computers and Programming
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Prototype & Design Computer Inputs. How to Prototype & Design Computer Inputs Step 1: Review Input Requirements Step 2: Select the GUI Controls Step 3:
Fundamentals of Python: From First Programs Through Data Structures
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Using Arrays and File Handling
Python File Handling. In all the programs you have made so far when program is closed all the data is lost, but what if you want to keep the data to use.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 10 - Testing.
Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
Introduction to Computer Programming Using C Session 23 - Review.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Basic Program Construction
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
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.
ELEE 4303 Digital II Introduction to Verilog. ELEE 4303 Digital II Learning Objectives Get familiar with background of HDLs Basic concepts of Verilog.
Programming Logic and Design Seventh Edition
HOW A COMPUTER PROCESSES DATA. What is hardware? Hardware is the electric, electronic and mechanical equipment that makes up a computer What is software?
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
The Development Process Compilation. Compilation - Dr. Craig A. Struble 2 Programming Process Problem Solving Phase We will spend significant time on.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
CIT 590 Intro to Programming Files etc. Agenda Files Try catch except A module to read html off a remote website (only works sometimes)
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Chapter 1: Introduction to Computers and Programming.
Today… Functions, Cont.: –Designing functions. –Functional Decomposition –Importing our own module –A demo: Functional solution to assignment 2. Winter.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
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.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
ME 142 Engineering Computation I
Chapter 2 - Introduction to C Programming
Topics Introduction to Repetition Structures
Programming Fundamental
Functions CIS 40 – Introduction to Programming in Python
Chapter 2 - Introduction to C Programming
For Monday Read WebCT quiz 18.
Files I/O, Streams, I/O Redirection, Reading with fscanf
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Python Primer 2: Functions and Control Flow
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Loops CIS 40 – Introduction to Programming in Python
Coding Concepts (Basics)
Chapter 2 - Introduction to C Programming
Presented By: Darlene Banta
Chapter 2 - Introduction to C Programming
Module 5 ● Vocabulary 1 a sensing block which will ask whatever question is typed into the block and display an input text box at the bottom.
 Is a machine that is able to take information (input), do some work on (process), and to make new information (output) COMPUTER.
Presentation transcript:

Streams Using Python ByTim Walsh

What is a stream? Called a “generator” in Python Similar to a list First made available in Python 2.2, all newer versions make use of this feature

Application of Streams A common application of streams in other languages is for input/output. The sys module is where the standard input, output, and error streams can be found in Python: sys.stdin sys.stdout sys.stderr

Application of Streams Using the input and output streams one can read/write text from/to a file. At the command line: ~\testFile.py < input.txt ~\testFile.py > output.txt On some windows machines you must specify “python before the file name as in: ~\python testFile.py > input.txt

Application of Streams Similarly, python programs can be chained together so that the output from one program is the input to another ~\write.py | read.py Now the programs are connected, read will take the output from write as its input.

Applications of Streams known as ‘generators’ A stream is similar to a list ◦ Allows a user to deal with very large problems without waiting for the entire range of the list to be constructed first A function that contains a yield statement is called a generator function A generator function is able to construct the list as needed (specified by the user)

The Yield Statement Yield is a keyword in Python Yield replaced the required declaration that made generators possible from __future__ import generators (No longer needed, just use yield) Yield can be thought of as a ‘future statement’ and makes generators possible in Python

Applications of Streams known as ‘generators’ A generator is a function which can: ◦ stop whatever it is doing at an arbitrary point (the yield statement) ◦ Return a value back to the function ◦ Resume from the point it had `yielded at' later on in the function  This is accomplished because ‘yield’ automatically will call the.next() function .next() will get the next element from the generator-iterator

Example Problem: ◦ Find the first few prime numbers between a large interval of numbers Conventional methods require a long list of primes to be generated Solution ◦ Using a stream or generator in Python the problem requires very few calculations, focusing only on the pieces of the computation desired