Welcome to the exciting world of programming with Python! In this lesson, you will learn how to communicate with your computer using a simple script to display messages on the screen.
In Python, the primary way to output information is the print() function. Think of this function as a command you give the computer to "show this text to the user." To use it, you write the word print followed by parentheses. Inside those parentheses, you place your message surrounded by either single quotes (') or double quotes (").
For example, to display "Hello, World!", you would write the following line of code:
print("Hello, World!")
When you run this script, the Python interpreter reads your command and formats the text onto the console window. It is the gold standard for beginners because it provides immediate visual feedback.
Tip: Experienced developers often use the
print()function for debugging—the process of checking the values of variables while a program is running to see if it behaves as expected.
In programming, text data is officially known as a string. You can think of a string as a sequence of characters strung together. Python is very specific about syntax, which is essentially the grammar of the programming language. If you forget to close a quote or leave out a parenthesis, the computer will raise a SyntaxError because it won't understand your instruction.
It is helpful to practice writing different types of strings. You can include spaces, punctuation, and even symbols. Python will faithfully reproduce exactly what is inside the quotes.
print("Python is fun!")
print('I am learning to code.')
If you ever need to include a quote inside your string, you can simply switch the wrapping quote type; for example, use single quotes on the outside if you need double quotes on the inside.
Now that you have written your command, you need to execute it. Usually, you save your code in a file ending with a .py extension (e.g., hello.py). Once saved, you run the terminal or command prompt by typing python hello.py.
The computer executes the script line by line from top to bottom. If you have multiple print statements, they will appear in the order you wrote them. This linear execution flow is the foundation for all complex software, from simple mobile apps to massive artificial intelligence models.
The print() function is the fundamental way to communicate information from your Python script to the user. Explain in your own words why Python requires specific syntax, such as matching quotes and parentheses, for the print() function, and describe how this requirement helps prevent errors during the execution of your code.