🚀 New batches open: Advanced Excel • Power BI • SQL • AI for Analytics — Book a free demo

Day 9 — Python functions with def

A function is a named piece of work you can reuse. The moment you copy and paste code, you wanted a function.

Defining and calling

Arguments

return — handing a value back

print and return are not the same print shows something to a human. return hands a value back to your code so you can use it further. A function that prints but does not return gives you None if you try to store it.

Several arguments

Default values

A function that makes a decision

Functions calling functions

Try these yourself

  1. Write a function that returns the square of a number.
  2. Write average(numbers) that takes a list and returns the mean.
  3. Write a function with a default discount of 10%.
  4. Write is_even(n) that returns True or False.

Ready for pandas and real projects?

This tutorial covers core Python. Our Data Analytics course takes you into pandas, NumPy, real files and visualisation, with mentor support and portfolio projects.

See the course →