After installing Python and setting up a development environment, you are ready to write your first Python program. Python programs are simple to write and easy to understand.
Let's create a simple Python program that displays the message Hello World!.
The print() function sends the specified value to the standard output, which is normally the screen or terminal.
Let's understand the different parts of our first Python program.
The print() function can display different text messages.
Each print() call normally displays its output on a new line.
The print() function can also display numbers.
Numbers do not need quotation marks when they are written as numeric values.
Python can perform calculations and display the result using print().
You can pass multiple values to the print() function by separating them with commas.
By default, print() separates multiple values with a space.
Python programs are normally saved in files with the .py extension.
Open your code editor and write the following code:
Save the file and then run it using the Python interpreter.
A Python file can be executed from the terminal.
Suppose your file is named first_program.py. Open the terminal in the same folder and run:
On systems where Python 3 is invoked using python3, use:
Python also provides an interactive mode. In interactive mode, you can enter Python commands one at a time and immediately see the result.
Interactive mode is useful for quickly testing Python expressions and small pieces of code.
Python strings can be written using single quotes as well as double quotes.
Both single and double quotation marks can be used to create strings.
The print() function can display variables along with text.
Python provides f-strings for inserting variable values directly into strings.
The f before the string allows expressions inside curly braces to be evaluated.
Let's create a simple program that displays student information.
Beginners often make small syntax mistakes when writing their first Python programs.
Try writing the following Python programs:
Which function is commonly used to display output in Python?