Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 217 T03 Week IX Part #1: Function, Iteration and List Hubert (Sathaporn) Hu.

Similar presentations


Presentation on theme: "CPSC 217 T03 Week IX Part #1: Function, Iteration and List Hubert (Sathaporn) Hu."— Presentation transcript:

1 CPSC 217 T03 Week IX Part #1: Function, Iteration and List Hubert (Sathaporn) Hu

2 Today’s Tutorial TIPS For-loop for a list While-loop for list Slice notation Function that return a list List nesting Tracing Better Boggle

3 TIPS 1.Make sure that the style of the code is good. Look at the starter code for example. 2.You can attempt to run the starter code even if you have implemented nothing or if you haven’t finished to implement the function. The game will fail or will play perfectly, but it will tell you where it fails. 3.Play a bit of Minesweeper to get the hang of the game.

4 For-loop for list You can substitute range with a list in a for- loop. Something interesting happens: for p in [“Rachel”, “Brian”, “Ric”]: print(“Welcome,”, p) a = [1, 2, 3, 4, 5] for x in a: print(x ** 0.5) EXERCISE: Figure what does the code above do?

5 For-loop for list However, you can never use for-loop to modify a list though. So this code will not change anything: a = [1, 2, 3, 4, 5] for x in a: x = x ** 0.5 Use while-loop if you want to modify something.

6 While-loop for list You can use while-loop to do what for-loop can and more – like modifying a list. Use the index variable to interact with the list elements. a = [1, 2, 3, 4, 5] i = 0 while i < len(a): a[i] = a[i] ** 0.5 print(a[i]) i = i + 1

7 While-loop for list Notice len. len is a function that find the length of a list. To iterate through all of the list, we need to know the length. Also beware that list starts from 0 and ends at the length of the list minus 1. So a[len(a) – 1] will get you the last item in a. a[len(a)] will just crash the program.

8 Slice notation Python has something to make your life easier when dealing with list. It’s called sliced notation, and it allows to grab a section of a list. For example, if we have b = a[10:20], b will be a list whose elements are from the 10 th elements to the 19 th from b. You can also have [:10] or [10:]. [:10] means 0 th elements to 10 and [10:] means 10 th element until the last one.

9 Slice notation Use negative index to access the elements from the last index.  a[-1]: accessing the last element  a[-2]: accessing the second last element You can also use variables in the notation: a = [10, 20, 30, 40] x = 1 y = 3 print(a[x:y])

10 Function that returns a list Did you know that a function can also returns a list? So if your assignment asks you to return a list, don’t hesitate.

11 List Nesting Now a major part of the tutorial is to show how to do a loop on a 2D list. It turns out that we can use a nested loop for it. For instance, if we want to print all of the element in a 2D list, we can 2 nested loop. EXERCISE: If we have 2D list A, how can we print everything in it?

12 List Nesting Here is an example code for a function that multiplies a 2D list with a number: function scalar_mult(x, 2d_list): ret_list = [] i = 0 j = 0 while i < len(2d_list): ret_list.append([]) while j < len(2d_list[i]): ret_list[i].append(x * 2d_list[i][j]) j += 1 i += 1 return ret_list

13 List Nesting Here is another example of adding 2 lists together: function 2d_list_add(a, b): ret_list = [] i = 0 j = 0 while i < len(a): ret_list.append([]) while j < len(b): ret_list[i].append(a[i][j] + b[i][j]) j += 1 i += 1 return ret_list

14 Tracing Better Boggle Now, we are going to trace Better Boggle

15 Next Tutorial Next tutorial is a work period, so I won’t be presenting anything.


Download ppt "CPSC 217 T03 Week IX Part #1: Function, Iteration and List Hubert (Sathaporn) Hu."

Similar presentations


Ads by Google