Top Banner
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine
33

CS190/295 Programming in Python for Life Sciences: Lecture 6

Dec 31, 2015

Download

Documents

lane-walter

CS190/295 Programming in Python for Life Sciences: Lecture 6. Instructor: Xiaohui Xie University of California, Irvine. Announcement. Homework #4 will be out this week (will send out emails). Due on Feb 16 (next Thur) Lab session next Tuesday. Control Structures. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS190/295  Programming in Python for Life Sciences: Lecture 6

CS190/295 Programming in Python for Life Sciences: Lecture 6

Instructor: Xiaohui Xie

University of California, Irvine

Page 2: CS190/295  Programming in Python for Life Sciences: Lecture 6

Announcement

• Homework #4 will be out this week (will send out emails). Due on Feb 16 (next Thur)

• Lab session next Tuesday.

Page 3: CS190/295  Programming in Python for Life Sciences: Lecture 6

Control Structures

Part I: Decision structures

Page 4: CS190/295  Programming in Python for Life Sciences: Lecture 6

Multi-way decisions: if-elif-else

• Python will evaluate each condition in turn looking for the first one that is true. If a true condition is found, the statements indented under that condition are executed, and control passes to the next statement after the entire if-elif-else.

• If none of the conditions are true, the statements under the else are performed.

• The else clause is optional; if omitted, it is possible that no indented statement block will be executed.

Page 5: CS190/295  Programming in Python for Life Sciences: Lecture 6

Control Structures

Part 2: Loop Structures

Page 6: CS190/295  Programming in Python for Life Sciences: Lecture 6

For Loops

• It allows us to iterate through a sequence of values

• The loop index variable var takes on each successive value in the sequence, and the statements in the body of the loop are executed once for each value.

Page 7: CS190/295  Programming in Python for Life Sciences: Lecture 6

Indefinite Loops: while-statement

• An indefinite loop keeps iterating until certain conditions are met.

• Here condition is a Boolean expression, just like in if statements. The body is, as usual, a sequence of one or more statements.

• The body of the loop executes repeatedly as long as the condition remains true. When the condition is false, the loop terminates

Page 8: CS190/295  Programming in Python for Life Sciences: Lecture 6

Nested LoopsAgain in the number averaging example, suppose instead of one-per-line we allow any number of values on a line, separated by comma.

Page 9: CS190/295  Programming in Python for Life Sciences: Lecture 6

Post-Test LoopSuppose you are writing an input algorithm that is supposed to get a nonnegative number from the user. If the user types an incorrect input, the program asks for another value. It continues to reprompt until the user entersa valid value. This process is called input validation.

Here is a simple algorithm:repeat get a number from the useruntil number is >= 0

Page 10: CS190/295  Programming in Python for Life Sciences: Lecture 6

Post-Test Loop• Python does not have a statement that directly implements a post-test loop. However you can implemented with a while statement using the trick of

“seeding”:

• Or use the break statement:

Page 11: CS190/295  Programming in Python for Life Sciences: Lecture 6

Data Collections

Page 12: CS190/295  Programming in Python for Life Sciences: Lecture 6

Most real-world programs deal with large collections of data

A few examples:

•Words in a document.•Students in a course.•Data from an experiment.•Customers of a business.•Graphics objects drawn on the screen.•Cards in a deck.

Page 13: CS190/295  Programming in Python for Life Sciences: Lecture 6

Example problem: simple statistics

Extend this program so that it computes not only the mean, but also the median and standard deviation of the data

Page 14: CS190/295  Programming in Python for Life Sciences: Lecture 6

Basic summary statistics

Page 15: CS190/295  Programming in Python for Life Sciences: Lecture 6

Lists

• Lists are ordered sequences of items, a collection of values denoted by the enclosing square brackets

• Lists can be created by listing items inside square brackets

Page 16: CS190/295  Programming in Python for Life Sciences: Lecture 6

