Skip to main content

Repetition

  • Chapter
  • First Online:
The Python Workbook

Part of the book series: Texts in Computer Science ((TCS))

Abstract

Loops allow a program to perform a large number of calculations with only a few lines of code. Python includes two types of loops: while loops and for loops. Both while loops and for loops cause a program’s execution to return to an earlier point and make a decision about whether or not a group of statements should be executed again.

  • A while loop executes a group of statements as long as, or while, something is true. Like an if statement, a while loop has a condition which can include both relational operators and Boolean operators.

  • A for loop executes a group of statements once for each value in a collection. A collection of integers can be constructed using Python’s range function. For loops can also be used with collections of values like lists and dictionaries which are discussed in later chapters.

This chapter ends with 22 exercises that allow the reader to practice using both while loops and for loops. In some cases the exercises tell the reader which type of loop to use. Other exercises require the user to analyze the problem carefully and make a decision about which type of loop is most suitable for solving it.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Subscribe and save

Springer+ Basic
EUR 32.99 /Month
  • Get 10 units per month
  • Download Article/Chapter or eBook
  • 1 Unit = 1 Article or 1 Chapter
  • Cancel anytime
Subscribe now

Buy Now

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 49.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info
Hardcover Book
USD 54.99
Price excludes VAT (USA)
  • Durable hardcover edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Ben Stephenson .

Rights and permissions

Reprints and permissions

Copyright information

© 2019 Springer Nature Switzerland AG

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Stephenson, B. (2019). Repetition. In: The Python Workbook. Texts in Computer Science. Springer, Cham. https://doi.org/10.1007/978-3-030-18873-3_3

Download citation

  • DOI: https://doi.org/10.1007/978-3-030-18873-3_3

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-030-18872-6

  • Online ISBN: 978-3-030-18873-3

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics