








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of python's loop structures, including definite and indefinite loops, and discusses the concept of mutable objects and sentinel values in the context of parameter passing. Students will learn how to write functions that exchange parameters, modify parameters, and use sentinel values to signal the end of data. The document also includes interactive exercises and coding examples.
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!
^ Formal parameters only receive the
values
of the
actual parameters Assigning a new value to a formal parameter doesnot affect the actual parameter Python passes actual parameters
by value
^ Can functions in Python mutate parameters?
^ How do functions send information back?^ ^ Return statements^ ^ Mutating
parameters ^ Value of actual parameter must be a mutable object ^ State of the mutable object is changed ^ The actual parameter itself is NOT changed since it refers tothe same object ^ Parameter is still passed by value
^ Definite Loop^ ^ We know at the beginning of the loop how many times itsbody will execute^ ^ Implemented in Python as a
for^ loop.
^ Indefinite loop^ ^ The body executes as long as some condition is true.^ ^ Implemented in Python as a
while^ loop.
^ Can be an infinite loop if the condition never becomesFalse. Python's
for line in file:
construct
^ indefinite loop that looks syntactically like a definite loop!
^ Open module averageUserCount.py and execute ittogether ^ When does the loop terminate? ^ Is this the best way to make the user enter input?^ ^ Why?^ ^ Why not?
^ Open module averageMoreData.py and execute ittogether ^ User no longer has to count, but still has a bigburden
^ What if negative numbers are legitimate values? ^ Open module averageOtherSentinel.py and studythe code^ ^ Execute it together^ ^ What is the sentinel? ^ Again note:
sentinel value is not used in calculations.
^ Open module averageFile.py and execute togetherwith input file numbers.txt ^ Uses a
for^ loop as we have seen before
^ Also note the conditional execution of
main()
Define function
listAndMax()
in module
listMax.py
that
^ Prompts the user to enter numbers, one at a time ^ Uses a blank line (
main()
in module
listMax.py
that
^ Calls listAndMax() ^ Prints the list of numbers entered ^ Prints the maximum value of the list of numbers