Learn
Introduction to Functions
Whitespace

Consider this function:

def greet_customer(): print("Welcome to Engrossing Grocers.") print("Our special is mandarin oranges.") print("Have fun shopping!")

The three print statements are all executed together when greet_customer() is called. This is because they have the same level of indentation. In Python, the amount of whitespace tells the computer what is part of a function and what is not part of that function. If we wanted to write another line outside of greet_customer(), we would have to unindent the new line:

def greet_customer(): print("Welcome to Engrossing Grocers.") print("Our special is mandarin oranges.") print("Have fun shopping!") print("Cleanup on Aisle 6") greet_customer() greet_customer()

When we run this program, the message "Cleanup on Aisle 6" will be printed once, while the messages in greet_customer() will all be printed twice. This is because we call the function twice, and "Cleanup on Aisle 6" is not part of the function. Notice also that "Cleanup on Aisle 6" will be printed before the greet_customer() messages since we call the function after it. We would see the following output from this program:

Cleanup on Aisle 6 Welcome to Engrossing Grocers. Our special is mandarin oranges. Have fun shopping! Welcome to Engrossing Grocers. Our special is mandarin oranges. Have fun shopping!

Here at Codecademy, we use 2 spaces for our default indentation. Anything other than that will throw an error when you try to run the program. Many other platforms use 4 spaces. Some people even use tabs! These are all fine. What is important is being consistent throughout the project.

Instructions

1.

Run script.py. Look at what is printed out!

2.

Remove the indent on the second print statement. Run the file. Now what’s printed?

Folder Icon

Sign up to start coding

Already have an account?