Now that you have successfully installed Python and set up your IDE, it is time to write your first Python program. In the programming world, it is a tradition to start by printing the message “Hello, World!” on the screen.
Python provides two different ways to run a program:
- Interactive Mode (Running code line-by-line in the terminal)
- Script Mode (Writing code in a file and executing it)
Let us explore both methods.
Method 1: Using Interactive Mode
Interactive mode is useful for testing short snippets of code quickly. It executes one line of code at a time and immediately shows the output.
Steps to Run Code in Interactive Mode:
- Open your Command Prompt (Windows) or Terminal (macOS/Linux).
- Type
pythonand press Enter. You will see the Python version and a prompt looking like>>>. This means the Python shell is active. - Type the following code next to the
>>>prompt and press Enter:
print("Hello, World!")
Code language: PHP (php)
Output:
Hello, World!
Note: To exit the interactive mode, type exit() and press Enter.
Method 2: Using Script Mode (Recommended)
While interactive mode is good for quick testing, you cannot save your code there. For actual software development, we write our code in a file and save it with a .py extension. This is called Script Mode.
Steps to Run Code in Script Mode:
- Open your code editor (like VS Code or PyCharm) or a basic text editor like Notepad.
- Create a new file and name it
hello.py. The.pyextension is mandatory for Python files. - Write the following code inside the file:
# This is our first Python program
print("Hello, World!")
Code language: PHP (php)
- Save the file.
- Open your terminal or command prompt, navigate to the folder where you saved
hello.py, and run the following command:
python hello.py
Code language: CSS (css)
Output:
Hello, World!
Understanding the Code
Let us break down the simple one-line code we just wrote to understand how Python works:
# This is our first Python program: The#symbol is used to write comments. The Python interpreter ignores comments during execution. They are used to make the code easier to understand for humans.print(): This is a built-in Python function. Its job is to display whatever is placed inside the parentheses()onto the screen."Hello, World!": The text enclosed in double quotes (or single quotes) is called a String. A string is simply a sequence of characters.
Common Mistakes Beginners Make
When writing your first program, you might encounter some errors. Here are the most common mistakes and how to fix them:
1. Missing Parentheses
In Python 3, the print statement requires parentheses. Wrong: print "Hello, World!" Error: SyntaxError: Missing parentheses in call to 'print'. Correction: Always use print("...").
2. Missing Quotes
If you are printing text, it must be inside quotes. Wrong: print(Hello, World!) Error: SyntaxError: invalid syntax Correction: Ensure text is enclosed in " " or ' '.
3. Case Sensitivity
Python is a case-sensitive language. Uppercase and lowercase letters are treated differently. Wrong: Print("Hello, World!") (Capital ‘P’) Error: NameError: name 'Print' is not defined Correction: Always use lowercase print().
4. Unnecessary Spaces (Indentation Error)
In Python, spaces at the beginning of a line have a special meaning. Wrong: “`python print(“Hello, World!”) # Notice the space at the start
**Error:** `IndentationError: unexpected indent`
**Correction:** Make sure the `print` statement starts exactly at the beginning of the line without any leading spaces.
Code language: JavaScript (javascript)