Lists vs. Strings

• In Python strings and lists are both sequences that can be indexed. In fact, all of the built-in string operations that we discussed previously are sequence operations and can also be applied to lists:

Page 17: CS190/295  Programming in Python for Life Sciences: Lecture 6

Lists vs. Strings: differences• The items in a list can be any data type, including instances of programmer-defined classes. Strings, obviously, are always

sequences of characters.

• Second, lists are mutable. That means that the contents of a list can be modified. Strings cannot be changed “in place.”

Page 18: CS190/295  Programming in Python for Life Sciences: Lecture 6

Tuples

• A tuple is a sequence of immutable Python objects. Just like lists. The only difference is that types cannot be changed.

• Tuples are defined using parentheses while lists use square brackets.

• Examples:Tup1 = (1,2,3,4)

Tup2 = (‘UCI’, ‘UCLA,’UCSD’)

Page 19: CS190/295  Programming in Python for Life Sciences: Lecture 6

List operations

• Python lists are dynamic. They can grow and shrink on demand. They are also heterogeneous. You can mix arbitrary data types in a single list. In a nutshell, Python lists are mutable sequences of arbitrary objects. This is very different from arrays in other programming languages.

• A list of identical items can be created using the repetition operator.

• Typically, lists are built up one piece at a time using the append method.

Page 20: CS190/295  Programming in Python for Life Sciences: Lecture 6

List operations: remove elements

Page 21: CS190/295  Programming in Python for Life Sciences: Lecture 6

List operations

Page 22: CS190/295  Programming in Python for Life Sciences: Lecture 6

Using lists is easy if you keep these basic principles in mind

• A list is a sequence of items stored as a single object.

• Items in a list can be accessed by indexing, and sublists can be accessed by slicing.

• Lists are mutable; individual items or entire slices can be replaced through assignment statements.

• Lists will grow and shrink as needed.

Page 23: CS190/295  Programming in Python for Life Sciences: Lecture 6

Statistics with Lists

# get a list of numbers and store in a list

Page 24: CS190/295  Programming in Python for Life Sciences: Lecture 6

Statistics with Lists: Mean

Page 25: CS190/295  Programming in Python for Life Sciences: Lecture 6

Statistics with Lists: Standard Deviation

Page 26: CS190/295  Programming in Python for Life Sciences: Lecture 6

Statistics with Lists: Median

Page 27: CS190/295  Programming in Python for Life Sciences: Lecture 6

Simple Statistics: put together

Page 28: CS190/295  Programming in Python for Life Sciences: Lecture 6

Non-sequential collections• Dictionary is a build-in data type for non-sequential collections in Python.

• Python dictionaries are mappings: Keys -> values

• A dictionary can be created by listing key-value pairs inside a curly braces.

• We can access the value associated with a particular key using:

• Dictionaries are mutable:

Page 29: CS190/295  Programming in Python for Life Sciences: Lecture 6

Dictionary Operations

• You can also extend a dictionary by adding new entries

• In fact, a common method for building dictionaries is to start with an empty collection and add the key-value pairs one at a time

Page 30: CS190/295  Programming in Python for Life Sciences: Lecture 6

Dictionary Operations

Page 31: CS190/295  Programming in Python for Life Sciences: Lecture 6

Dictionary methods: examples

Page 32: CS190/295  Programming in Python for Life Sciences: Lecture 6

References and Objects

• Consider the following example:

x = [1,2,3,4]

y = x

y[1] = ‘hello’

>>> y

[1,’hello’,3,4]

>>> x

[1,’hello’,3,4]

Note that items in x are also changed after y assignment.

Page 33: CS190/295  Programming in Python for Life Sciences: Lecture 6

Call by reference in functions

• Consider the following example:

>>> def my_func(x):

x.append(’hello’)

>>> x = [1,2,3,4]

>>> my_func(x)

>>> x

[1,2,3,4,’hello’]