Presentation is loading. Please wait.

Presentation is loading. Please wait.

Last Week Doctests Lists List methods Nested Lists Aliasing.

Similar presentations


Presentation on theme: "Last Week Doctests Lists List methods Nested Lists Aliasing."— Presentation transcript:

1 Last Week Doctests Lists List methods Nested Lists Aliasing

2 This Week Looping though lists Looping using range While loops

3 Visiting the Items in a List L Printing out the list English: for each item in L print the item Python: for item in L: print(item)

4 For Loops -- Revisited Want to print the numbers from 0-100. for num in [0, 1, 2, 3, 4, …, 99, 100]: print(num) Is there an easier way to do this? for num in range(0, 101): print(num) range(start, stop[,step]) returns a list of integers beginning with start to the last integer before stop. start can be omitted and defaults to 0 step can be omitted and defaults to 1

5 For Loops -- Revisited L1 = [1, 2, 3, 4] L2 = [‘A’, ‘B’, ‘C’, ‘D’] Want to print each element from L1 followed by the corresponding element from L2. for num in L1: print(num, ??) # How do we print the item from L2? Loop over indices: for index in range(len(L2)): print(L1[index], L2[index])

6 While Loops Sometimes we need to loop until a condition is met. For example: –Ask user for a password twice –If the passwords don’t match, ask again until they match or the user quits

7 While Loop Example English example: Ask for password. Ask for password again. While the passwords don’t match: Ask again.

8 While Loop Example Python example: password1 = input(‘Enter password: ’) Ask for password again. While the passwords don’t match: Ask again.

9 While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) While the passwords don’t match: Ask again.

10 While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) while password1 != password2: Ask again.

11 While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) while password1 != password2: password2 = input(‘Re-enter password:’)


Download ppt "Last Week Doctests Lists List methods Nested Lists Aliasing."

Similar presentations


Ads by Google