1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

print vs return

The #1 beginner trap: print shows a value on screen, while return hands it back to whoever called the function.

A function without return gives back None:

def bad(n):
    print(n * 2)    # shows it... then returns None
x = bad(5)          # x is None!

See it run

def shout(word):
    print(word.upper())

def loud(word):
    return word.upper()

a = shout("hi")
b = loud("hi")
print(a, b)

Two functions do the same work. Only one hands the result back.

Practice print vs return

Exercise 1

In the code editor below, double prints its answer instead of returning it, so result ends up None. Fix it so result is 10.

Exercise 2

Fix discount(price) so it returns the price with 20% off: discount(50) is 40.0. Showing the number with print inside the function is not enough. Prices are zero or more.

Exercise 3

Separate a conversion from its display: to_minutes(hours) returns hours * 60 and prints nothing. show_duration(hours) prints Duration: N min using that function and returns None. For whole-hour input, show an integer N. Set minutes to to_minutes(2), then call show_duration(2).

Exercise 4

Predict, then verify. Read this program before running it:

def shout(word):
    print(word.upper() + "!")
result = shout("hi")

What value does result hold? Guess, then run it, press Step through your code, and press Next until the label says Returning from shout(): the box shows what the call hands back. Set prediction to that value.

The explanation and tasks are public. Running this lesson requires an account with course access.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest