1 / 40

Stepping through a ‘for’ loop

Stepping through a ‘for’ loop. At the beginning - run module. for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.”. Variable Names. Objects. Start with the first line - the ‘for’ statement. for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]:

yovela
Download Presentation

Stepping through a ‘for’ loop

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. Stepping through a ‘for’ loop

  2. At the beginning - run module for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  3. Start with the first line - the ‘for’ statement for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  4. Look at the list for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  5. Is it empty? No. Start with the first object for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  6. name “Andrew” Assign the first object to the variable ‘name’ for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects Create the string object “Andrew” and assign it to the variable named ‘name’

  7. name “Andrew” Then start the first line of the code block for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  8. name “Andrew” This is the ‘print’ statement for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  9. name “Andrew” print the string object “Hello,” and the value of the variable with name ‘name’ for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects Hello,Andrew

  10. name “Andrew” The print statement is finished. Python is at the end of the code block... for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  11. name “Andrew” ... so go to the ‘for’ statement again for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  12. name “Andrew” Move the list pointer forward by one for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  13. name “Andrew” Is it past the end? No, it’s on the second item. for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  14. name “Andrew” “Tsanwani” Assign the second object to the variable ‘name’ for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects Create the string object “Tsanwani” and assign it to the variable named ‘name’

  15. name “Andrew” “Tsanwani” Then start the first line of the code block for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  16. name “Andrew” “Tsanwani” print the string object “Hello,” and the value of the variable with name ‘name’ for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects Hello, Andrew Hello,Tsanwani

  17. name “Andrew” “Tsanwani” The print statement is finished. Python is at the end of the code block... for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  18. name “Andrew” “Tsanwani” ... so go to the ‘for’ statement again for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  19. name “Andrew” “Tsanwani” Move the list pointer forward by one for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  20. name “Andrew” “Tsanwani” Is it past the end? No, it’s on the third item. for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  21. name “Andrew” “Tsanwani” “Arno” Assign the third object to the variable ‘name’ for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects Create the string object “Arno” and assign it to the variable named ‘name’

  22. name “Andrew” “Tsanwani” “Arno” Then start the first line of the code block for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  23. name “Andrew” “Tsanwani” “Arno” print the string object “Hello,” and the value of the variable with name ‘name’ for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects Hello, Andrew Hello, Tsanwani Hello,Arno

  24. name “Andrew” “Tsanwani” “Arno” The print statement is finished. Python is at the end of the code block... for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  25. name “Andrew” “Tsanwani” “Arno” ... so go to the ‘for’ statement again for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  26. name “Andrew” “Tsanwani” “Arno” Move the list pointer forward by one for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  27. name “Andrew” “Tsanwani” “Arno” Is it past the end? No, it’s on the fourth item. for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  28. name “Andrew” “Tsanwani” “Arno” “Tebogo” Assign the fourth object to the variable ‘name’ for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects Create the string object “Tebogo” and assign it to the variable named ‘name’

  29. name “Andrew” “Tsanwani” “Arno” “Tebogo” Then start the first line of the code block for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  30. name “Andrew” “Tsanwani” “Arno” “Tebogo” print the string object “Hello,” and the value of the variable with name ‘name’ for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects Hello, Andrew Hello, Tsanwani Hello, Arno Hello,Tebogo

  31. name “Andrew” “Tsanwani” “Arno” “Tebogo” The print statement is finished. Python is at the end of the code block... for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  32. name “Andrew” “Tsanwani” “Arno” “Tebogo” ... so go to the ‘for’ statement again for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  33. name “Andrew” “Tsanwani” “Arno” “Tebogo” Move the list pointer forward by one for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  34. name “Andrew” “Tsanwani” “Arno” “Tebogo” Is it past the end? Yes! for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  35. name “Andrew” “Tsanwani” “Arno” “Tebogo” So skip past the code block to the next statement. for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  36. name “Andrew” “Tsanwani” “Arno” “Tebogo” This is another print statement for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  37. name “Andrew” “Tsanwani” “Arno” “Tebogo” It prints the string “The end.” for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects Hello, Andrew Hello, Tsanwani Hello, Arno Hello, Tebogo The end.

  38. name “Andrew” “Tsanwani” “Arno” “Tebogo” Python looks for the next statement... for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  39. name “Andrew” “Tsanwani” “Arno” “Tebogo” ... but there isn’t any, so Python stops. for name in ["Andrew", "Tsanwani", "Arno", "Tebogo"]: print "Hello,", name print “The end.” Variable Names Objects

  40. Final Program Output Hello, Andrew Hello, Tsanwani Hello, Arno Hello, Tebogo The end.

More Related