The "Hello, World" program in Python is a simple program that displays the text "Hello, World!" on the console or output device. It serves as a starting point for beginners to understand the fundamental concepts and terms of Python programming.
Your Very First Program
To write the "Hello, World" program, you can either use an integrated development environment (IDE) like IDLE or run the code in an editor window. The program consists of the following parts:
print("Hello, World!")
In this code snippet, we use the print() function to display the text "Hello, World!" on the console.
The print() Function
In Python, the print keyword is a function name. Functions in Python are separate parts of the code that can cause some effect or evaluate a value. The print() function is a built-in function in Python that sends text or data to the output device, usually the console.
Function Arguments
Functions in Python can have arguments, which are values or data passed to the function for it to perform its task. The print() function can accept any number of arguments, including zero. In the "Hello, World" program, the print() function takes a single argument, which is the string "Hello, World!" enclosed in quotation marks.
Function Invocation
Invoking a function means calling and executing it. In the "Hello, World" program, invoking the print() function with the string argument causes it to display the text on the console. Python checks if the function exists, validates the number of arguments, executes the function's code, and returns to the next line of code.
Working with the print() Function (Lab)
Let's practice using the print() function with a lab exercise. Try running the following code snippets and observe their outputs:
print("Hello, Python!")
name = "John"
print("Hello, " + name + "!")
print("Hello,", "Python!")
The print() Function and Its Effect, Arguments, and Values Returned
The print() function has the following characteristics:
- Effect: It sends the arguments to the output device, displaying them on the screen.
- Arguments: The print() function can accept any type of data offered by Python, including strings, numbers, characters, logical values, and objects.
- Returned Value: The print() function does not return any value; its effect is enough.
Instructions
Python's syntax requires each line to contain only one instruction. Multiple instructions cannot be placed on the same line. However, a single instruction can spread across multiple lines using line continuation. The "Hello, World" program demonstrates this by using two separate print() function invocations on different lines.
Python Escape and Newline Characters
Python uses the backslash (\) as an escape character within strings. The backslash followed by certain characters forms escape sequences, representing characters that are difficult to type or encode directly. For example, the escape sequence \n represents a newline character, which causes the text to move to a new line.
print("Hello, World!\nWelcome to Python!")