Getting Started with Python: Write and Run Your First Program

Tuesday, November 19, 2024 by TommyA
Getting Started with Python: Write and Run Your First Program

Getting Started with Python: Write and Run Your First Program

Introduction

Hello! Tommy here, and today I’m excited to introduce you to Python and Visual Studio Code (VS Code)! This tutorial will guide you through installing Python, setting up VS Code as your code editor, and writing your first Python program. Don’t worry if you’re new to coding; I’ll make this as simple and fun as possible.

By the end of this tutorial, you’ll have Python and VS Code set up and will have run your first Python script—a little program that says, “Hello, World!” It’s the perfect starting point for your programming journey.

Whether you’re curious about coding or ready to explore Python's magic, this guide will walk you through the process in a way that’s beginner-friendly and accessible. Let’s dive in and start coding! 🎉

Step 1: Installing Python

Python is the programming language we’ll use. It’s powerful yet beginner-friendly, making it a great choice to start your coding adventure.

Steps to Install Python:

  1. Download Python:

    • Visit the Python download page.
    • Click the Download Python button. The website will automatically detect the right version for your operating system.
      python download page
      Python download page
  2. Install Python:

  • Locate the downloaded file in your Downloads folder and double-click it to start the installation.

    downloading python
    Downloading Python
  • Follow the on-screen instructions, and be sure to check the box that says "Add Python to PATH" during the setup process.

3. Verify Installation:

  • For macOS Users:
    • Open your Terminal (press Cmd + Space, type "Terminal," and hit Enter).
    • Type python3 --version and press Enter. If Python is installed, you’ll see the version number displayed.
  • For Windows Users:
    • Open the Command Prompt (press Win + R, type cmd, and hit Enter).
    • Type python --version and press Enter. If Python is installed, you’ll see the version number displayed.

Step 2: Setting Up Visual Studio Code (VS Code)

VS Code is a free and lightweight code editor. It’s perfect for writing and testing Python programs.

Steps to Install VS Code:

  1. Download VS Code:

    • Head over to the VS Code download page.
    • Select the download option that matches your operating system:
      _ 1: For Windows users, click the Windows button.
      _ 2: For Linux users, choose .deb or .rpm based on your Linux distribution.
      * 3: For Mac users, click the Mac button and select the appropriate version (Intel chip, Apple silicon, or Universal).
      vscode download page
      vscode download page
  2. Install VS Code:

    • After downloading, locate the file in your Downloads folder:
      • For Windows, double-click the .exe file and follow the installation instructions.
      • For Mac, open the .dmg file and drag the Visual Studio Code icon into your Applications folder.
      • For Linux, install using the .deb or .rpm file with your package manager.
  3. Launch VS Code:

    • Open Visual Studio Code:
      • On Windows, find it in your Start menu or desktop.
      • On Mac, go to your Applications folder and double-click Visual Studio Code.
      • On Linux, search for "Visual Studio Code" in your application launcher or start it from the terminal using the command code.

Step 3: Setting Up Python in VS Code

Great! Now that you’ve installed Visual Studio Code, open it to set it up for Python coding

Steps to Configure VS Code:

  1. Install the Python Extension:

    • In VS Code, click the square icon on the left sidebar to open the Extensions view.
    • Type Python in the search bar and press Enter.
    • Install the extension titled Python by Microsoft.
      install python extension
      install python extension
  2. Select Python Interpreter (if needed):

    • Normally, VS Code automatically selects the correct Python interpreter when you open a Python file.
    • If it doesn’t, press Cmd + Shift + P (or Ctrl + Shift + P on Windows/Linux) to open the Command Palette.
    • Type Python: Select Interpreter and press Enter.
    • Choose the Python 3.x version you installed earlier.

Step 4: Write and Run Your First Python Program

Now that everything is set up, let’s jump into the fun part—writing your very first Python program! Don’t worry, this is simple, and you’ll see how easy it is to make your computer do something exciting.

Steps to Write and Run Your First Program:

  1. Create a New File:

    • In VS Code, click File > New File.
    • Then select Python file
      new python file
      new python file
  2. Save the File:

    • Click File > Save As, then choose a location to save your file.
    • Name the file hello_world.py and ensure it ends with .py, which tells the computer it’s a Python file.
  3. Write Your Code:

    • In the blank editor window, type the following line of code:

      print("Hello, World!")
      
  4. Run the Program:

    • Save your file by clicking File > Save (or press Cmd + S / Ctrl + S).

    • Open a terminal in VS Code by clicking Terminal > New Terminal.

      Open new terminal
      Open a new terminal
    • At the top right corner of VS Code, you’ll see a play icon (▶). Click this icon.

    • From the dropdown menu that appears, select Run Python File.

      Run python file
      Run Python file
  5. See the Output:

    • The terminal window in VS Code will open automatically and display the output:
      output from python file
      output from python file

🎉 And there you have it! You’ve just written and executed your first Python program.

Conclusion

Congratulations on taking your first steps in coding! In this tutorial, we installed Python, set up VS Code as your code editor, and wrote your first Python program. Each step builds a foundation for the coding adventures ahead.

If you saw "Hello, World!" appear in your terminal, give yourself a big pat on the back—you’ve just joined the world of programming! Remember, it’s okay to make mistakes and experiment as you learn. The journey has just begun, and there’s so much to explore.

What’s Next?

Now that you’ve got the basics down, here are a few ideas for what you can do next:

  • Try changing the message in your program to something else, like "Hello, Python!".
  • Explore Python’s math capabilities by writing a program that calculates simple equations.
  • Dive into learning more Python features, like variables, loops, and functions.