• Home
  • Python
    • Introduction to Python
    • Python Developer
    • Expert Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
    • Expert JavaScript Developer
  • React.js
    • Introduction to React
    • React Developer
    • Expert React Developer
  • Linux
    • CyberSecurity - Introduction to Linux
    • CyberSecurity - Linux Administrator
    • CyberSecurity - Expert Linux Administrator
  • Active Directory
    • CyberSecurity - Introduction to Active Directory
    • CyberSecurity - Active Directory Administrator
    • CyberSecurity - Expert Active Directory Administrator
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • Python
      • Introduction to Python
      • Python Developer
      • Expert Python Developer
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
      • Expert JavaScript Developer
    • React
      • Introduction to React
      • React Developer
      • Expert React Developer
    • Linux
      • CyberSecurity - Introduction to Linux
      • CyberSecurity - Linux Administrator
      • CyberSecurity - Expert Linux Administrator
    • Active Directory
      • CyberSecurity - Introduction to Active Directory
      • CyberSecurity - Active Directory Administrator
      • CyberSecurity - Expert Active Directory Administrator
  • Interactive Training
  • Pricing
  • Brainstorm

Quick Links

  • About Us
  • Pricing
  • Brainstorm

Courses

  • Python
    • Introduction to Python
    • Python Developer
    • Expert Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
    • Expert JavaScript Developer
  • React
    • Introduction to React
    • React Developer
    • Expert React Developer
    • Professional Master React Developer
  • Linux
    • CyberSecurity - Introduction to Linux
    • CyberSecurity - Linux Administrator
    • CyberSecurity - Expert Linux Administrator
  • Active Directory
    • CyberSecurity - Introduction to Active Directory
    • CyberSecurity - Active Directory Administrator
    • CyberSecurity - Expert Active Directory Administrator

Newsletter

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

© 2024 - 2026 STEMTrainingGrounds. All Rights Reserved.

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:

Loading...

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")

# 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:

Loading...

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

# 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:

Loading...

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 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:

Loading...

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.
Lesson Progress: 0%
Functions
Lesson Incomplete
Lesson 1 of 34
Next: DocStrings →