Python Basics

Function in Python

Functions group reusable behavior behind a name and can accept inputs through parameters.

What is Function?

Functions group reusable behavior behind a name and can accept inputs through parameters.

Define and call a Python function with parameters and a return value.

When should you use it?

  • Learn Python syntax through a practical example.
  • Build a foundation for larger programming problems.
  • Test an idea quickly without a local setup.

Example code

Run code →
main.py
def greet(name, language="Python"):
    return f"Hello, {name}! Welcome to {language}."

message = greet("Grace")
print(message)

Expected output

Hello, Grace! Welcome to Python.

How it works

The function returns a formatted string rather than printing internally, allowing the caller to reuse the result elsewhere.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in Python IDE →