Presentation is loading. Please wait.

Presentation is loading. Please wait.

Math 15 Introduction to Scientific Data Analysis Lecture 8 Python Programming – Part 2 University of California, Merced.

Similar presentations


Presentation on theme: "Math 15 Introduction to Scientific Data Analysis Lecture 8 Python Programming – Part 2 University of California, Merced."— Presentation transcript:

1 Math 15 Introduction to Scientific Data Analysis Lecture 8 Python Programming – Part 2 University of California, Merced

2 UC Merced2 Schedule December 17 6-9pm

3 UC Merced3 Last Week!  First time of Python Programming What did you think?

4 UC Merced4 October, 2007 Bis 180

5 UC Merced5  Any Questions?

6 UC Merced6 Assignment #5 due November 2 nd, 2007! Assignments & their materials!

7 UC Merced7 Click This!

8 UC Merced8 First, Retrieve this file. For submission, first click this to attach your python codes.

9 UC Merced9 Click this to browse your file and attach your python file

10 UC Merced10 Then hit this to continue!

11 UC Merced11 To submit

12 UC Merced12  Any Questions?

13 UC Merced13 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 >>>

14 UC Merced14 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!

15 UC Merced15 Example – Conversion of US$ to 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

16 UC Merced16 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 >>>

17 UC Merced17 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!”

18 UC Merced18  Any Questions?

19 UC Merced19 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 ’ ]

20 UC Merced20 Remember!  The process of learning to program is an excellent opportunity to practice problem- solving skills.

21 UC Merced21 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

22 UC Merced22 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:

23 UC Merced23 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 element1 st element

24 UC Merced24 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

25 UC Merced25 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']

26 UC Merced26 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']

27 UC Merced27  Any Questions?

28 UC Merced28 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 : Syntax The while command first checks if the condition is true or false and then execute the command(s) if true.

29 UC Merced29 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!"

30 UC Merced30 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 : Syntax

31 UC Merced31  Any Questions?

32 UC Merced32 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): Syntax

33 UC Merced33 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

34 UC Merced34 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 3 2 5 3 7 4 9 5 11 6 13 7 Starting Value End Value Increment Counter

35 UC Merced35  Any Questions?

36 UC Merced36  Homework #5 due November 2 nd Already available in UCMCROP Here comes Quiz #4!


Download ppt "Math 15 Introduction to Scientific Data Analysis Lecture 8 Python Programming – Part 2 University of California, Merced."

Similar presentations


Ads by Google