1 / 14

Python – Loops and Iteration

Python – Loops and Iteration. Lecture03. Sums. What’s the sums of the numbers from 1 to 10?. sum = 0 sum = sum + 1 sum = sum + 2 sum = sum + 3 sum = sum + 4 sum = sum + 5 sum = sum + 6 sum = sum + 7 sum = sum + 8 sum = sum + 9 sum = sum + 10

shakti
Download Presentation

Python – Loops and Iteration

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. Python – Loops and Iteration Lecture03

  2. Sums What’s the sums of the numbers from 1 to 10? sum = 0 sum = sum + 1 sum = sum + 2 sum = sum + 3 sum = sum + 4 sum = sum + 5 sum = sum + 6 sum = sum + 7 sum = sum + 8 sum = sum + 9 sum = sum + 10 printsum

  3. Sums What’s the sums of the numbers from 1 to 1000? sum = 0 for i inrange(1001): sum += i printsum

  4. range() The range function generates an array up to its argument. range(start) range(start, stop) range(start, stop, increment)

  5. Operator - in array = range(6) if5inarray: print"YEP!" for item inarray: print"YUP!"

  6. For Loops When you know how many times you want to loop for x inrange(1,10): pass

  7. While Loops For when you’re not sure how many times you want to iterate. while (condition): pass

  8. Keywords for Iteration break continue pass

  9. Break sum = 0 foriinrange(11): sum += 1 ifi == 5: break printsum

  10. Continue sum = 0 foriinrange(11): sum += 1 ifi == 5: continue printsum

  11. Pass sum = 0 foriinrange(11): sum += 1 ifi == 5: pass printsum

  12. Nesting Loops Loops can contain loops: sum = 0 foriinrange(10): for j inrange(10): sum += 1 printsum

  13. Printing a Square It can be accomplished the long way… print"* * * * * * * * * *" print"* * * * * * * * * *" print"* * * * * * * * * *" print"* * * * * * * * * *" print"* * * * * * * * * *" print"* * * * * * * * * *" print"* * * * * * * * * *" print"* * * * * * * * * *" print"* * * * * * * * * *" print"* * * * * * * * * *"

  14. Square – Deluxe Edition for row inrange(10): for column inrange(10): print"*", print""

More Related