list - University of California, Merced

foremanyellowSoftware and s/w Development

Nov 7, 2013 (4 years and 3 days ago)

86 views

Math 15

Introduction to

Scientific Data Analysis

Lecture 8

Python Programming


Part 2


University of California, Merced

UC Merced

2

Schedule

Final Examination
December ?
Final
Project #2
Python Programming
-
#6
December 5
15
Python Programming

#5
November 28
14
Quiz #6
Python Programming

#4
November 21
13
No Lecture
November 14
12
Quiz #5
Python Programming

#2
November 7
11
Python Programming

#1
October 31
10
Quiz #4
Programming

Introduction to Python Part
-
II
October 24
9
Project #1
Programming

Introduction to Python Part
-
I
October 17
8
Quiz #3
Excel #6

Interactive Programming
October 10
7
Excel #5

Data Analysis
October 3
6
Quiz #2
Excel #4

Regression Analysis
September 26
5
Excel #3

Statistical Analysis
September 19
4
Quiz #1
Excel #2

Plotting Graphs/Charts
September 12
3
Excel #1

General Techniques
September 5
2
Introduction to the data analysis
August 29
1
Project Due
Concepts
Date
Week
Final Examination
December ?
Final
Project #2
Python Programming
-
#6
December 5
15
Python Programming

#5
November 28
14
Quiz #6
Python Programming

#4
November 21
13
No Lecture
November 14
12
Quiz #5
Python Programming

#2
November 7
11
Python Programming

#1
October 31
10
Quiz #4
Programming

Introduction to Python Part
-
II
October 24
9
Project #1
Programming

Introduction to Python Part
-
I
October 17
8
Quiz #3
Excel #6

Interactive Programming
October 10
7
Excel #5

Data Analysis
October 3
6
Quiz #2
Excel #4

Regression Analysis
September 26
5
Excel #3

Statistical Analysis
September 19
4
Quiz #1
Excel #2

Plotting Graphs/Charts
September 12
3
Excel #1

General Techniques
September 5
2
Introduction to the data analysis
August 29
1
Project Due
Concepts
Date
Week
December 17

6
-
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 2
nd
, 2007!

Assignments & their materials!

UC Merced

7

Click 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 1
st

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, " + name1

Python Program

IDLE 1.2.1 ==== No Subprocess ====

>>>

What is your name?

Masa

Hello, 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
$
Can
US


6
.
0
$
$
US
Can

#

# 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.5

US$ 2.5 = Canadian $ 4.16666666667

>>>

Execute (F5 of Editing window)

us_money = 2.5

Variables

UC Merced

16

Python 2.5.1

Type "copyright", "credits" or "license()" for more information.

>>> x = 2

>>> x

2

>>> y = 1

>>> y

1

>>> x + y

3

>>> y = x

>>> x + y

4

>>>

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 4
th

element

From 3
rd

element on

0
th

element

1
st

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.






0
th

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
-
1

print "Blastoff!"


>>>

10

9

8

7

6

5

4

3

2

1

Blastoff!

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

>>>

banana

apple

orange

N = 10

for var0 in range(N):


print var0

>>>

0

1

2

3

4

5

6

7

8

9

UC Merced

34

for loop

statement


cont.

N = 10

for var in range(1,N,2):


print var


Examples

>>>

1

3

5

7

9

N = 15

icount = 0 # Intializing a variable

for var in range(1,N,2):


icount = icount + 1


print var, icount

>>>

1 1

3 2

5 3

7 4

9 5

11 6

13 7

Starting Value

End Value

Increment

Counter

UC Merced

35


Any Questions?

UC Merced

36


Homework #5 due November 2
nd


Already available in UCMCROP


Here comes Quiz #4!