Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today we're going to learn how to write our very first Python program using the print() function. This function is essential because it allows us to output text to the screen. Let's start with a basic example: print("Hello, World!"). Does anyone want to predict what will happen when we run this code?
I think it will just show 'Hello, World!' on the screen.
Yes! That sounds correct because the text is in quotes.
Exactly! The quotes indicate that this is a string. Remember, strings are sequences of characters. Can anyone tell me why we use parentheses in the print function?
Is it because it's a function? Functions usually require parentheses.
Correct! Functions use parentheses to contain their arguments, which in this case, is the string we want to print. Good job, everyone!
Now that we've written our program, let’s discuss how to run it. You can write this code in a .py file using a text editor, or try it in an online IDE. Who can tell me what should happen when we run the program?
It should display 'Hello, World!' on the screen, just like we predicted before.
And if we don't see that output, something might be wrong!
Exactly! It's important to check for errors if the output isn’t what we expect. Remember, programming is about troubleshooting and learning from mistakes. Let's practice by running this in pairs.
Great job running your first program! Now let’s explore more examples with print. For instance, what if we wanted to print your names? What code would we write?
We would write something like print("My name is Alex.")
And I could change it to print("My name is Jamie.")
Exactly! Now each of you can personalize your output. This ability to modify and reuse code is central to programming. Let's come up with another example—how about a math result?
When we run our print command, we produce output on the console. What's vital here is understanding that this output is precisely what you asked the program to display. Why do you think this is important?
It helps us verify that our program ran correctly.
If the output isn't what we expected, we know we need to debug.
Well said! The output is your program's feedback. A good practice is to always expect a specific outcome, so let’s write a program that adds numbers and prints them as well. Can anyone suggest an addition we could try?
To print and display, in code we must say, add quotes with pride, to let strings reside.
Once upon a time in a coding land, there was a magic function called print. Whenever it was cast with words in quotes, it shared those words with the world, cheering joy and excitement each time it displayed a message.
P for Print, R for Run; if it’s in quotes, you’ve surely won!
P.S.S. - Print,String,Screen. Remember: To put a string on the screen, use print!
Term
What does print() do?
Click to flip
Term
What is a string?
Click to flip
This section introduces learners to writing and running their first Python program using the print() function to display text.
In this section, learners discover how to write a simple Python program that utilizes the print() function to output text. They explore the structure of the code and its components, including strings and functions, while also evaluating the expected output when the code is executed.
In this section, we delve into writing your first program in Python by utilizing the built-in print() function. The example provided demonstrates how to produce a simple message, "Hello, World!", to the console.
Writing a simple program like this serves as a foundational step for beginners, as it not only familiarizes them with the syntax but also demonstrates how code can be used to produce results in a straightforward manner. Furthermore, the output of the program is essential feedback, affirming that the code is functioning correctly. In future sections, we will build upon this knowledge to explore more complex programming concepts.