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
- Launch PyCharm on your computer.
- Create a new project by selecting "File" > "New Project".
- Choose a location for your project and click "Create".
Step 2: Create a New Python File
- Right-click on the project folder in the Project Explorer.
- Select "New" > "Python File".
- 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
- Right-click on the
hello.py
file in the Project Explorer. - Select "Run ‘hello’".
- 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
- Launch Visual Studio Code on your computer.
- Open a folder for your project by selecting "File" > "Open Folder".
- Choose a location for your project and open the folder.
Step 2: Create a New Python File
- Click the "New File" icon or select "File" > "New File".
- 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
- Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
- Type
Run Python File in Terminal
and select it. - 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.