math 15 introduction to scientific data analysis lecture 8 python programming – part 2 university...
Post on 05-Jan-2016
214 Views
Preview:
TRANSCRIPT
Math 15Introduction to Scientific Data Analysis
Lecture 8Python Programming – Part 2
University of California, Merced
UC Merced 2
Schedule
Final ExaminationDecember ?Final
Project #2Python Programming - #6December 515
Python Programming – #5November 2814
Quiz #6Python Programming – #4November 2113
No LectureNovember 1412
Quiz #5Python Programming – #2November 711
Python Programming – #1October 3110
Quiz #4Programming – Introduction to Python Part - IIOctober 249
Project #1Programming – Introduction to Python Part - IOctober 178
Quiz #3Excel #6 – Interactive ProgrammingOctober 107
Excel #5 – Data AnalysisOctober 36
Quiz #2Excel #4 – Regression AnalysisSeptember 265
Excel #3 – Statistical AnalysisSeptember 194
Quiz #1Excel #2 – Plotting Graphs/ChartsSeptember 123
Excel #1 – General TechniquesSeptember 52
Introduction to the data analysisAugust 291
Project DueConceptsDateWeek
Final ExaminationDecember ?Final
Project #2Python Programming - #6December 515
Python Programming – #5November 2814
Quiz #6Python Programming – #4November 2113
No LectureNovember 1412
Quiz #5Python Programming – #2November 711
Python Programming – #1October 3110
Quiz #4Programming – Introduction to Python Part - IIOctober 249
Project #1Programming – Introduction to Python Part - IOctober 178
Quiz #3Excel #6 – Interactive ProgrammingOctober 107
Excel #5 – Data AnalysisOctober 36
Quiz #2Excel #4 – Regression AnalysisSeptember 265
Excel #3 – Statistical AnalysisSeptember 194
Quiz #1Excel #2 – Plotting Graphs/ChartsSeptember 123
Excel #1 – General TechniquesSeptember 52
Introduction to the data analysisAugust 291
Project DueConceptsDateWeek
December 176-9pm
UC Merced 3
Last Week!
First time of Python Programming
What did you think?
UC Merced 4
October, 2007
Bis 180
UC Merced 5
Any Questions?
UC Merced 6
Assignment #5 due November 2nd, 2007!
Assignments & their materials!
UC Merced 7Click This!
UC Merced 8
First, Retrieve this file.
For submission, first click this to attach your python codes.
UC Merced 9
Click this to browse your file and attach your python file
UC Merced 10
Then hit this to continue!
UC Merced 11
To submit
UC Merced 12
Any Questions?
UC Merced 13
Reviews from 1st Programming Experience
Case sensitive Name1 = raw_input(“….. name1 = raw_input(“…..
If computer is asking questions, you need to respond!
name1 = raw_input("What is your name?")print "Hello, " + name1Python Program
IDLE 1.2.1 ==== No Subprocess ====>>> What is your name? MasaHello, Masa>>>
UC Merced 14
What is a computer programming?
A computer programming is a sequence of instructions that specifies how to perform a computation.
Basically, all computers need someone to tell them what to do!
UC Merced 15
Example – Conversion of US$ to Can$
$6.0$ CanUS 6.0$$ USCan
# # This program converts from US $ to Canadian $
us_money = input ("Money value in US $ ")
can_money = us_money /0.6
print "US$", us_money, " = Canadian $", can_money
IDLE 1.2>>> Money value in US $ 2.5US$ 2.5 = Canadian $ 4.16666666667>>>
Execute (F5 of Editing window)
us_money = 2.5
Variables
UC Merced 16
Python 2.5.1Type "copyright", "credits" or "license()" for more information. >>> x = 2>>> x2>>> y = 1>>> y1>>> x + y3>>> y = x>>> x + y4>>>
UC Merced 17
3 mistakes in this program.
# if statement
answer = raw_input("Do you like Mathematics? ")if answer == "yes": print "That is great!"else: print "That is disappointing!”
UC Merced 18
Any Questions?
UC Merced 19
Next Week
More Programming
while statement General loop
for loop statement A sequence iteration
List Mutable (changeable) arrays of object
reference: i.e. [0,1,2,3] [‘Lions’,”Tigers’,’Bears’,’Oh my’]
UC Merced 20
Remember!
The process of learning to program is an excellent opportunity to practice problem-solving skills.
UC Merced 21
Lists
A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements.
Examples:
var1 = [10, 20, 30, 40]
var2 = ["spam", "bungee", "swallow"]
var3 = ["hello", 2.0, 5, “UC Merced”]
a list of four integers. a list of three strings.
a list of mixing value types
UC Merced 22
List operations
>>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> c = a + b >>> print c [1, 2, 3, 4, 5, 6]
The + operator concatenates lists:
UC Merced 23
List operations – cont.
>>> list = ['a', 'b', 'c', 'd', 'e', 'f']
>>> list[:4] [‘a’, 'b', 'c‘, ‘d’]
>>> list[3:]
['d', 'e', 'f']
>>> list[1:3]
['b', 'c']
The slice operations:
Up to 4th element
From 3rd element on
0th element 1st element
UC Merced 24
List operations – cont.
>>> fruit = ["banana", "apple", "quince"] >>> fruit[0] = "pear"
>>> fruit[-1] = "orange"
>>> print fruit
['pear', 'apple', 'orange']
lists are mutable, which means we can change their elements.
0th Element
Last Element
UC Merced 25
List operations – cont.
List deletion del removes an element from a list:
>>> a = ['one', 'two', 'three']
>>> del a[1]
>>> a
['one', 'three']
You can use a slice as an index for del: >>> list = ['a', 'b', 'c', 'd', 'e', 'f']
>>> del list[1:5]
>>> print list
['a', 'f']
UC Merced 26
List operations – cont.
Append & Remove Syntax- list_name.append() & list_name.remove()
Example:ucm = [“shrimp", “hare", "eagle", “bobcat"]
print ucm
ucm.append("dog")
print "The zoo has the following animals:", ucm
ucm.remove("eagle")
print "The zoo has the following animals:", ucm
>>> [‘shrimp', ‘hare', 'eagle', ‘bobcat']The zoo has the following animals: [‘shrimp', ‘hare', 'eagle', ‘bobcat', 'dog']The zoo has the following animals: [‘shrimp', ‘hare', ‘bobcat', 'dog']
UC Merced 27
Any Questions?
UC Merced 28
while statement
Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that computers do well and people do poorly.
The first feature we are going to look at is the while statement. The while statement continuously performs an
action while a condition is true.
while <condition>:<commands>
Syntax
The while command first checks if the condition is true or false and then execute the command(s) if true.
UC Merced 29
while statement – cont.
Example
N = 10 # Set the initial# value of N
while N > 0: print N N = N-1print "Blastoff!"
>>> 10987654321Blastoff!This program reads:
"While N is greater than 0, continue displaying the value of N and then reducing the value of N by 1. When you get to 0, display the word Blastoff!"
UC Merced 30
while statement – cont.
Here is the flow of execution for a while statement: 1. Evaluate the condition, yielding false or true.2. If the condition is true, execute each of the
commands in the body and then go back to 1.
3. If the condition is false, exit the while statement and continue execution at the next statement.
while <condition>:<commands>
<next statements>
Syntax
UC Merced 31
Any Questions?
UC Merced 32
for loop statement
Another operation for repetitive tasks is for loop statement.
Each time through the loop, the next character in the string or next value in the value list is assigned to the variable, jj. The loop continues until no characters or values are left.
for jj in (list):<commands>
Syntax
UC Merced 33
for loop statement – cont.
fruit = ["banana","apple","orange"]for masa in fruit: print masa
Examples>>> bananaappleorange
N = 10for var0 in range(N): print var0
>>>0123456789
UC Merced 34
for loop statement – cont.
N = 10for var in range(1,N,2): print var
Examples>>> 13579
N = 15icount = 0 # Intializing a variablefor var in range(1,N,2): icount = icount + 1 print var, icount
>>> 1 13 25 37 49 511 613 7
Starting Value
End Value
Increment
Counter
UC Merced 35
Any Questions?
UC Merced 36
Homework #5 due November 2nd
Already available in UCMCROP
Here comes Quiz #4!
top related