Python – a HowTo Peter Wad Sackett and Henrike Zschach.

Slides:



Advertisements
Similar presentations
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
Advertisements

Lists Introduction to Computing Science and Programming I.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
Chapter 5: Loops and Files.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Group practice in problem design and problem solving
Automation Testing- QTP Rajesh Charles Batch No: Date: jan
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Programming for Linguists An Introduction to Python 24/11/2011.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
CPS120: Introduction to Computer Science Decision Making in Programs.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Python Simple file reading Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Simple Pythonic file reading Python has a special.
Python Syntax tips Henrike Zschach. 2DTU Systems Biology, Technical University of Denmark Why are we talking about syntax ’Good’ coding Good syntax should.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Automation Testing- QTP Rajesh Charles Batch No: Date: jan
Lab 4: Loops and Iteration Graham Northup
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
How to python source: Web_Dev fan on pinterest.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Introduction to Python
REPETITION CONTROL STRUCTURE
Chapter 4 C Program Control Part I
CS161 Introduction to Computer Science
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
Department of Computer Science,
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Scripts & Functions Scripts and functions are contained in .m-files
Engineering Innovation Center
Exceptions and files Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CHAPTER FOUR Functions.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Arrays, For loop While loop Do while loop
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Introduction to Programming
Exceptions and files Taken from notes by Dr. Neil Moore
If selection construct
Coding Concepts (Basics)
Repetition Structures
If selection construct
Coding Concepts (Data- Types)
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Introduction to Python
Introduction to Python
Python I/O Peter Wad Sackett.
Introduction to Computer Science
Java LESSON 3 Loops.
“Everything Else”.
Class code for pythonroom.com cchsp2cs
Python Simple file reading
Introduction to Computer Science
Presentation transcript:

Python – a HowTo Peter Wad Sackett and Henrike Zschach

Introduction While evaluating the exercises, we have noticed some examples of sub-optimal programming. While most of those constructs are not technically wrong or will lead to errors, they are not good programming! Therefore we’ll show some examples of how people tend to go wrong and how it can be made right. Motto: Making code better Thank you, wikipedia

The use of flow control break and continue are so-called flow control tools that can be very useful, but they can also be used to be a bit lazy precisely because they give control over the flow of the program. Excessive use of flow control can be a sign that your loops are poorly designed and your logic flawed. When programming, you should strive for code that makes the most sense. How long does this loop have to run? What is the step size? What are the entry and exit conditions? Letting a loop run further even though there is no more need is not stronger logic than terminating! looking_for = input(“What are you looking for?\n”) found = False for item in myList: if item == looking_for: found = True break for loops can only terminate ”before time” by using break.

Flow control and while Using break in while loops is almost always weak logic, because while loops are already condition-dependent. looking_for = input(“What are you looking for?\n”) found = False i = 0 while i < len(myList): if myList[i] == looking_for: found = True break i += 1 Write instead while i < len(myList) and myList[i] != looking_for : The logic is to stop stop looking (incrementing i) when you have seen the entire list or found the item.

Flow of programming You should separate actions that belong inside a loop (f.x. data extraction) from actions that belong outside the loop because they only need to run once (f.x. data processing, setting up constant variables). Do not write for line in infile: if not line.startswith(‘>’): seq += line.rstrip() seq_len += len(seq) transTable = str.maketrans(’ATCG’,’TAGC’) complementdna = seq.translate(transTable) Write instead seq_len = len(seq)

Conditional statements This one is a bit hard to exemplify but basically think about whether the conditions you pose are sensible. # You would not be in the loop if the word was STOP # No need to check an extra time while word != "STOP”: if word != "STOP": # If line is “this” then it is not “that” if line == that: if line == this: Sometimes the same condition signifies several actions to be taken. for line in infile: if line.startswith(“SQ”): # Code here to extract length # Ups, I also need to extract the whole sequence seq_flag = True Join the actions together under one if.

List building Task: I have some info in this line that I want to save in a list -> Many build-in functions actually return lists, e.g. split(). # don’t data_list = list() for item in line.split(): data_list.append(item) # instead data_list = line.split() Also, our favorite sys.argv is already a list. Take advantage of slicing. # don't input_files = list() for index in range(1, len(sys.argv)): input_files.append(sys.argv[index]) # instead input_files = sys.argv[1:] .

Regular expressions - warning Some people, when confronted with a problem, think: "I know, I'll use regular expressions.” Now they have two problems. - Jamie Zawinski While regex are a very useful and powerful concept, for many problems there exist simpler solutions. Regex in python are slow compared to ’simple’ functions because they have huge overhead. They are also often harder to follow and hard to get right without missing rare cases.

Regular expressions – alternatives Examples of tasks for which functions from the base library are preferable: - Splitting strings on elementary separators (space/tab/semicolon/comma) - Extracting information from such a line split_line = line.split() split_line = line.split(’;’) ID = line.split()[1] - Checking for identifiers if line.startswith(‘ID’): if line.endwith(‘)’):

Regular expressions – proper use Because the results of regex commands are typically small it is more sensible to store the result for future tasks than to re-execute the command: # Don’t if re.search(r'^ID\s+(\S+)', line) is not None: sp_id = re.search(r'^ID\s+(\S+)', line).group(1) # Instead REresult = re.search(r'^ID\s+(\S+)', line) if REresult is not None: sp_id = REresult.group(1)

Variable names This should be obvious. You (and we) will have a much easier time if you name your variables in a way that the name is descriptive of what the variable does or stores is distinct from other variable names is not a reserved keyword Please don’t: def_regex = re.compile("^ID\s+(\w+)”) dee_regex = re.compile("^AC\s+(\w+)”) des_regex = re.compile("^SQ\s+(\w+)”) Or: for char in range(len(line)): base = line[char] # char is in no way a character!!!

Indexing Pay close attention when indexing strings. Example: sequence: A T G T T G A G A T A G human: 1 2 3 4 5 6 7 8 9 10 11 12 machine: 0 1 2 3 4 5 6 7 8 9 10 11 exon: CDS join(1..12) Consider: Where is the first codon position-wise? What will happen with this line: print(seq[0:2]) What is seq[1:12] ? Which index do you actually need to extract the full exon?

Output verification Unfortunately we see programs that almost work too often. The reason is the output has not been checked thoroughly. When extracting information, do a one-on-one check for at least one example. ‘Looks ok’ is not sufficient. It still looks allright if you are not extracting the last or the first base of every sequence because your indexing is wrong! What does your program do if you give different legal input (f.x. a different genbank file)? What does it do if you give illegal input (f.x. your grade sheet from last semester)? You should also get used to making ‘sanity checks’, i.e. is my result sensible, is it what I expected? Integrate your knowledge of what you know or assume to be true, i.e. exons start with ATG and end with a stop codon.

Happy Programming 