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

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 201 Functions Debzani Deb.
JavaScript, Third Edition
Introduction to C Programming
Chapter 2: Algorithm Discovery and Design
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
The switch Statement, DecimalFormat, and Introduction to Looping
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Introduction to Python
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Input, Output, and Processing
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
CPS120: Introduction to Computer Science Decision Making in Programs.
Built-in Data Structures in Python An Introduction.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Files Tutor: You will need ….
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.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Python Programing: An Introduction to Computer Science
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
String and Lists Dr. José M. Reyes Álamo.
Topics Designing a Program Input, Processing, and Output
Python: Control Structures
Introduction to Scripting
MATLAB: Structures and File I/O
CS190/295 Programming in Python for Life Sciences: Lecture 5
CS190/295 Programming in Python for Life Sciences: Lecture 1
Topics Introduction to File Input and Output
CS190/295 Programming in Python for Life Sciences: Lecture 4
CS190/295 Programming in Python for Life Sciences: Lecture 6
T. Jumana Abu Shmais – AOU - Riyadh
String and Lists Dr. José M. Reyes Álamo.
Python Primer 1: Types and Operators
CS190/295 Programming in Python for Life Sciences: Lecture 3
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Topics Introduction to File Input and Output
Introduction to C Programming
Introduction to Computer Science
Presentation transcript:

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

Announcements Homework assignment #2 is out, due on Jan 31 (Tue) before class Jake will lead a lab session this Thursday (Jan 26). Bring your laptop.

Computing with Strings

The String Data Type A string is a sequence of characters

The String Data Type A string is a sequence of characters Remember that the input statement treats whatever the user types as an expression to be evaluated

Indexing of the String A string is a sequence of characters Individual characters can be accessed through the operation of indexing. The general form for indexing is [ ].

Slicing of the String A string is a sequence of characters Access a contiguous sequence of characters or substring from a string is called slicing. The general form for slicing is [ : ]. Both start and end should be int-valued expressions. A slice produces the substring starting at the position given by start and running up to, but not including, position end.

The string data type is immutable

Operations for putting strings together + concatenation * repetition

Summary of basic string operations

Example: single string processing

String Representation How does a computer represent strings? Each character is translated into a number, and the entire string is stored as a sequence of (binary) numbers in computer memory. It doesn’t really matter what number is used to represent any given character as long as the computer is consistent about the encoding/decoding process. One important standard, called ASCII, uses the numbers 0 through 127 to represent the characters typically found on a computer keyboard. For example, the capital letters A–Z are represented by the values 65–90, and the lowercase versions have codes 97–122. UniCode is an extended standard to include characters of other written languages

The String Library split - This function is used to split a string into a sequence of substrings. By default, it will split the string wherever a space occurs

The String Library

The eval() function eval - This function takes any string and evaluates it as if it were a Python expression.

Converting Numbers to Strings

String Formatting Notice that the final value is given as a fraction with only one decimal place. How to output something like $1.50 ? You can do this by using the string formatting operator:

String Formatting The string formatting operator is used like this: % ( ) % signs inside the template-string mark “slots” into which the values are inserted. There must be exactly one slot for each value. Each of the slots is described by a format specifier that tells Python how the value for that slot should appear. A formatting specifier has this general form: %.  type-char: decimal, float, or string  width: how many spaces are used to display the value? If a value requires more room than is given in width, Python will just expand the width so that the value fits.  precision: used with floating point values to indicate the desired number of digits after the decimal.

String Formatting

