The first program that beginners write in any programming language is the "Hello, World!" program. It is a simple way to test if Python is installed correctly and to understand basic syntax.
Writing "Hello, World!" in Python
To print "Hello, World!" in Python, follow these steps:
-
Open a Python Environment
- You can use the Python IDLE, a text editor, or an IDE like VS Code or PyCharm.
- Alternatively, open a terminal (Command Prompt, Terminal, or PowerShell).
-
Write the Python Code
print("Hello, World!") -
Run the Python Code
- If using an IDE, save the file as
hello.pyand run it. - If using the command line, navigate to the file location and run:
python hello.py - You will see the output:
Hello, World!
- If using an IDE, save the file as
Understanding the Code
print()→ This is a built-in Python function used to display text on the screen."Hello, World!"→ The message inside the parentheses is called a string and should be enclosed in double ("") or single ('') quotes.
Running "Hello, World!" in an Interactive Shell
You can also run this program directly in the Python interactive shell:
-
Open a terminal or command prompt.
-
Type
pythonorpython3to start the interactive mode. -
Type the following and press Enter:
print("Hello, World!") -
You will see:
Hello, World!
Advanced "Hello, World!" Examples
-
Storing in a Variable:
message = "Hello, World!" print(message) -
Using String Formatting:
name = "Nitin" print(f"Hello, {name}!") -
Using Multi-line Strings:
print("""Hello, World!""")
Conclusion
The "Hello, World!" program is a simple yet essential step for beginners to get started with Python. Once you have successfully run this program, you are ready to explore more Python concepts! π