Loading the lesson
Loading the lesson
Forward Deployed Engineer · Programming Foundations · Lesson 5 of 10
Breaking a program into functions, importing modules, and using the standard library.
Read the short notes and pictures.Do everything inside a blue frame.
Before you read: a function prints a result and another returns it. Which one can you use in a calculation afterwards?
When the same steps are needed in more than one place, put them in a function and give it a name. Values go in, a result comes out.
Defining a function runs nothing. Calling it does. Watch a new box of memory appear for the call and vanish when it returns.
1def area(width, height):2result = width * height3return result45room = area(4, 3)6print(room)
Press Next to run the first line.
Memory
Nothing yet
Output
After the call finishes, can the rest of the program read result?
Write a function is_adult(age) that returns True when age is 18 or more, and False otherwise.
Hint: A comparison already produces True or False, so you can return it directly.
1def greet(name, greeting="Hello"):2return greeting + ", " + name34print(greet("Asha"))5print(greet("Ravi", "Welcome"))
Press Next to run the first line.
Memory
Nothing yet
Output
import brings a module into your program so you can use what is inside it.
1import math2from statistics import mean34print(math.sqrt(49))5print(mean([4, 8, 6]))
Press Next to run the first line.
Memory
Nothing yet
Output
Built in
print, lenAlways there. Nothing to import.
Standard library
import jsonComes with Python. Import it and use it.
Third party
pip install requestsWritten by others. Install once, then import.
Your own
import helpersAny .py file of yours can be imported.
You wrote: import math. Which line works?
Once a program grows past a screen, split it into files by job. Put the functions that talk to the internet in one file and the ones that save data in another. Then import them by file name, without the .py.
helpers.py
def to_fahrenheit(c):
return c * 9 / 5 + 32
def describe(c):
if c >= 30:
return "hot"
return "fine"main.py
import helpers
def main():
c = 31
print(helpers.to_fahrenheit(c))
print(helpers.describe(c))
if __name__ == "__main__":
main()Python sets __name__ to "__main__" only in the file you ran. In a file that was imported, it is the file name instead. So the last two lines of main.py say: start the program, but only when this file is the one being run, not when something imports it.
python main.py
__name__ == "__main__"main() runs and the program starts.
import main
__name__ == "main"The functions load, nothing starts. Handy for tests.
You have weather.py with a function find_city. How does main.py call it?
Practice: write functions
Write a function to_fahrenheit(c) that returns c × 9 / 5 + 32.
Press Run to see the output here.
def area(w, h):
print(w * h)
x = area(3, 4) # shows 12
print(x + 1) # error: x is Nonereturn
def area(w, h):
return w * h
x = area(3, 4) # x is 12
print(x + 1) # 13Write a function initials(full_name) that returns the initials in capitals. initials("asha rao") should return "AR".
Hint: Split the name into words, take the first letter of each, join them and use .upper().
Keep these