DEV Community

Cover image for Your First Line of Code: A Beginner's Guide to Installing Python and Running "Hello World"
Aryan Jaswal
Aryan Jaswal

Posted on

Your First Line of Code: A Beginner's Guide to Installing Python and Running "Hello World"

Have you ever gazed at the incredible apps on your phone, the dynamic websites you browse, or the clever programs that automate tasks, and wondered, "How do they do that?" Every single one of these digital marvels starts with a single, humble line of code – a simple instruction that tells a computer what to do. Imagine unlocking the ability to speak this language, to bring your own ideas to life with just a few taps on your keyboard. Today, that exciting journey begins for you, a curious human poised to step into the captivating world of programming.

We're about to embark on an adventure with Python, a programming language celebrated for its clear, readable syntax and friendly nature. Think of it as learning a new language, but this one lets you communicate directly with computers, and Python is particularly welcoming to newcomers. It's a fantastic starting point into a universe of possibilities, allowing you to build everything from engaging websites and insightful data analysis tools to fun games and clever automated scripts. So, let's get you set up and prepare to write your very first line of code!

Getting Python on Your Machine

Before you can start whispering instructions to your computer in Python, you need to install Python itself. Consider it like downloading a special translator for your digital companion. The process is quite straightforward, whether you're using a Windows PC or a Mac.

For Windows Users:

Often, the simplest way to get Python on Windows is through the Microsoft Store.

  1. Open the Microsoft Store application. You can easily find it by typing "Microsoft Store" into your Start menu search bar.
  2. Within the store, search for "Python". You'll likely see several versions, such as "Python 3.10" or "Python 3.11". Always aim for the latest stable version available for the best experience.
  3. Click "Get" or "Install" and allow it to download and set up. This method often takes care of all the technical bits for you, including adding Python to your system's PATH, which makes it much easier to run Python commands later.

Alternatively, you can download the official installer directly from Python's official website:

  1. Navigate to the official Python website's downloads page specifically for Windows.
  2. Look for the "Python Releases for Windows" heading and click the link for the most recent Python 3 Release.
  3. Scroll down to the "Files" section and download the "Windows installer (64-bit)" (most modern computers operate on 64-bit).
  4. Once the download is complete, double-click the .exe file to begin the installation. A crucial step here: make sure to check the box labeled "Add python.exe to PATH" during the installation process. This step is absolutely vital for your computer to effortlessly locate and run Python commands.
  5. Then, simply click "Install Now" and follow any remaining on-screen prompts.

For macOS Users:

While your macOS device comes with a version of Python pre-installed, it's often an older version (Python 2.x) and not the ideal choice for contemporary development. It's always best to install the latest Python 3.x version.

  1. Visit the official Python website's downloads page tailored for macOS.
  2. Under the "Python Releases for macOS" heading, click the link for the Latest Python 3 Release.
  3. Scroll to the bottom of the page and click on the "macOS 64-bit universal2 installer" to download the installation package.
  4. After the download finishes, double-click the .pkg file and follow the clear on-screen instructions to run the installer. For most humans, the default settings will work perfectly.

Verify Your Installation:

After installation, let's just confirm that Python is all set and ready to obey your commands.

  1. Open your Command Prompt (on Windows, search for "cmd") or Terminal (on macOS, search for "Terminal").
  2. Type python --version and press the Enter key. If you installed Python through the Microsoft Store or correctly added it to your PATH during manual installation, you should see the Python version number displayed, something like "Python 3.11.5". If python doesn't yield a result, try typing python3 --version instead.

If you see a version number, congratulations! Python is successfully installed. If not, don't fret! Carefully re-check your installation steps, especially the "Add to PATH" part for Windows, or consider giving your computer a quick restart.

Your First Code: "Hello World"

Now for the truly exhilarating part: writing your very first program! This time-honored first step in programming, affectionately known as "Hello World," simply instructs your computer to display the words "Hello, World!" It might seem like a small action, but it's a giant leap into the world of code.

  1. Open a Simple Text Editor: You won't need any complex software for this. On Windows, use Notepad. On macOS, use TextEdit (you might need to go to "Format" > "Make Plain Text" to prevent rich text formatting from interfering).
  2. Type Your Code: In the blank document, carefully type the following exactly as it appears:

    print("Hello, World!")
    

    What does this magic do? print() is a special built-in Python command that instructs the computer to display whatever is placed inside the parentheses onto your screen. The text "Hello, World!" is enclosed within quotation marks because it's considered a "string" of characters – a literal piece of text that Python should output exactly as it is.

  3. Save Your File:

    • Click "File" > "Save As".
    • Choose a location where you can easily find it later, such as your Desktop or a new folder you create called MyPythonProjects.
    • Name the file hello.py. The .py extension is absolutely essential; it tells your computer that this is a Python file.
    • Ensure you select "All Files" or "Plain Text" for the "Save as type" option if you're using Windows Notepad, or "Make Plain Text" if using macOS TextEdit, to ensure it doesn't accidentally save as a .txt file.
  4. Run Your Program:

    • Open your Command Prompt (Windows) or Terminal (macOS) once more.
    • You need to navigate to the directory where you saved your hello.py file. For instance, if you saved it directly on your Desktop, you would type cd Desktop and press Enter. (If you saved it in a folder named MyPythonProjects on your Desktop, you would type cd Desktop\MyPythonProjects on Windows or cd Desktop/MyPythonProjects on macOS).
    • Once you're in the correct directory, type python hello.py and press Enter. (Again, if python doesn't work, try python3 hello.py).

    Voila! You should proudly see this appear in your command line:

    ``` Hello, World!

    
    
    You've done it! You've just given a computer a direct instruction, and it listened! Feel that spark? That's the beginning of something truly incredible.
    

What's Next?

Feeling that little surge of accomplishment? That's the undeniable magic of coding! You've successfully taken the first brave step into an entirely new world. This "Hello World" program, as simple as it appears, is the fundamental building block for everything else you'll ever create. Every complex piece of software, every groundbreaking application, started with a similar, humble beginning. Keep that delightful curiosity alive! Experiment by changing the message within your print() statement, save the file again, and rerun it. See what amazing new output you can conjure!

This is merely the kickoff of your coding adventure. Python is an incredibly powerful and versatile tool, and with each new line of code you write, you'll unlock more fascinating possibilities. Keep exploring, keep asking questions, and most importantly, keep coding!

Top comments (0)