Write and Run Your First Python Program

Introduction

Now that you have set up your Python development environment, it’s time to write and run your first Python program. In this chapter, we will guide you through creating a simple Python script and executing it using PyCharm and Visual Studio Code (VS Code).

Writing and Running Your First Python Program

Using PyCharm

Step 1: Open PyCharm

  1. Launch PyCharm on your computer.
  2. Create a new project by selecting "File" > "New Project".
  3. Choose a location for your project and click "Create".

Step 2: Create a New Python File

  1. Right-click on the project folder in the Project Explorer.
  2. Select "New" > "Python File".
  3. Name the file hello.py.

Step 3: Write Your Python Program

In the hello.py file, type the following code:

print("Hello, World!")

Step 4: Run Your Python Program

  1. Right-click on the hello.py file in the Project Explorer.
  2. Select "Run ‘hello’".
  3. The output "Hello, World!" should appear in the Run tool window at the bottom of the screen.

Using VS Code

Step 1: Open VS Code

  1. Launch Visual Studio Code on your computer.
  2. Open a folder for your project by selecting "File" > "Open Folder".
  3. Choose a location for your project and open the folder.

Step 2: Create a New Python File

  1. Click the "New File" icon or select "File" > "New File".
  2. Save the file as hello.py.

Step 3: Write Your Python Program

In the hello.py file, type the following code:

print("Hello, World!")

Step 4: Run Your Python Program

  1. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
  2. Type Run Python File in Terminal and select it.
  3. The terminal at the bottom of VS Code should display the output "Hello, World!".

Conclusion

Congratulations! You’ve just written and run your first Python program using PyCharm and VS Code. This simple "Hello, World!" script is the traditional first step in learning a new programming language.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top