Notes on chapter 4 of Tony Gaddis' Starting Out with Python complete with screenshotted examples of class lectures, both from the textbook and from the professor. Simplified bullet points.
4.1: Introduction to Repetition Structures
● A repetition structure causes a statement or set of statements to execute repeatedly
● A condition-controlled loop uses a true/false condition to control the number of times that it
repeats
● A count-controlled loop repeats a specific number of times
4.2: The while Loop: A Condition-Controlled Loop
● While a condition is true, do some task
● Two parts: a condition is tested to be true or false, and a statement is executed if it is true
● while condition:
statement
statement
● If the statement is false, it exits the loop
● Each execution of the body of a loop is known as an iteration
The while Loop is a Pretest Loop
● The while loop is a pretest loop, meaning it tests the condition before performing an iteration
● Usually, you have to write some steps before the loop for it to iterate
Infinite Loops
● Almost always, loops must contain a way to terminate itself; something must make it false
eventually
● An infinite loop continues until the program is interrupted
● You should not write infinite loops; they are usually
mistakes
4.3: The for Loop: A Count-Controlled Loop
● Count-controlled loops iterate a specific number of times
● for variable in [value1, value2, etc…]
statement
statement
, ● The variable used in the for clause is the target variable
Using the range Function with the for Loop
● The range function creates an iterable
○ An iterable is an object similar to a list; it contains a sequence of values that can be
iterated over with something like a loop
● Instead of using a list of values, you can pass a number as an
argument
○ for num in range(5):
print(num)
○ This prints 5 numbers
○ Whatever number you put in the range, it goes from 0
to, but not including, that number
○ That code above is the same as:
for num in range[0, 1, 2, 3, 4]
print(num)
● If you pass 1 number as an argument, it acts as the end number with the starting number being 0;
if you pass 2 numbers, the first number is the starting number and the second is the end
● If you pass 1 or 2 numbers, each number increases by 1 in the range; if you pass 3, the 3rd
number acts as the interval (i.e. if you put 2, each number increases by 2); this is called the step
value
Using the Target Variable Inside the Loop
● Target variables reference each item in a sequence as the loop iterates
● Each time the loop iterates, the variable references the next number in the range
Letting the User Control Loop Iterations
● Sometimes, the user needs to decide how many loops need to be created
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller jaystoff. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.99. You're not tied to anything after your purchase.