Machine Learning For Kids: Python Functions

Machine learning for kids - Python functions


Machine learning for kids: Welcome to this tutorial on Python Functions! Have you ever wished you could reuse a piece of your code just like you reuse your favorite LEGO blocks? In Python, you can do this with something called ‘functions’. Functions are reusable pieces of code that perform a specific task.

Who Is This For?

Grade: 6th to 10th.

This tutorial is crafted for students in grades 6 to 10 who have basic knowledge of Python including variables, data types, conditionals, and loops. If you’re ready to take your coding to the next level by learning about code reusability and organization, then you’re in the right place!

Also Read: How Long Does It Take To Learn Python

What Will We Learn?

Machine learning for kids: In this tutorial, we’ll learn how to create our own functions in Python. We’ll see how functions can take inputs and produce outputs. We’ll also learn about the importance of comments in making our functions understandable to others and to ourselves when we revisit our code.

A function in Python is defined using the keyword def, followed by a function name, a pair of parentheses (), and a colon :. The code block within every function is indented. Functions can take parameters (inputs) and can return a value (output). Here’s the basic syntax of a Python function:

def function_name(parameters):
    """docstring (optional)"""
    # function body: your code goes here
    return output

function_name: A unique identifier to call the function later.

parameters (optional): Values that the function uses to perform a task.

docstring (optional): A brief description of what the function does. This is optional but recommended because it helps others (and future you) understand what your function does.

return (optional): The output that the function gives back.

Let’s write a function that greets a person:

def greet(name):
    """This function greets the person passed in as a parameter"""
    print("Hello, " + name + ". Good morning!")

# Now let's use our function

Here’s what the code does:

We start by defining our function with def greet(name):. The word greet is the name of our function, and name inside the parentheses is a parameter.

Inside the function, we have a docstring that briefly explains what our function does.

Then we have a line of code that prints a greeting message using the name parameter. This is the task our function performs.

After defining our function, we call it using greet("Alice") and greet("Bob"). When we call greet("Alice"), the function gets the string “Alice” as an input, and it prints “Hello, Alice. Good morning!”. When we call greet("Bob"), it prints “Hello, Bob. Good morning!”.

So, the output of our program will be:

Hello, Alice. Good morning!
Hello, Bob. Good morning!

That’s it! You’ve learned how to write and use your own functions in Python. Functions are a great way to organize and reuse your code. They make your programs easier to write, read, test, and fix. Keep practicing and stay tuned for the next tutorial!


Tutorial 1 – Installing Python
Tutorial 2 – Your First Program in Python
Tutorial 3 – Python Variables
Tutorial 4 – Python Data Types
Tutorial 5 – Python Conditionals
Tutorial 6 – Python Loops
Tutorial 7 – Python Functions
Tutorial 8 – Advanced Python Functions
Tutorial 9 – Starter Machine Learning Python Program
Tutorial 10 – Your First Machine Learning Program!

Explore more with this book.

Python for Kids, 2nd Edition: A Playful Introduction to Programming
Buy Now
We earn a commission if you make a purchase, at no additional cost to you.
09/17/2023 12:41 am GMT


Briggs, Jason R. Python for Kids: A Playful Introduction To Programming. No Starch Press, 2012.