Tutorial

How To Write Your First Python 3 Program

Updated on August 20, 2021
English
How To Write Your First Python 3 Program

Introduction

The “Hello, World!” program is a classic and time-honored tradition in computer programming. Serving as a simple and complete first program for beginners, as well as a good program to test systems and programming environments, “Hello, World!” illustrates the basic syntax of programming languages.

This tutorial will walk you through writing a “Hello, World” program in Python 3.

Prerequisites

You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

Writing the “Hello, World!” Program

To write the “Hello, World!” program, let’s open up a command-line text editor such as nano and create a new file:

  1. nano hello.py

Once the text file opens up in the terminal window we’ll type out our program:

hello.py
print("Hello, World!")

Let’s break down the different components of the code.

print() is a function that tells the computer to perform an action. We know it is a function because it uses parentheses. print() tells Python to display or output whatever we put in the parentheses. By default, this will output to the current terminal window.

Some functions, like the print() function, are built-in functions included in Python by default. These built-in functions are always available for us to use in programs that we create. We can also define our own functions that we construct ourselves through other elements.

Inside the parentheses of the print() function is a sequence of characters — Hello, World! — that is enclosed in quotation marks. Any characters that are inside of quotation marks are called a string.

Once we are done writing our program, we can exit nano by typing the control and x keys, and when prompted to save the file press y.

Once you exit out of nano you’ll return to your shell.

Running the “Hello, World!” Program

With our “Hello, World!” program written, we are ready to run the program. We’ll use the python3 command along with the name of our program file. Let’s run the program:

  1. python3 hello.py

The hello.py program that you created will cause your terminal to produce the following output:

Output
Hello, World!

Let’s go over what the program did in more detail.

Python executed the line print("Hello, World!") by calling the print() function. The string value of Hello, World! was passed to the function.

In this example, the string Hello, World! is also called an argument since it is a value that is passed to a function.

The quotes that are on either side of Hello, World! were not printed to the screen because they are used to tell Python that they contain a string. The quotation marks delineate where the string begins and ends.

Since the program ran, you can now confirm that Python 3 is properly installed and that the program is syntactically correct.

Conclusion

Congratulations! You have written the “Hello, World!” program in Python 3.

From here, you can continue to work with the print() function by writing your own strings to display, and can also create new program files.

Keep learning about programming in Python by reading our full tutorial series How To Code in Python 3.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


Tutorial Series: How To Code in Python

Python banner image

Introduction

Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. It is a great tool for both new learners and experienced developers alike.

Prerequisites

You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
1 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I can run the program but it immediately closes why does it do that

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel