1 / 24

COMPSCI 101 S1 2014 Principles of Programming

COMPSCI 101 S1 2014 Principles of Programming. 16 Iterating through a range. Recap. A list stores data as a sequence Each element of the list can be accessed We use a for loop to iterate through the contents of a list len() returns the number of elements in a list.

arnie
Download Presentation

COMPSCI 101 S1 2014 Principles of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 101 S1 2014Principles of Programming 16 Iterating through a range

  2. Recap • A list stores data as a sequence • Each element of the list can be accessed • We use a for loop to iterate through the contents of a list • len() returns the number of elements in a list for fruit in ["apple", "banana"]: print (fruit) for item in [1, 2, 3]: print (item) sum = 0 for item in [1, 2, 3]: sum = sum + item print (sum) sum = 0 for item in [1, 2, 3]: sum = sum + item print (sum) 1 3 6 6 COMPSCI101

  3. Week 6 for item in [1, 2, 3]: print (item) • Lecture 16: • The range() function • The range() function + list() => to generate a list • for + range() -=> iterating through a range • Lecture 17: • Indexing: use the index operator to access individual elements • [ ]: positive indices and negative indices • Lecture 18: • for + index =>Iterating through sequences using index values 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 … 3 x 9 = 27 [1, 2, 3] 1+2+3+4 = 10 14 13 [ 0, 5, 19, 21, 34] 2 5 COMPSCI101

  4. Learning outcomes • At the end of this lecture, students should be able to: • understand the range() function, • Iterating through a range • Examples and Exercises: • Case study 1: Printing the Squares of Numbers • Case study 2: Multiplication Table • Case study 3: Calculating the Total • Exercise 1: Printing Numbers • References: • The Python Standard Library • https://docs.python.org/3.4/library/stdtypes.html#typesseq-range • The Python Tutorial • https://docs.python.org/3/tutorial/controlflow.html COMPSCI101

  5. 16.1 The range function Understanding the range function • The range function generates arithmetic progressions • The standard range() function generates numbers from zero to one less than the parameter. • The general Python syntax for the range function is • The arguments passed to the range constructor must be integers! • Note: the range() function returns a "range object" only. You must convert the "range object" into a list with the list() function. • Examples: range(n) returns 0, 1, 2, …, stop-1 range(stop) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(10)) Increment of ONE by default! [0, 1, 2, 3] >>> list(range(4)) Empty List! >>> list(range(0)) >>> list(range(-1)) [] [] COMPSCI101

  6. 16.1 The range function Understanding the range function • Two parameters: • The syntax for the range function with two parameters is • It generates numbers from start to one less than stop. • The arguments passed to the range constructor must be integers! • Examples: range(start, stop) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(1, 10)) [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> list(range(10, 20)) [-5, -4, -3, -2] >>> list(range(-5, -1)) range(start, stop) returns start, start + 1, …, stop-1 Can take negative numbers! COMPSCI101

  7. 16.1 The range function Understanding the range function • Three parameters: • The syntax for the range function with THREE parameters is: • It generates numbers from start up to (but not including) stop in steps of step • Examples: range(start, stop, step) [1, 3, 5, 7, 9] >>> list(range(1, 10, 2)) [1, 4, 7] >>> list(range(1, 10, 3)) COMPSCI101

  8. 16.1 The range function Parameters: start, stop, step • Requirements: • The arguments passed to the range constructor must be integers! • step must not be zero ! TypeError:'str' object cannot be interpreted as an integer >>> list(range('z')) ValueError: range() arg 3 must not be zero >>> list(range(1, 10, 0)) COMPSCI101

  9. 16.1 The range functionStep Demo 1 • If step is positive, the last element is the largest • if step is negative, the last element is the smallest [1, 4, 7] ascending order >>> list(range(1, 10, 3)) start < stop [] Empty List >>> list(range(10, 1, 3)) descending order [10, 8, 6, 4, 2] >>> list(range(10, 1, -2)) [] >>> list(range(1, 10, -2)) Empty List COMPSCI101

  10. 16.2 Iterating through a rangeUsing the range() function • The range() function is most often used in for loops • The syntax is: • Example: for i in range(start, stop): # Loop body false i <5 yes for i in range(5): print(i) print (i) 0 1 2 3 4 COMPSCI101

  11. 16.2 Iterating through a rangeExamples Demo 1 • Using the range() function • Examples: 1 2 3 4 5 for i in range(1, 6): print(i) 1 3 5 for i in range(1, 6, 2): print(i) for i in range(5, 1, -1): print(i) 5 4 3 2 COMPSCI101

  12. 16.2 Iterating through a rangeExercise 1: Printing Numbers Exercise 1 • Given the following code fragment: • It prints integers from 0 to 4. • How to print numbers in reverse order? (i.e. 4, 3, 2, 1, 0) 0 1 2 3 4 for i in range(5): print(i) 4 3 2 1 0 for i in range(4,-1,-1): print(i) COMPSCI101

  13. 16.2 Iterating through a rangeExercise 2: Printing Numbers • Task: • Complete the print_numbers()function which prints numbers starting from start and going up to but not including stopand then going back down to start • Arguments: start, stop • Prints: Elements at: start, start+1, … stop-1, stop-1, … start+1, start • Case: 2 3 4 5 5 4 3 2 print_numbers(2, 6) COMPSCI101

  14. 16.2 Iterating through a rangeExercise 2: Printing Numbers Exercise 2 start • Case: • Prints 2, 3, 4, 5 • Prints 5, 4, 3, 2 • Algorithm: print_numbers(2, 6) stop COMPSCI101

  15. Case Study 1Printing the Squares of Numbers • Task: • Complete the print_squares()function which prints the squares of the numbers between 1 and n • Arguments: a number, n • Prints: squares of the numbers between 1 and n (inclusive) • Cases: 1 4 print_squares(2) 1 4 9 16 print_squares(4) COMPSCI101

  16. Case Study 1Printing the Squares of Numbers 1 4 • Case • Questions: n print_squares(2) Start = 1 Stop = n + 1 Step = 1 print (i*i) COMPSCI101

  17. Case Study 1Printing the Squares of Numbers Demo 2 1 4 • Case: • Algorithm: print_squares(2) n for i in range(1, n+1): If no more items in sequence Item from sequence Next item Calculate the square of i And print it print (i*i) COMPSCI101

  18. Case Study 2Multiplication Table • Task: • Complete the multiplication_table() function which prints a multiplication table of a given number • Arguments: n (a positive number) • Prints: a multiplication table of a given number • Case: 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 multiplication_table(3) COMPSCI101

  19. 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 … 3 x 9 = 27 Case Study 2Multiplication Table • Questions: Start = 1 Stop = 10 Step = 1 multiplication_table(3) i i * parameter parameter n 3 x 1 = 3 string string COMPSCI101

  20. Case Study 2Multiplication Table Demo 3 • Algorithm: multiplication_table(3) 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 for i in range(1, 10): If no more items in sequence Item from sequence Next item Print a row print ( n, "x", i, "=", (i*n)) COMPSCI101

  21. Case Study 3Accumulator Loop • A common approach in loops and the range function is to accumulate something in every iteration of the loop. • Task: • Complete the sum() function which returns the sum of the numbers from 1 to n inclusive • Arguments: n (a positive number) • Returns: sum • Cases: 1+2+3 = 6 6 sum(3) 1+2+3+4 = 10 10 sum(4) COMPSCI101

  22. Case Study 3Calculating the Total using the range() • Case: • Questions: n 1+2+3+4 = 10 10 sum(4) Start = 1 Stop = n + 1 Step = 1 total = total + i COMPSCI101

  23. Case Study 3Calculating the Total Demo 4 • Algorithm: Set total = 0 for i in range(1, n+1): If no more items in sequence Item from sequence Next item Calculate the total total = total + i COMPSCI101

  24. Summary • The range() function generates arithmetic progressions • The range() function generates numbers from zero up to (but not including) the parameter. • The range(start, stop, step) function generates numbers from startup to (but not including) stopin steps of step • The arguments passed to the range constructor must be integers! • Note: the range() function returns a "range object“ only. • The range() function is most often used in for loops. COMPSCI101

More Related