Web Hosting Monkey
menu icon

Python Hello World

Updated:

This is a beginner’s guide to writing and running a Hello World program in Python on various platforms. Python is a versatile and powerful programming language that is widely used for various purposes such as web development, data analysis, artificial intelligence, and more. In this comprehensive tutorial, we will walk you through the process of setting up Python on different operating systems and writing your first “Hello World” program on Windows, Mac, Linux, iOS, and Android.

Setting Up Python Environment

Windows

  1. Visit the official Python website at python.org/downloads.
  2. Download the latest version of Python for Windows.
  3. Run the installer and follow the on-screen instructions.
  4. Make sure to check the box that says “Add Python to PATH” during installation.

Here is a detailed tutorial on How to Install Python on Windows.

Mac

MacOS typically comes with Python pre-installed. To check if Python is installed on your macOS system and to see the version, you can open the Terminal application and type:

python --version

If you don’t have Python on your Mac, follow these steps to download and install Python:

  1. Open a web browser and navigate to python.org/downloads.
  2. Download the latest version of Python for macOS.
  3. Double-click the downloaded file to launch the installer.
  4. Follow the installation prompts, ensuring to check the box that says “Add Python to PATH”.

Linux

Linux usually comes with Python installed. You can check the version of Python with the following command.

python --version

If you linux system doesn’t have python, you can follow the instructions below to install Python.

For Linux desktops, open a terminal window. For Linux servers on the cloud, connect to your server via SSH.

Run the following command to install Python:

On Red Hat-based Linux distributions:

sudo dnf install python3

On Debian-based Linux distributions:

sudo apt-get install python3

iOS (iPad and iPhone)

  1. Download and install the “Pythonista 3″ app from the App Store.
  2. Launch Pythonista 3 and start coding!

Android

  1. Install a Python interpreter app from the Google Play Store, such as Pydroid 3.
  2. Launch the app and begin programming in Python.

Writing and Running Your First Python Program

As a Python File (.py File)

Open a text editor (such as Notepad on Windows, TextEdit on macOS, or any code editor of your choice) and type the following code:

print("Hello World!")

Save the file with a .py extension, for example, hello.py.

Open a terminal or command prompt, navigate to the directory where you saved the file, and run the following command:

python hello.py

You should see “Hello World!” printed to the console.

As a System Command Line

To execute Python code directly from the system command line, open a terminal or command prompt and enter the following command:

python -c "print('Hello World!')

Press Enter, and you should see “Hello World!” printed to the console.

As a Python Command Line

To run Python code interactively from the command line, open a terminal or command prompt and enter the following command to enter the Python runtime environment:

python

You will then see the Python prompt, indicated by three arrows (>>>). Enter the following command:

print('Hello World!')

Press Enter, and you should see “Hello World!” printed to the console.

Writing and Running “Hello World” using Integrated Development Environment (IDE)

Now that you have finished writing and running your “Hello World” program in a Python runtime environment, consider using an Integrated Development Environment (IDE), such as VS Code, for more advanced Python programming. IDEs offer more features and capabilities compared to simple editing tools like Notepad and TextEdit. You can read this tutorial on how to set up Visual Studio Code (VS Code) for Python.

Conclusion

Congratulations! You’ve successfully set up Python on your system and written your first Python program. This “Hello World” example is just the beginning of your journey into the exciting world of Python programming. Stay curious, keep learning, and explore the endless possibilities that Python has to offer!