Writing Your First Python Program
Let’s create and run your first Python program. This guide will help you write a simple script that prints “Hello, World!”—a classic way to start programming in any language.
Step 1: Write the Program
Option 1: Using a Text Editor
- Open any text editor, such as:
- Notepad (Windows)
- TextEdit (macOS, in plain text mode)
- nano or vim (Linux)
- Type the following code:
print(“Hello, World!”)
- Save the file with a .py extension, such as hello.py.
Option 2: Using an IDE or Code Editor
- Open an IDE (e.g., PyCharm, VS Code) or Python’s built-in IDLE.
- Create a new file and enter:
print(“Hello, World!”)
- Save the file as hello.py.
Step 2: Run the Program
Method 1: Command Line or Terminal
- Open a terminal (macOS/Linux) or Command Prompt (Windows).
- Navigate to the directory where you saved the hello.py file:
cd path/to/your/file
- Run the script by typing:
python hello.py
or for Python 3:
python3 hello.py
Output:
Hello, World!
Method 2: Using Python IDLE
- Open IDLE.
- Go to File > Open, then select hello.py.
- In the editor window, press F5 or go to Run > Run Module.
Output in IDLE:
Hello, World!
Method 3: Using an Online Interpreter
- Visit an online Python compiler, such as:
- Google Colab
- Paste the code:
print(“Hello, World!”)
- Click the Run button.
Explaining the Code
- The print() Function:
- The print() function displays the text or data inside the parentheses on the screen.
- In this case, “Hello, World!” is the string that gets printed.
- Double or Single Quotes:
- Python allows both single (‘) and double (“) quotes for strings:
print(‘Hello, World!’)
Experiment with Modifications
Try changing the message or adding new lines to expand your program:
print(“Welcome to Python programming!”)
print(“Let’s learn and explore together.”)
Output:
Welcome to Python programming!
Let’s learn and explore together.