Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
College aantekeningen

Chapter 4 - Starting Out With Python Notes w/ Examples

Beoordeling
-
Verkocht
-
Pagina's
6
Geüpload op
16-10-2024
Geschreven 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.

Instelling
Vak

Voorbeeld van de inhoud

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

Gekoppeld boek

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
16 oktober 2024
Aantal pagina's
6
Geschreven in
2024/2025
Type
College aantekeningen
Docent(en)
Dr. reham abou-zaid
Bevat
Alle colleges

Onderwerpen

€7,57
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
jaystoff

Maak kennis met de verkoper

Seller avatar
jaystoff Eastern Illinois University
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
-
Lid sinds
1 jaar
Aantal volgers
0
Documenten
1
Laatst verkocht
-

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Populaire documenten

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Veelgestelde vragen