1 / 11

Computer Science 101

Computer Science 101. For Statement. For-Statement . The For-Statement is a loop statement that is especially convenient for loops that are to be executed a specific number of times (not controlled by user input, etc.). The For-Statement is also convenient for working with lists.

ilana
Download Presentation

Computer Science 101

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. Computer Science 101 For Statement

  2. For-Statement • The For-Statement is a loop statement that is especially convenient for loops that are to be executed a specific number of times (not controlled by user input, etc.). • The For-Statement is also convenient for working with lists.

  3. For-Statement (cont.) • Syntax: for <variable> in <list> : <list of statements> • The variable is called the loop index. • Semantics: For each value in the list, the loop index takes on the value and the loop body is executed.

  4. Python Session

  5. Python Session

  6. Find Largest Example

  7. Range • range provides a list of consecutive numbers. • If num is a positive integer, then range(num)gives the list [0,1, …, num-1] • If num1 < num2, then range(num1, num2)gives the list [num1, num1+1, …, num2-1]

  8. Range Example

  9. Find Largest Once More

  10. Find Largest Once More

  11. It's a dirty job, but somebody's gotta do it!

More Related