+91 81900 98289

Python Functions

This Python Functions lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

A function packages steps under a name. def starts it. Call the function with parentheses.

Parameters receive values. return sends a value back. A function without return gives None.

Example 1: Start here

Keep a function on one job so the name stays honest.

def passed(score): # define the function; it runs when you call it return score >= 70 # send this value back to the caller print(passed(82)) print(passed(60))

Example 2: Define and call

The def line names the function. Nothing inside it runs until you call it.

def greet(name): # define the function; it runs when you call it print("Hello", name) # a comma prints each value, with a space between greet("Asha") # call the function

Example 3: Return a value

return sends a value back to the caller. print shows that value.

def double(n): # define the function; it runs when you call it return n * 2 # send this value back to the caller print(double(6))

Example 4: A default argument

If you omit the argument, the default is used.

def greet(name="learner"): # define the function; it runs when you call it return "Hello " + name # send this value back to the caller print(greet()) print(greet("Asha"))
Keep a function on one job so the name stays honest.
Call Now 81900 98289