Related Tutorial

4- Writing Your First Python Program

Introduction

Welcome to your first step in Python programming! In this tutorial, we will guide you through writing a simple Python program. This exercise will help you get familiar with the basic syntax and structure of Python code. You can use any text editor for this task, but we recommend Visual Studio Code (VS Code). Other good options include Sublime Text, PyCharm, or Notepad++ for Windows users.

Setting Up Your Text Editor

If you don’t already have a text editor, pause here and install one. A text editor is an essential tool for programming in any language. Once you’re set up, create a new file named hello.py. The .py extension indicates that this is a Python file.

Writing the Code

Open hello.py in your text editor and type the following line of Python code:

				
					print('Hello, World!')

				
			

This line uses the print function to display text on the screen. The text “Hello, World!” is enclosed in single quotes. These quotes tell Python that you are writing a string of text. It’s important to use quotes correctly to avoid syntax errors.

Adding Comments

Comments are an essential part of programming. They help you and others understand what your code does. In Python, comments start with a hash symbol (#). Let’s add a comment to our program:

				
					# Print Hello, World! to the terminal
print('Hello, World!')

				
			

This comment explains that the line of code prints “Hello, World!” to the terminal. Comments are ignored by the Python interpreter, so they won’t affect how your program runs.

Saving the File

After writing your code and comments, save the file. Make sure it is saved with the .py extension.

Running Your Program

To run your Python program, you need to use the terminal or command prompt. Here are the steps:

  1. Open the Terminal:

    • Windows: Press Win + R, type cmd, and hit Enter.
    • Mac: Press Cmd + Space, type Terminal, and hit Enter.
    • Linux: Open your terminal application (varies by distribution).
  2. Navigate to Your File’s Directory: Use the cd command to change the directory to where your hello.py file is stored. For example, if your file is in the Documents folder, you would type:

				
					cd Documents

				
			

Run the Program: Type the following command to run your Python file:

				
					python hello.py

				
			

On some systems, you might need to use python3 instead of python:

				
					python3 hello.py

				
			

Example

Here’s a complete example to illustrate the process:

  1. Write the Code:

				
					# Print Hello, World! to the terminal
print('Hello, World!')

				
			
  1. Save the File: Save it as hello.py.

  2. Open the Terminal and Navigate to the File’s Directory:

				
					cd Documents

				
			

Run the Program:

				
					python hello.py
				
			

You should see the output:

				
					Hello, World!
				
			

Conclusion

Congratulations on writing and running your first Python program! This simple exercise introduces you to the basics of Python syntax and the use of a text editor. As you continue to learn Python, you’ll build on this foundation to create more complex programs. Happy coding!