Sitelet https://howtodoinjava.com/python-examples/python-hello-world/

Python Hello World Program: Write and Run Your First Script

Write and run the Python Hello World program from a file and the interactive prompt, fix common first-run errors, and add a main block to the script.

Python

A Python Hello World program is a single line, print(“Hello, World!”), saved in a .py file and run with the Python interpreter. Getting it to run proves that Python is installed and that the terminal can find it.

print("Hello, World!")

In this tutorial, we run the program from a file and from the interactive prompt, fix the errors beginners hit most often, and grow the one-liner into a script with an if __name__ == “__main__” block. Commands and outputs come from Python 3.14.

1. Write and Run the Program

We save the line above in a file named hello.py and run it from a terminal in the same folder. The command name depends on the operating system.

python --version      # Python 3.14.6 (Windows: py --version, macOS/Linux: python3 --version)
python hello.py       # Windows: py hello.py, macOS/Linux: python3 hello.py
Hello, World!

On Windows, the py launcher (installed by the Python Install Manager) picks the newest installed Python. On macOS and Linux, python may be missing or may point to an old version, so python3 is the safer command.

2. Run It in the Interactive Prompt

Running python (or py, or python3) without a file name opens the interactive prompt, shown as >>>. It runs each line as soon as we press Enter, which is handy for trying out code.

>>> print("Hello, World!")
Hello, World!
>>> exit

Since Python 3.13, typing exit or quit without parentheses closes the prompt. Older versions need exit().

3. Common Errors

Almost every failed first run comes down to one of four mistakes. The first one is about the PATH, the list of folders where the terminal looks for programs.

What we seeWhyFix
‘python’ is not recognized or command not foundPython is not installed or not on the PATHUse py on Windows or python3 elsewhere, or reinstall with PATH enabled
SyntaxError: Missing parentheses in call to ‘print’Python 2 syntax, print “Hello”Write print(“Hello”)
SyntaxError: invalid syntax at python hello.pyThe command was typed inside the >>> promptLeave the prompt with exit and run it in the terminal
can’t open file ‘hello.py’The terminal is in a different foldercd to the folder that holds the file

4. A Hello World Script with a main Block

Real scripts put their code in functions and start from an if __name__ == “__main__”: block. That block runs only when we run the file, not when another file imports it. The sep and end arguments of print() control what goes between and after the values.

def greet(name):
    return f"Hello, {name}!"


if __name__ == "__main__":
    print(greet("World"))
    print("Hello", "World", sep=", ", end="!\n")
Hello, World!
Hello, World!

5. A Hello World That Asks for a Name

A fixed message is the smallest program; reacting to the user is the next small step. The input() function reads a line, and an f-string puts the value into the message.

name = input("What is your name? ").strip() or "World"
print(f"Hello, {name}!")
What is your name? Lokesh
Hello, Lokesh!

The or “World” part gives a default when the user presses Enter without typing anything, because an empty string counts as false.

6. Running the Program in an IDE

An editor saves us from typing commands. In VS Code with the Python extension, we open hello.py and click the Run button in the top-right corner, and in PyCharm we right-click the file and choose Run. Both show the output in a panel at the bottom and use the interpreter selected for the project, so the interpreter version shown there is the one that runs the code.

7. Troubleshooting the First Program

A first script that refuses to run is almost always an operating system issue, not a Python one.

7.1. Why does the window close at once when we double-click the file on Windows?

Windows opens a console, runs the script and closes the console when the script ends. Running the file from a terminal keeps the output visible, or we add input(“Press Enter to exit”) as the last line.

7.2. Do we need a main function in Python?

No. Python runs a file from top to bottom, so print(“Hello, World!”) alone is a complete program. The if __name__ == “__main__”: block from section 4 is a convention that becomes important once other files import our code.

7.3. How do we run the script without typing python first?

On Linux and macOS, we add the line #!/usr/bin/env python3 at the top, run chmod +x hello.py once, and start the script with ./hello.py. On Windows, the py launcher handles .py files.

8. Conclusion

Once print(“Hello, World!”) runs from a file, our setup works, and every later script runs the same way. Good next steps are the input() function and reading values from the user.

9. References

Happy Learning !!

Source Code on Github

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.