Printing with Edward
Give Edward the ability to communicate with us.
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()
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.
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.
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 ()
.
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.
Let's see what these components are:
A function takes zero, one, or more than one input(s). In programming, we call this input an argument.
It does some processing (using the input, if any) to create some output.
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 round()
button.
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.
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.
print("Here is some text)"
We get an
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.
# comma separated multiple messagesprint("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
What is the output of the following Python code?
print("Python", "is", "fun!")
Python
is
fun!
Python is fun!
It will raise an error.
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.
# 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:
The
print()
function: Theprint()
function in Python is used to display output on the screen. It can take one or more arguments and print them on the screen.Function syntax: Functions in Python are called using the function name followed by parentheses containing the arguments (if any). For example,
print("Hello")
calls theprint()
function with the argument"Hello"
.Quotation marks: In Python, both single (
'
) and double ("
) quotation marks can be used to define text.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.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.