1 / 57

Core Elements

Core Elements. Part iii: Iterations. Overview. Tuples Mutable, Immutable object For Loops While Loops. Lists in Python. Lists are mutable objects, e.g. we can modify their contents. Code. >>> my_list = [1, 10, 4, 5 ] # List creation [ ] >>> my_list [2 ] # Accessing the third element

drea
Download Presentation

Core Elements

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. Core Elements Part iii: Iterations TPOP

  2. Overview • Tuples • Mutable, Immutable object • For Loops • While Loops TPOP

  3. Lists in Python • Lists are mutable objects, e.g. we can modify their contents. Code >>>my_list = [1, 10, 4, 5] # List creation [ ] >>>my_list[2]# Accessing the third element 4 >>>my_list [1, 10, 4, 5] >>>my_list[2] = 9 # Modifying the third element >>>my_list# The modified list [1, 10, 9, 5] >>> TPOP

  4. Tuples in Python • Tuples are immutable objects, e.g. we CANNOT modify their contents. Code >>>my_tuple= (1, 10, 4, 5)# tuple creation ( ,) >>>my_tuple[2]# Accessing the third element 4 >>>my_tuple (1, 10, 4, 5) >>>my_tuple[2] = 9 # Modifying the third element Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> my_tuple[2] = 9 TypeError: 'tuple' object does not support item assignment >>> TPOP

  5. Tuples in Python • Tuples are immutable objects, e.g. we CANNOT modify their contents. • Note that numbers and strings are immutable too. Code >>>my_t= (1, 10, 4, 5)# tuple creation ( ,) >>>my_t = my_t(:2) + (9,) + my_t(3:) # Modifying the third element >>>my_t (1, 10, 9, 5) TPOP

  6. Be Careful • Example: Code >>>lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>>lst1 [2, 4, 6, 8] >>>lst2 [2, 4, 6, 8] >>> t1 (1, 3, 7, 9) >>> t2 (1, 3, 7, 9) >>> TPOP

  7. Be Careful • Example: A change in t2 does not affect t1 Code >>>t2 = t2[:2] + (5, ) + t2[3:] # Modifying the third element t2 >>> lst2[2] = 5 # Modifying the third element of lst2 >>>t1 (1, 3, 7, 9) >>> t2 (1, 3, 5, 9) >>> TPOP

  8. Be Careful • Example: A change in t2 does not affect t1 Code >>>t2 = t2[:2] + (5, ) + t2[3:] # Modifying the third element t2 >>> lst2[2] = 5 # Modifying the third element of lst2 >>>t1 (1, 3, 7, 9) >>> t2 (1, 3, 5, 9) >>> lst1 [2, 4, 5, 8] >>> lst2 [2, 4, 5, 8] >>> A change in lst2 does affect lst1 TPOP

  9. Be Careful • State Diagram: 6 4 2 8 0 1 2 3 lst1 Code lst2 >>>lst1 = lst2 = [2,4,6,8] >>> TPOP

  10. Be Careful • State Diagram: 7 6 4 3 2 1 8 9 0 1 2 3 0 1 2 3 lst1 Code lst2 >>>lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> t1 t2 TPOP

  11. Be Careful • State Diagram: 6 7 5 3 4 3 1 2 1 9 9 8 0 1 2 3 0 1 2 3 0 1 2 3 lst1 Code lst2 >>>lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>>t2 = t2[:2] + (5, ) + t2[3:] >>> t1 t2 TPOP

  12. Be Careful • State Diagram: 5 6 7 3 3 4 1 1 2 8 9 9 0 1 2 3 0 1 2 3 0 1 2 3 5 lst1 Code lst2 >>>lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>>t2 = t2[:2] + (5, ) + t2[3:] >>>lst2[2] = 5 >>> t1 t2 TPOP

  13. Be Careful • State Diagram: 5 5 7 5 4 3 3 4 2 1 1 2 9 8 9 8 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 lst1 Code lst2 >>>lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>>t2 = t2[:2] + (5, ) + t2[3:] >>>lst2[2] = 5 >>> >>>lst2 = [2, 4, 5, 8] >>> t1 t2 TPOP

  14. Be Careful • State Diagram: 7 5 5 5 4 3 4 3 1 2 2 1 8 9 9 8 0 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 lst1 Code lst2 >>>lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>>t2 = t2[:2] + (5, ) + t2[3:] >>>lst2[2] = 5 >>> >>>lst2 = [2, 4, 5, 8] >>> lst2[2] = 0 t1 t2 TPOP

  15. Swapping Values • State Diagram: 5 num_one 9 Code num_two >>>num_one = 5 >>>num_two = 9 >>> TPOP

  16. Swapping Values • State Diagram: First Attempt 5 num_one 9 Code num_two >>>num_one = 5 >>>num_two = 9 >>>num_one = num_two >>>num_two = num_one >>> TPOP

  17. Swapping Values • State Diagram: Second Attempt 5 num_one 9 Code num_two >>>num_one = 5 >>>num_two = 9 >>>temp = num_two >>>num_two = num_one >>> num_one = temp >>> temp TPOP

  18. Iteration Definite & indefinite loops TPOP

  19. Iteration • We need a structure to execute a sequence of statements multiple times in succession • So Far, we have to write the sequence of statement n times to achieve n repetition • How can we proceed if we are not sure how many times it needs to be repeated? TPOP

  20. Definite Loop: For statement • The simplest kind of loop • It will execute a definite amount of times • The for loop statement for <var> in <iterable>: <body> • Iterable can return its element one at a time • The body of the loop can be any sequence of Python statements • The variable after the keyword for is called the loop index. TPOP

  21. Example Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print"--> inside loop: square of", str(val), "is", str(square_val) print"After the for loop" Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 --> inside loop: square of 3 is 9 After the for loop TPOP

  22. Example Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print . . . print "After the for loop" TPOP

  23. Example val 3 2 1 Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print . . . print "After the for loop" Before the for loop TPOP

  24. Example val square_val 3 2 1 1 Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print . . . print "After the for loop" Before the for loop TPOP

  25. Example val square_val 3 2 1 1 Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print . . . print "After the for loop" Before the for loop --> inside loop: square of 1 is 1 TPOP

  26. Example val square_val 3 2 1 1 Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print . . . print "After the for loop" Before the for loop --> inside loop: square of 1 is 1 TPOP

  27. Example val square_val 3 2 4 Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print . . . print "After the for loop" Before the for loop --> inside loop: square of 1 is 1 TPOP

  28. Example val square_val 3 2 4 Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print . . . print "After the for loop" Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 TPOP

  29. Example val square_val 3 2 4 Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print . . . print "After the for loop" Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 TPOP

  30. Example val square_val 3 9 Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print . . . print "After the for loop" Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 TPOP

  31. Example val square_val 3 9 Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print . . . print "After the for loop" Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 --> inside loop: square of 3 is 9 TPOP

  32. Example val square_val 3 9 Code Python shell print"Before the for loop" forvalin [1,2,3]: square_val = val * val print . . . print "After the for loop" Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 --> inside loop: square of 3 is 9 After the for loop TPOP

  33. Indefinite Loop: While Statement • An indefinite loop keeps iterating until certain condition are met (conditional loop) • There is no guarantee ahead of time regarding how many times the loop will go around • zero time • x-times • indefinitely TPOP

  34. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  35. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  36. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  37. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  38. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  39. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  40. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  41. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  42. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  43. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  44. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  45. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  46. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  47. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  48. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> False <condition> True <body> TPOP

  49. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> • The <body> usually contains statements that modify the evaluation of <condition> at some point False <condition> True <body> TPOP

  50. Indefinite Loop: While Statement • The while loop statement while <condition>: <body> • The <body> usually contains statements that modify the evaluation of <condition> at some point • The <body> may never be executed! False <condition> True <body> TPOP

More Related