• Home
  • Python
    • Introduction to Python
    • Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • Python
      • Introduction to Python
      • Python Developer
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • React
      • Introduction to React
      • React Developer
  • Interactive Training
  • Pricing
  • Brainstorm

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds

Courses

  • Python
    • Introduction to Python
    • Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • React
    • Introduction to React
    • React Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

Python Developer - Lesson 1

Lesson 1 of 34

Functions

Lesson Progress: 0%

Lesson Progress: 0%
Functions
Lesson Incomplete
Lesson 1 of 34
Next: DocStrings →
Code Example
# function with simple output
def shout():
    print("WATCH OUT!")

shout()

# function with simple output
def show_line():
    print("**********") 

show_line()

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A function is like a recipe that stores a set of instructions for Python to follow later.
  • We start by using the word def, which is short for "define."
  • Inside the function, we indent the code to show it belongs to that specific recipe.
  • The function won't do anything until you call its name, like shout().
  • This helps us reuse the same code over and over without having to type it all out again.
Code Example
# simple function with a return
def show_color(color):
    print("Your favorite color is", color)

show_color("Blue")

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A function is a small group of code that does a job for us.
  • We make a function using the word def and give it a name.
  • The word inside the parentheses (like color or number) is what we give to the function.
  • Inside the function, print() shows a message on the screen.
  • To use a function, we write its name with something inside the parentheses, like show_color("Blue").
Code Example
# 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:

Click "Run Code" to see the output here
  • A function is like a small machine that does a job for you.
  • This function takes in a number as input.
  • It changes the number by adding 10 to it.
  • Then it shows the new number on the screen.
Code Example
# simple function with a return
def multiply_by_two(number):
    print(number * 2)

multiply_by_two(6)

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A function is like a small machine that does a job.
  • This function takes in a number as input.
  • It changes the number by multiplying it by 2.
  • Then it shows the new number on the screen.
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)

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Think of a function like a machine that can take in different pieces of information to do a job.
  • Sometimes, we give a piece of information a "backup value" using the equals sign, like price=5.0.
  • This backup value is used automatically if you don't pick a specific one yourself.
  • If you do provide your own value, Python will use yours instead of the backup.
  • This is really helpful because it saves you time when most of your answers are the same!
Code Example
# 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:

Click "Run Code" to see the output here
  • user="Guest" gives a default name if no name is provided.
  • It is okay to skip the user, and "Guest" will be used automatically.
  • You must always give a message for text when calling the function.
  • print(user, "says:", text) shows who is speaking and what they said.
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)

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A function can send back more than one answer at the same time.
  • We use return to send answers back from the function.
  • When a function sends back two answers, we can save them into two names, like total and diff.
  • We can also pull out pieces from a list, like the first and last name.
  • print() shows the answers on the screen so we can see them.
Code Example
# Function to return both the product and division of two numbers
def get_math2(a, b):
    return a * b, a / b

product, division = get_math2(12, 3)
print(product, division)

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • This function takes in 2 inputs and gives back 2 outputs.
  • You must give values for both a and b when calling the function.
  • return a * b, a / b gives back the product and the division.
Code Example
# 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:

Click "Run Code" to see the output here
  • You can give a whole list as the input to a function.
  • The first item in a list is always at position 0.
  • The function uses one list to get two separate pieces of information.
  • The function returns two outputs: the first name and the last name.
Code Example
# Function to return a greeting and a farewell
def get_words(name):
    return "Hello " + name, "Goodbye " + name

greet, bye = get_words("Alex")
print(greet, bye)

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • This function takes one input but gives back two outputs.
  • return "Hello " + name, "Goodbye " + name makes two messages using the name.
  • The first message is a greeting, and the second is a goodbye.
  • greet, bye = stores the two results into two variables.

Test Incomplete

What is a function like in Python?

Question #

1/15

Score

0/0 - 0.0 %