Multi-Line Strings Special characters: ’\n’ - newline (as if you are typing key on your keyboard ’\t’ -

File Processing Three Key steps of file-processing in all programming languages: 1.Associate a file on disk with a variable in a program. This process is called opening a file. Once a file has been opened, it is manipulated through the variable we assign to it. 2.Define a set of operations that can manipulate the file variable. At the very least, this includes operations that allow us to read the information from a file and write new information to a file. 3.When we are finished with a file, it is closed. Closing a file makes sure that any bookkeeping that was necessary to maintain the correspondence between the file on disk and the file variable is finished up. (For example, if you write information to a file variable, the changes might not show up on the disk version until the file has been closed.)

File Processing: open a file Associate a file on disk with a variable in a program. This process is called opening a file. = open(, ) mode: “r” for read, “w” for write Example: infile = open(“numbers.data”,”r”) Now we can use the variable inflie to read the contents of numbers.data from the disk.

File Processing: read Once a file is open, Python provides three related operations for reading information from a file:.read() –Returns the entire contents of the file as a single string. If the file contains more than one line of text, the resulting string has embedded newline characters between the lines.readline() –read one line from a file (read all the characters up through the next newline character); the string returned by readline will always end with a newline character.readlines() o returns a sequence of strings representing the lines of the file

File Processing: read

File Processing: write Open a file for output: Outfile = open(“mydata.out”, “w”) A word of warning: if a file with the given name does exist, Python will delete it and create a new, empty file. Put data into a file using the write operation:.write( )

Coming Attraction: Objects Notice the operations of the file processing are in the format of: –infile.read() –infile.close() which are different from the normal function applications such as abs(x) In Python, a file is an example of an object. Objects combine both data and operations together. An object’s operations, called methods, are invoked using the dot notation. Notice that strings are also objects in Python. You can invoke methods of strings: myString.split() Is equivalent to string.split(myString)

Defining Functions

Functions, Informally You can think of a function as a subprogram—a small program inside of a program. The basic idea of a function is that we write a sequence of statements and give that sequence a name. The instructions can then be executed at any point in the program by referring to the function name. The part of the program that creates a function is called a function definition. When a function is subsequently used in a program, we say that the definition is called or invoked. A single function definition may be called at many different points of a program.

Functions – simple example Examples: “Happy Birthday” song. The standard lyrics look like this. Happy birthday to you! Happy birthday, dear. Happy birthday to you!

Functions – simple example

Parameters of a function

A function definition looks like this: A function is called by using its name followed by a list of actual parameters or arguments A function can have several parameters (arguments): The actual parameters are matched up with the formal parameters by position Functions and Parameters def (,…, ) (,…, )

When a function is called When Python comes to a function call, it initiates a four-step process. 1.The calling program suspends at the point of the call. 2.The formal parameters of the function get assigned the values supplied by the actual parameters in the call. 3.The body of the function is executed. 4.Control returns to the point just after where the function was called.

Functions with return values Using return statement:

Functions can return more than one value When calling this function, place it in a simultaneous statement:

Control Structures Part I: Decision structures

Motivation So far, we have viewed computer programs as sequences of instructions that are followed one after the other. Sequencing is a fundamental concept of programming, but alone, it is not sufficient to solve every problem. Often it is necessary to alter the sequential flow of a program to suit the needs of a particular situation. This is done with special statements known as control structures. Decision structures, one type of control structures, are statements that allow a program to execute different sequences of instructions for different cases,

An example: temperature warning Suppose now we want to enhance the program by providing temperature warning like this:

Flowchart of the temperature conversion program with warning

The temperature conversion program with warning decision structure

Simple if-statement Control flow of simple if-statement:

Forming simple conditions Simple conditions that compare the values of two expressions: is short for relational operator. There are six relational operators in Python: Conditions may compare either numbers or strings. When comparing strings, the ordering is lexicographic. Basically, this means that strings are put in alphabetic order according to the underlying ASCII codes. So all upper-case letters come before lower case letters (e.g., “Bbbb” comes before “aaaa”, since “B” precedes “a”).

Boolean expression Conditions are actually a type of expression, called a Boolean expression When a Boolean expression is evaluated, it produces a value of either True (the condition holds) or False (it does not hold).

Boolean operators Python provides three Boolean operators: and, or and not. The Boolean operators and and or are used to combine two Boolean expressions and produce a Boolean result. and or –The and of two expressions is true exactly when both of the expressions are true. –The or of two expressions is true when either expression is true The not operator computes the opposite of a Boolean expression. It is a unary operator, meaning that it operates on a single expression.

Precedence of Boolean operators Build arbitrarily complex Boolean expressions using Boolean operators Consider this expression: a or not b and c Python follows a standard convention that the order of precedence is: not, followed by and, followed by or. So the expression would be equivalent to this parenthesized version. (a or ((not b) and c)) I suggest that you always parenthesize your complex expressions to prevent confusion.

Two-way decisions: if-else statement When the Python interpreter encounters this structure, it will first evaluate the condition. If the condition is true, the statements under the if are executed. If the condition is false, the statements under the else are executed. In either case, control then passes to the statement following the if-else

Two-way decisions: example

Two-way decisions: an example

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.

For Loop example: computer the average of a series of numbers

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

Indefinite loop example: computing the average of a series of numbers

Another implementation:

File Loops One disadvantage of all the averaging programs presented so far is that they are interactive. A better approach to the problem is to type all of the numbers into a file. The data in the file can be perused and edited before sending it to a program that generates a report.

File Loops One potential problem with readlines() is that the entire contents of the file are first read into main memory. With very large data files, it is better to read and process small sections of the file at a time. In the case of text files, a simple approach is to process the file one line at a time The readline method gets the next line from a file as a string. When we come to the end of the file, readline returns an empty string.

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

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

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: exammples