Your First Python Program
Overview
In this lesson, we will guide you through writing your very first Python program. By the end of this lesson, you’ll learn how to run a Python script that prints a message to the screen. This simple exercise will familiarize you with Python syntax and running Python scripts.
Introduction
Python’s simplicity makes it an ideal language for beginners. The traditional first program in any programming language is “Hello, World!” because it’s a simple way to introduce the basic syntax of a programming language. In Python, this can be accomplished with just one line of code.
Writing Your First Program
- Open a Text Editor: Start by opening a text editor of your choice. This could be Notepad on Windows, TextEdit on macOS, or Gedit on Linux. For a more programming-focused experience, you might consider using a code editor like Visual Studio Code or Sublime Text.
-
Write the Code: Type the following line in your text editor: ```python print(“Hello, World!”)
- Save the File: Save your file with a .py extension, which denotes a Python script. You might name it
hello_world.py
.
Running Your Program
Open the Terminal or Command Prompt: Navigate to the directory where you saved your Python script. Run the Script: Type python hello_world.py and press Enter. You should see the message Hello, World! printed to the terminal.
Understanding the Code
-
The print() Function: The
print()
function in Python outputs the string provided to it to the console. Strings are sequences of characters enclosed in quotes. -
Comments: You can add comments to your Python code using the # symbol. Anything following # on a line is ignored by Python, allowing you to annotate your code. For example:
# This is a comment
print("Hello, World!") # This prints a message
Ready to Run Your First Python Program?
Now that you’ve written your first Python program, you’re ready to explore more complex concepts and applications. This simple exercise is just the beginning of your journey with Python.
Project: Your First Python Program
Conclusion
Congratulations on running your first Python program! This step is just the beginning of your journey with Python. As you continue learning, you’ll discover the vast capabilities and applications of Python across different fields.