Module: Getting Started with Python

First Program

Getting Started with Python: Your First Program

This guide will walk you through writing and running your very first Python program. It's a classic: printing "Hello, World!" to the console.

1. Setting up Your Environment (If you haven't already)

Before you can write and run Python code, you need to have Python installed on your computer. Here's a quick overview:

  • Download Python: Go to https://www.python.org/downloads/ and download the latest version of Python for your operating system (Windows, macOS, Linux).
  • Installation: Run the installer. Important: During installation, make sure to check the box that says "Add Python to PATH". This makes it easier to run Python from your command line/terminal.
  • Verify Installation: Open your command prompt (Windows) or terminal (macOS/Linux) and type python --version or python3 --version. You should see the Python version number printed.

2. Writing the Code

Now, let's write the "Hello, World!" program. You can use any text editor (like Notepad, TextEdit, VS Code, Sublime Text, etc.) to create a new file.

Type the following code into your text editor:

print("Hello, World!")

Explanation:

  • print() is a built-in Python function that displays output to the console.
  • "Hello, World!" is a string literal – the text you want to print. Strings are enclosed in either single quotes (') or double quotes (").

3. Saving the File

Save the file with a .py extension. For example, you could save it as hello.py. The .py extension tells your computer that this is a Python file. Choose a location you'll remember (like your Documents folder or a dedicated Python projects folder).

4. Running the Program

There are a few ways to run your Python program:

  • From the Command Line/Terminal:

    1. Open your command prompt/terminal.
    2. Navigate to the directory where you saved hello.py using the cd command (change directory). For example, if you saved it in your Documents folder, you might type cd Documents.
    3. Type python hello.py (or python3 hello.py if you have both Python 2 and Python 3 installed) and press Enter.
  • Using an Integrated Development Environment (IDE):

    • IDEs like VS Code, PyCharm, and Spyder provide a more feature-rich environment for writing and running Python code. They often have a "Run" button or a keyboard shortcut to execute your program. The exact steps will vary depending on the IDE you're using.

5. Expected Output

When you run the program, you should see the following output in your command prompt/terminal:

Hello, World!

Congratulations! You've written and run your first Python program!

Key Takeaways:

  • print() is used to display output.
  • Strings are enclosed in quotes.
  • .py is the file extension for Python files.
  • You can run Python programs from the command line/terminal or using an IDE.

Next Steps:

  • Experiment with changing the text inside the quotes in the print() function.
  • Learn about variables and data types.
  • Explore more built-in Python functions.
  • Start working through online tutorials and documentation. The official Python documentation is a great resource: https://docs.python.org/3/