Basic

Passing different kinds of values

A parameter can hold any kind of value: a number, text, or a list. The body works on whatever kind you pass, as long as the steps fit that kind.

What you will see

TypeError

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Pass numbers, text, and lists to functions.

1

Define a function that uppercases text.

def upper(text): return text.upper() print(upper("owl"))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
OWL
2

Define one that takes the first item of a list.

def first(items): return items[0] print(first([7, 8, 9]))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
7
3

Use len on both a list and text.

def size(items): return len(items) print(size([1, 2, 3])) print(size("abc"))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
3 3
4

Give a text function a number and watch it fail.

def shout(text): return text + "!" print(shout(5))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
TypeError
The plus expects text. Passing a number is a kind mismatch that only shows when the call runs.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A parameter can hold any kind of value, and the body must use steps that fit the kind you pass.
1 parameter holds text
↓
2 a text method runs
↓
3 a list can be passed too
↓
4 a wrong kind fails at run time
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

def upper(text): sets nothing on screen
return text.upper() sets nothing on screen
print(upper("owl")) OWL
Why this works
The kind is not declared

In Python a parameter does not say its kind. The kind is decided by what you pass. That is flexible but means a wrong kind shows up only when the function runs.

Steps decide the kind

A function works on whatever kind its steps accept. Writing steps that only use kind-safe operations, like len, lets one function serve several kinds.

Examples

Text in, text out

def upper(text):
    return text.upper()

print(upper("owl"))

The parameter holds text, so a text method works on it. The function works for any string you pass.

A list in

def first(items):
    return items[0]

print(first([7, 8, 9]))

A list can be an argument too. The body indexes it, which is a step a list supports.

Kind must fit the step

def shout(text):
    return text + "!"

print(shout("hi"))

The plus here joins text. The same function given a number would fail, because number plus text is not allowed.

Count a list

def size(items):
    return len(items)

print(size([1, 2, 3]))
print(size("abc"))

len works on a list and on text, so this function answers both. A step that fits several kinds makes a flexible function.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Parameters and arguments
Previous Lesson
Parameters and arguments
Next Lesson
Putting calls inside calls
Putting calls inside calls →