Working on exercises (a few notes first). Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.

Slides:



Advertisements
Similar presentations
Python November 14, Unit 7. Python Hello world, in class.
Advertisements

 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
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.
Recitation 1 Programming for Engineers in Python.
Fundamentals of Python: From First Programs Through Data Structures
28/08/2015SJF L31 F21SF Software Engineering Foundations ASSUMPTIONS AND TESTING Monica Farrow EM G30 Material available on Vision.
Python programs How can I run a program? Input and output.
Python Programming Fundamentals
“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?
An Introduction to Textual Programming
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
Introduction to Python
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Sorting and Modules. Sorting Lists have a sort method >>> L1 = ["this", "is", "a", "list", "of", "words"] >>> print L1 ['this', 'is', 'a', 'list', 'of',
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
Computer Science 101 Introduction to Programming.
Lists and the ‘ for ’ loop. Lists Lists are an ordered collection of objects >>> data = [] >>> print data [] >>> data.append("Hello!") >>> print data.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Variables, Expressions, and Statements
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Fractions in Binary.
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
Working on exercises (a few notes first)‏. Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
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.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
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.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Thinking about programming Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.
Day 22, Slide 1 CSE 103 Day 22 Non-students: Please logout by 10:12. Students:
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Learning to use a ‘For Loop’ and a ‘Variable’. Learning Objective To use a ‘For’ loop to build shapes within your program Use a variable to detect input.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Few More Math Operators
C++ Memory Management – Homework Exercises
Whatcha doin'? Aims: To start using Python. To understand loops.
Introduction to Programming
BIT116: Scripting Loops.
Data Types and Conversions, Input from the Keyboard
(optional - but then again, all of these are optional)
Python’s input and output
Presented By S.Yamuna AP/IT
CSC 108H: Introduction to Computer Programming
Ruth Anderson UW CSE 160 Spring 2018
Coding Concepts (Basics)
Introduction to Programming
Python programming exercise
CHAPTER 3: String And Numeric Data In Python
Functions continued.
Introduction to Computer Science
“Everything Else”.
Introduction to Programming
Hardware is… Software is…
Presentation transcript:

Working on exercises (a few notes first)

Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores everything from a # to the end of the line. Feel free to write anything you want. # This is ignored by Python. print "Hello, Cape Town" # And so is this. # n = 0 # for line in open("filename"): # print n, line # n = n + 1

IDLE helps out To comment many lines: Select some lines then choose “Format” from the pull-down menu. Choose “Comment Out Region” from the list that appears. To uncomment many lines: Use “Format” -> “Uncomment Region” To move lines to the right: Use “Format” -> “Indent Region” To move lines to the left, “Dedent Region”

Python and division Python integers are “closed” under division That’s a special way of saying that an integer divided by another integer using Python will always return an integer. “integers” are a special way of saying “whole numbers,” that is, numbers without a fraction. Mathematicians have lots of special names! (And so do biologists. And programmers.☺)

Python rounds down When the result is a fraction, Python rounds it down to the next smallest integer >>> 20 / 10 2 >>> 15 / 10 1 >>> 10 / 10 1 >>> 9 / 10 0 >>>

How to fix it The author of Python now says this behavior was a mistake. It should work like people expect. Instead, you need to convert one of the integers into a floating point number >>> 15 / float(10) 1.5 >>>

More examples >>> 20 / float(10) 2.0 >>> 15 / float(10) 1.5 >>> 10 / float(10) 1.0 >>> 9 / float(10) >>>

Why do you need to know about this? Yesterday’s assignment asked you to find all sequences with more than 50% GC content >>> G = 120 >>> C = 33 >>> length = 400 >>> (G + C) / length 0 >>> (G + C) / float(length) >>>

Exercise 6 Look again at sequences.seq from yesterday. Did your program assume only A, T, C, and G? For this exercise, count the number of sequences in that file which have some other letter. What might those mean? How does it affect your %GC calculations?

Exercise 7 What are the extra letters? Write a program to list which letters in the data file sequences.seq are not A, T, C, or G. It should only list each letter once.

Hint for Exercise 7 # Start with a list of unknown letters. # (This is empty because at the start there are # no unknown letters.) unknown_letters = [] for each sequence in the data file: for each letter in the sequence: if letter not in “ATCG”: # it isn’t an A, T, C, or G: if letter not in unknown_letters: # it isn’t in the list of unknown letters append it to the list of unknown letters print the list of unique letters

Exercise 8 Search by molecular weight From experiments you believe your DNA sequence has a molecular weight between and You think it might be in the database from yesterday, sequences.seq. For each sequence in that file which have a molecular weight in that range, print the molecular weight and the sequence. You might need the data table on the next page.

Molecular weights A = C = B = D = G = H = K = M = N = S = R = T = W = V = Y = X = And the molecular weight of water is (Why did I give you that?) I put a copy of these weights in the file /usr/coursehome/dalke/weights.txt

Exercise 9 Verify that your program actually works. How? Here are some possibilities: Compare your results with others’. Create a small sequence file by hand where you know the weight. Compare your manual calculations to what your program found. Find a web site which does this. Compare. Find a published table of weights. Compare.....

Exercise 10 Modify your program from Exercise 8 to ask the user for the filename to use, the lower weight, and the upper weight. You will need the float function to convert the string from raw_input into a number. Redo exercises 8 and 9 with the file /usr/coursehome/dalke/many_sequences.seq Your program must get the filename and ranges from the user via raw_input and not by modifying your program.

Exercise 11 If you finish all the previous assignments: Use the many_sequences.seq file as the input file for the programs you wrote yesterday. Help other people learn Python.