Lesson 1
Lesson 1 of 34
Functions
Lesson Progress: 0%
Code Example
# function with simple output
def shout():
print("WATCH OUT!")
# function with simple output
def show_line():
print("**********")Instructions
▼ ← Click the triangle to hide or reveal instructions.Python Code Editor
Task Incomplete
Editor Input:
Editor Output:
def, which is short for "define."shout().Code Example
# simple function with a return
def show_color(color):
print("Your favorite color is", color)
show_color("Blue")
# simple function with a return
def add_ten(number):
print(number + 10)
add_ten(5)Instructions
▼ ← Click the triangle to hide or reveal instructions.Python Code Editor
Task Incomplete
Editor Input:
Editor Output:
def and give it a name.color or number) is what we give to the function.print() shows a message on the screen.show_color("Blue").Code Example
# Function to show an item and its price
def buy_item(item, price=5.0):
print("Item:", item, "| Price:", price)
buy_item("Book")
buy_item("Phone", 500.0)
# Function to print a message from a specific user
def send_msg(text, user="Guest"):
print(user, "says:", text)
send_msg("Hello!")
send_msg("I am late", "Sam")Instructions
▼ ← Click the triangle to hide or reveal instructions.Python Code Editor
Task Incomplete
Editor Input:
Editor Output:
price=5.0.Code Example
# Function to return both the sum and difference of two numbers
def get_math(a, b):
return a + b, a - b
total, diff = get_math(10, 5)
print(total, diff)
# Function to return the first and last name from a list
def split_name(full_name):
first = full_name[0]
last = full_name[1]
return first, last
fname, lname = split_name(["Sam", "Smith"])
print(fname, lname)Instructions
▼ ← Click the triangle to hide or reveal instructions.Python Code Editor
Task Incomplete
Editor Input:
Editor Output:
return to send answers back from the function.total and diff.print() shows the answers on the screen so we can see them.