Write Your First Python 3 Program

Guidance

By Chandrashekhar Fakirpure

Updated on Feb 07, 2024

In this tutorial, we'll explain how to write a your first Python 3 program. It is the great way to start learning programming. Here is a simple guide to help you.

Let's learn how to print "Hello world!" the classic tradition in the programming language.  It illustrates the basic syntax of programming languages. Also it will help us to test the system environments.

Install Python 3

If you haven't installed the Python 3, follow these steps. If you have installed Python3 in your system, you can skip this step. 

For this demonstration purpose, we are installing Python 3 using package manager. You may get older version but it is simple to install. We are installing Python 3 in Linux based system, if you have Windows based system, you can download Python 3 installer .exe file from official website and install it.

For Ubuntu/Debian based system:

sudo apt install python3 python3.10-venv

For Redhat/Fedora based system:

sudo dnf install python3 

Create Python 3 Virutal Environment (Optionally)

It is recommended to create a Python 3 virtual environment before executing Python 3 programs but is not required part, you can run Python 3 program without virtual environment. It is the best practice. If you habit yourself, it will benefit for the future.

python3 -m venv env

Next, activate the virtual environment using following command:

source env/bin/activate

The virual envroinment is activated now.

Write your first Python program

Let's start with a classic first program, the Hello World! program. Open your favorite text editor or IDE and create file name hello_world.py.

nano hello_world.py

Copy and paste following content in the file:

print("Hello, World!")

In this program, print() is a built-in Python function that outputs the text "Hello, World!" to the console. Save and exit

Run your Python program

Open your system terminal and navigate to the directory where your Python file is saved and execute following command:

python hello_world.py

If you are not using virutal enviornment, use python3 instead of python in the above command. You should see the output "Hello, World!" printed to the terminal.

Experiment and Learn:

Congratulations, you have successfully run your first Python program. Now, try to experiment with it. Like printing different test or different Python's syntax and learn about its features. We have covered wide range of tutorials and documentations to help you learn Python programming.

Remember, the key to learning Python programming is practice and persistence. Keep exploring and seeking help when needed.

The chapter is completed. We have seen how to write a your first Python 3 program.