Improve your skills with Exercise 16: Defining Functions | HolyPython.com (original) (raw)

Python User Function Exercises

Let’s check out some exercises that will help understand “Defining Functions” better.

Exercise 16-a

Define a function named f_1 which will print "Hello World!"

The syntax for defining functions is:

def FunctionName ():

After your 1st line of function definition your statement needs to be indented either as:

  1. 1 Tab key
  2. 4 Space characters.

def f_1(): print("Hello World!")

Exercise 16-b

Now define the same function f_1 and assign it to variable ans_1. See what happens.

The syntax for defining functions is:

def FunctionName ():

After your 1st line of function definition your statement needs to be indented either as:

  1. 1 Tab key
  2. 4 Space characters.

Exercise 16-c

Now define the same f_1 function this time so that it returns a value instead of just printing it..

return keyword will allow your function to return a value.

def f_1(): return "Hello World!"

Need More Exercises?

Check out Holy Python AI+ for amazing Python learning tools.

*Includes 14 more programming languages, inspirational tools and 1-on-1 consulting options.

Umut Sagir

Finance & Data Science Professional,
Founder of HolyPython