100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

Chapter 4 - Starting Out With Python Notes w/ Examples

Rating
-
Sold
-
Pages
6
Uploaded on
16-10-2024
Written in
2024/2025

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.










Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
October 16, 2024
Number of pages
6
Written in
2024/2025
Type
Class notes
Professor(s)
Dr. reham abou-zaid
Contains
All classes

Content preview

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
$7.99
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
jaystoff

Get to know the seller

Seller avatar
jaystoff Eastern Illinois University
View profile
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
1 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions