Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Python Loops and Parameters: Understanding Mutable Objects and Sentinel Values, Study notes of Software Engineering

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

Pre 2010

Uploaded on 08/18/2009

koofers-user-69r-2
koofers-user-69r-2 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PARAMETERS, INDEFINITE
LOOPS, AND LOOP PATTERNS
CSSE 120—Rose Hulman Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Python Loops and Parameters: Understanding Mutable Objects and Sentinel Values and more Study notes Software Engineering in PDF only on Docsity!

PARAMETERS, INDEFINITELOOPS, AND LOOP PATTERNS CSSE 120—Rose Hulman Institute of Technology

Review: Python parameter passing

^ 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?

Modifying 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

Recap: Two main types of loops

^ 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!

Interactive: Make the user count

^ 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?

Interactive: Ask user if there is more

^ Open module averageMoreData.py and execute ittogether ^ User no longer has to count, but still has a bigburden

Non-numeric Sentinel

^ 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.

File loop

^ 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()

Individual Exercise on Using loops

Define function

listAndMax()

in module

listMax.py

that

†^ Prompts the user to enter numbers, one at a time †^ Uses a blank line () as sentinel to terminate input †^ Accumulates the numbers in a list †^ Uses a loop to calculate the maximum value of the numbers †^ Returns two values:^ „^ the list of numbers entered in the order they were entered^ „^ the maximum value Define function

main()

in module

listMax.py

that

†^ Calls listAndMax() †^ Prints the list of numbers entered †^ Prints the maximum value of the list of numbers

  • Start homework When you are through with your individual exercisecommit your solutions to your svn repository Start working on homework