top of page

Chapter 3 Functions Study Notes - Automate the Boring Stuff with Python

  • Writer: Unusual Accountant
    Unusual Accountant
  • Jan 10, 2022
  • 2 min read


Chapter 3 Functions Study Notes - Practice Questions for Concepts related to Functions



Practice Questions


1. Why are functions advantageous to have in your programs?

A: Functions reduce the need for duplicate code. This makes programs shorter, easier to read, and easier to update.


2. When does the code in a function execute: when the function is defined or when the function is called?

A: The code in a function executes when the function is called, not when the function is defined.


3. What statement creates a function?

A: The def statement defines (that is, creates) a function.


4. What is the difference between a function and a function call?

A: A function consists of the def statement and the code in its def clause. A function call is what moves the program execution into the function, and the function call evaluates to the function’s return value.


5. How many global scopes are there in a Python program? How many local scopes?

A: There is one global scope, and a local scope is created whenever a function is called.


6. What happens to variables in a local scope when the function call returns?

A: When a function returns, the local scope is destroyed, and all the variables in it are forgotten.


7. What is a return value? Can a return value be part of an expression?

A: A return value is the value that a function call evaluates to. Like any value, a return value can be used as part of an expression.


8. If a function does not have a return statement, what is the return value of a call to that function?

A: If there is no return statement for a function, its return value is None.


9. How can you force a variable in a function to refer to the global variable?

A: A global statement will force a variable in a function to refer to the global variable.


10. What is the data type of None?

A: The data type of None is NoneType.


11. What does the import areallyourpetsnamederic statement do?

A: That import statement imports a module named areallyourpetsnamederic. (This isn’t a real Python module, by the way.)


12. If you had a function named bacon() in a module named spam, how would you call it after importing spam?

A: This function could be called with spam.bacon().


13. How can you prevent a program from crashing when it gets an error?

A: Place the line of code that might cause an error in a try clause.


14. What goes in the try clause? What goes in the except clause?

A: The code that could potentially cause an error goes in the try clause. The code that executes if an error happens goes in the except clause.


Reference: https://automatetheboringstuff.com/2e/appendixc/

Comments


Never Miss a Post. Subscribe Now!

I'm a paragraph. Click here to add your own text and edit me. It's easy.

Thanks for submitting!

© 2023 by Unusual Accountant

  • Grey Twitter Icon
bottom of page