Python Simple file reading Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Simple Pythonic file reading Python has a special.

Slides:



Advertisements
Similar presentations
What type of data can a variable hold?
Advertisements

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?
While loops.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
4. Python - Basic Operators
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
CIS Computer Programming Logic
Python Programming Using Variables and input. Objectives We’re learning to make use of if statements to enable code to ask questions. Outcomes Build an.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
Logic Structure - focus on looping Please use speaker notes for additional information!
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Xin Liu Feb 4, * Use midterm questions for previous 231/217 midterms for practices * ams
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Control Structures RepetitionorIterationorLooping Part I.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
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.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Python Exceptions and bug handling Peter Wad Sackett.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
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.
Python Basics Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Reasons to learn Python 1.Short development time 2.Learning curve.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Learning Javascript From Mr Saem
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
Python I/O Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Classic file reading 1 infile = open(’filename.txt’, ’r’) for line.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Python Pattern Matching and Regular Expressions Peter Wad Sackett.
Python Lists and Sequences Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark List properties What are lists? A list is a mutable.
Python Advanced Data Structures Peter Wad Sackett.
Python Syntax tips Henrike Zschach. 2DTU Systems Biology, Technical University of Denmark Why are we talking about syntax ’Good’ coding Good syntax should.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
More about Iteration Victor Norman CS104. Reading Quiz.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Introduction To Repetition The for loop
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CS 115 Lecture 8 Structured Programming; for loops
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Python Stateful Parsing
Python – a HowTo Peter Wad Sackett and Henrike Zschach.
CISC101 Reminders All assignments are now posted.
For loops Taken from notes by Dr. Neil Moore
Python I/O Peter Wad Sackett.
Introduction to Computer Science
Simple Branches and Loops
Python While Loops.
Python Simple file reading
Presentation transcript:

Python Simple file reading Peter Wad Sackett

2DTU Systems Biology, Technical University of Denmark Simple Pythonic file reading Python has a special construct for reading files in a simple manner. with open(’filename.txt’, ’r’) as infile: for line in infile: print(line) # doing something with the line # more python stuff can be done here print(”Now done with the file”) open opens the file ’filename.txt’ for reading; ’r’. The associated filehandle is here called infile, but can be anything. The for loop iterates through each line in the file. The variable is called line (appropiate), but can be anything. When done with the with statement, the file is automatically closed. Using the with statement

3DTU Systems Biology, Technical University of Denmark Questions to ask (yourself) when working with files Questions Did I read any numbers/lines? What happens if the input is different from the expected? Can I make input that will break my program?

4DTU Systems Biology, Technical University of Denmark Some answers Any variable can be assigned the value None which is different from zero (0). myvar = None if myvar is None: print(’Buuh, nothing’) if myvar is not None: print(’Yeah, something’) You test for identity by using the key words is (and is not). var1 = ’I am a string today’ var2 = ’I am a string today’ if var1 == var2:# This is true if var1 is var2:# This is false Due to an internal python quirk short strings and numbers reside the same place in memory and can trigger a true identity check.

5DTU Systems Biology, Technical University of Denmark Loop control When inside a loop you can exit it early with break. for i in range(10): if i == 5: break print(i) You can also go to the next iteration of the loop with continue. for i in range(10): if i == 5: continue print(i) This works with both while and for loops and you can have any number of break’s and continue’s in the loop. Generelly, break and continue should not be overused, as they lead to weaker logical thinking. Something beginners are especially prone to do. Yes, that means you.

6DTU Systems Biology, Technical University of Denmark Empty statement At any point where you can write a python statement, you can always use the empty statement: pass pass if var1 == var2: pass This statement does nothing and takes no time to execute. When the python syntax requires a statement and you don’t wish to ”do” anything, then you pass.