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

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Lists Introduction to Computing Science and Programming I.
Chapter 5: Control Structures II (Repetition)
Objectives You should be able to describe:
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Introduction to JavaScript for Python Programmers
The switch Statement, DecimalFormat, and Introduction to Looping
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Fundamentals of Python: From First Programs Through Data Structures
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Lists in Python.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
CPS120 Introduction to Computer Science Iteration (Looping)
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS190/295 Programming in Python for Life Sciences: Lecture 4 Instructor: Xiaohui Xie University of California, Irvine.
Built-in Data Structures in Python An Introduction.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CPS120 Introduction to Computer Science Iteration (Looping)
Xiaojuan Cai Computational Thinking 1 Lecture 8 Loop Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
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.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
String and Lists Dr. José M. Reyes Álamo.
REPETITION CONTROL STRUCTURE
Python: Control Structures
Lecture 10 Data Collections
JavaScript: Control Statements.
CS190/295 Programming in Python for Life Sciences: Lecture 5
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Python Primer 2: Functions and Control Flow
Bryan Burlingame 03 October 2018
CS190/295 Programming in Python for Life Sciences: Lecture 4
CS190/295 Programming in Python for Life Sciences: Lecture 6
4. sequence data type Rocky K. C. Chang 16 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
15-110: Principles of Computing
CHAPTER 4: Lists, Tuples and Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Dictionary.
Introduction to Computer Science
Presentation transcript:

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 s). Due on Feb 16 (next Thur) Lab session next Tuesday.

Control Structures Part I: Decision structures

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.

Control Structures Part 2: Loop Structures

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.

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

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

Post-Test Loop Suppose 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 enters a valid value. This process is called input validation. Here is a simple algorithm: repeat get a number from the user until number is >= 0

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:

Data Collections

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.

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

Basic summary statistics

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

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:

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.”

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’)

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.

List operations: remove elements

List operations

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.

Statistics with Lists # get a list of numbers and store in a list

Statistics with Lists: Mean

Statistics with Lists: Standard Deviation

Statistics with Lists: Median

Simple Statistics: put together

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:

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

Dictionary Operations

Dictionary methods: examples

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.

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’]