Skip to the content

Scope

Python · Unit 4: Functions

The variables you create inside a function are local: they are born when you call it and disappear when it ends.

Outside they don't exist. What does come out is the value of the return.

def calculate():
    total = 5 * 3
    return total

result = calculate()
print(result)

Prints

15

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Find the bug

    This program should print the price of 100 with the discount. Which line has the error?

  2. 2. Predict the output

    What does this code print?

  3. 3. Multiple choice

    Where can the variable total be used?

  4. 4. Predict the output

    What does this code print?

  5. 5. Complete the code

    Fill in the blank so it prints what 3 tickets cost.

  6. 6. Predict the output

    What does this code print?

  7. 7. Find the bug

    This program should add a deposit of 5 to balance and then print the new balance: 15. Which line has the error?

  8. 8. Put the lines in order

    Put in order to print the total of 70 plus 5 for shipping.

  9. 9. Predict the output

    What does this code print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →