Printing with Edward

We know that Edward is capable of both turning and navigating within his environment, as well as cleaning up trash and placing plants wherever we ask him to. Let’s guide him to where the trash is, remove it, and then place a plant there.

What if we added the ability for Edward to communicate with us as well? Wouldn’t it be great if Edward could provide feedback by displaying the state of each block on his path?

Communicating with Edward

We’ve added the functionality to print information that helps Edward communicate with us by displaying messages on the screen. Go ahead and interact with Edward below to see what print_info() doesYou can press the print button on any block. But press this button when Edward gets to the last block and after removing the trash and placing a plant.!

Getting feedback from Edward verifies the state of each block. This functionality of printing actually gives us the power, as a programmer, to verify what the computer (or Edward) has done for us. 

Press + to interact

Printing in Python

print() is also a function in Python that lets us print whatever (any message or the state of something) we want on the screen.

Let’s call the print() function in the widget below.

Press + to interact
print()

Hmm, not sure if it did anything. Let’s try again, but this time with what we want to display within quotation marks "" or '' inside its parentheses ().

Press + to interact
print("Hi there!")

Interesting, isn’t it? This function printed the exact text we gave it as input (in this case, Hi there!). Before we learn more about the print() function, let’s go back and talk about functions first.

More about functions

We know a function is like an action we want to perform in our code and that it can be called multiple times to perform that action more than once. But there’s more to it. Think of a function as a recipe that has ingredients (input), followed by a set of steps (some process), that results in a cooked meal (output). A function has three components: input, processing, and output.

Press + to interact
The function as a black box
The function as a black box

Let's see what these components are:

  1. A function takes zero, one, or more than one input(s). In programming, we call this input an argument.

  2. It does some processing (using the input, if any) to create some output.

  3. It returns the output.

Note: We don’t need to focus on how the processing is done inside the function right now.

To reiterate the above, let’s take another Python function, round, as an example. This function takes a fractional value (e.g., 3.1415) as input and rounds it off to the nearest whole number. The widget below helps us visualize how this function works. Provide it with an inputTry 3.14159265 or any number with a decimal point in it. and observe the output it produces when you press the round() button.

The round() function

Similarly, the print() function also takes an input. Now you might say functions turn(), move(), remove_trash() or place_plant() did not take any input. That’s because not all functions take input, but some do (and there can be more than one input!).

Back to the print() function

The text we give to the print() function is displayed as output. Click the “Run” button to execute the code below.

Press + to interact
print("Hello!")
print()
print('Nice to meet you!') # note that we can use signle quotations too

Lines 1 and 3 worked as expected, but what happened in line 2?

print() in line 2 is without any arguments and so prints nothing. It simply prints a blank line. This is why we didn’t see anything when we called the function above for the first time in the lesson. But it did print a blank line.

Now run the widget below.

Press + to interact
print("Here is some text)"

We get an errorAn error in programming is a mistake or issue in the code that prevents it from running correctly.. That’s because the closing parenthesis is within the quotation marks (single or double). This makes it a part of the text; therefore, it’s not detected as a closing parenthesis.

Python does not like it if you miss one quotation mark or a parenthesis. They come in pairs! So if you miss one, Python will raise an error.

We can also print multiple messages separated by commas through the print() function as show below. Go ahead and run the code to see its output.

Press + to interact
# comma separated multiple messages
print("Hello", "World")
print("Hello", "22")
print("Hello", 22)

In the widget above, the gray text in italics on line 1 is a comment. Comments are used to provide explanations, document the code, or leave notes for other programmers. They are not considered part of the executable code and are ignored by Python. Single-line comments start with #.

The print() function prints the text (whatever is placed inside the quotation marks " ") and the numbers as well. Notice that the numbers can be printed even without the quotation marks.

Test yourself

1

What is the output of the following Python code?

print("Python", "is", "fun!")
A)

Python

is

fun!

B)

Python is fun!

C)

It will raise an error.

Question 1 of 40 attempted

An exercise for you

Can you write a Python print statement that prints one of your favorite quotes? The quote can be from a movie or a book or perhaps even your own.

Press + to interact
# Print your favorite quote here.

Good job with the code above! Now let’s see what have we’ve learned in the lesson so far.

Recap

Key takeaways:

  1. The print() function: The print() function in Python is used to display output on the screen. It can take one or more arguments and print them on the screen.

  2. Function syntax: Functions in Python are called using the function name followed by parentheses containing the arguments (if any). For example, print("Hello") calls the print() function with the argument "Hello".

  3. Quotation marks: In Python, both single (') and double (") quotation marks can be used to define text.

  4. Arguments: In the context of functions, an argument is a value that is passed to a function within the parentheses. For the print() function, each value separated by commas inside the parentheses is considered an argument.

  5. Output: The print() function displays output on the screen.

Overall, the print() function is a fundamental tool in Python for displaying output and communicating information to the user.