









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
Various aspects of functions and decision structures in python programming. It explains how functions can return values, the concept of multiple value return, and the difference between mutating parameters and returning new values. The document also discusses decision structures, specifically the if statement and its semantics, as well as more complex multi-way decisions using if-elif-else statements.
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!










CSSE 120—Rose Hulman Institute of Technology
We've written functions that just do things printFactorial(5) printDistance(p1,p2)
We've used functions that return values abs(-1) fn_root_1 = math.sqrt(bb – 4a*c)
Define a function that returns a value # _____________________ def square(x): return x * x
Returns the square of x
return statement
Download threeSquares.py from Angel
Run the program to be sure it works
Add a function, stats that takes a Rectangle, r , as a parameter and returns the area of r
Change the program to display the area of each rectangle inside the shape
Finally, change stats to return the area and perimeter (see figure at right)
Upload to threeSquares dropbox on Angel
Example Display
How do functions send information back?
Return statements Mutating parameters
Consider: def awardEC(score, extra): newScore = score + extra score = newScore
earned = 87 bonus = 4 awardEC(earned, bonus) print earned
def awardEC(score, extra): newScore = score + extra return newScore
earned = 87 bonus = 4 earned = awardEC(earned, bonus) print earned
def awardEC(scores, extras): for i in range(len(scores)): scores[i] = scores[i] + extras[i] earned = [87, 63, 94] bonuses = [3, 5, 0] awardEC(earned, bonuses) print earned
Did the value of amounts change?
No, it refers to the same list The list's contents changed Functions can change state of mutable objects
The if statement
if :
Semantics: "if the condition is true, run the body, otherwise skip it"
Simple conditions
Some relational operators: Math < ≤ = ≥ > ≠ Python < <= == >= > !=
Define a function grade(score)
Where score is an exam score and result is "perfect", "passing", or "failing" based on the score
Syntax: if :
else:
Semantics: "If the condition is true, execute the statements for true, otherwise execute the statements for false"
Define a function countFailPass(scores) that
takes a list of exam scores returns two values: the count of failing scores in the list (those less than 60), and the count of passing scores in the list
Examples:
countFailPass([57, 100, 34, 87, 74]) returns (2,3) countFailPass([59]) returns (1,0) countFailPass([]) returns (0,0)
Upload to countFailPass dropbox on Angel
Syntax: if :
elif :
elif :
… else:
reach here if condition1 is false reach here if condition1 is false AND condition2 is true reach here if BOTH condition1 AND condition2 are false
Advantages of if-elif-else vs. nesting
Number of cases is clear Each parallel case is at same level in code Less error-prone
Fix grade function to use if-elif-else statement instead of nesting