How to Write and Run Your First Python Program: A Beginner's Guide
Introduction
Writing your first program is a major milestone when learning any programming language. Python is known for its simplicity and readability, making it an excellent language for beginners. In this post, you’ll write a basic Python script, run it, and see how Python handles output and user input.
You'll start by using the print()
function to display text in the terminal. Then, you’ll move on to using the input()
function to interact with users. These are the core concepts behind every interactive Python application — from simple scripts to complex systems.
By working through this post, you will:
Understand how to create and save a Python script.
Learn how to run your script using Visual Studio Code.
Use
print()
to display output.Use
input()
to collect user data from the terminal.
Let’s dive into the world of programming by bringing your very first Python code to life.
Writing Your First Python Program
Creating your first Python program is an exciting step in your programming journey. Let's walk through the process of writing a simple script that displays a message on the screen.
Step 1: Open Visual Studio Code
Launch Visual Studio Code (VS Code) on your computer.
Open a folder where you'd like to store your Python projects:
Go to File > Open Folder.
Select or create a folder (e.g.,
PythonProjects
).
Step 2: Create a New Python File
In the Explorer pane on the left, click the New File icon.
Name the file
hello.py
and press Enter.The
.py
extension indicates that this is a Python file.
Step 3: Write the Python Code
In the hello.py
file, type the following code:
print("Hello, World!")
This line uses Python's built-in print()
function to display the text "Hello, World!" on the screen.
Step 4: Save the File
Save your changes by pressing
Ctrl + S
(Windows/Linux) orCmd + S
(macOS).
Step 5: Run the Python Script
To execute your Python program:
Open the integrated terminal in VS Code:
Go to View > Terminal or press
Ctrl + `
.
Ensure you're in the directory containing
hello.py
.Run the script by typing:
python hello.py
You should see the output:
Hello, World!
Understanding the Code
print()
Function: This function outputs the specified message to the screen.String Literals: Text enclosed in quotes (
"Hello, World!"
) is known as a string literal.Syntax: Python syntax is straightforward, making it easy for beginners to read and write code.
By following these steps, you've successfully written and executed your first Python program. This foundational experience sets the stage for more complex and interactive applications as you continue learning Python.
Taking User Input with input()
Interactive programs often require input from users. In Python, the built-in input()
function facilitates this by allowing you to prompt users and capture their responses.
Basic Usage of input()
The input()
function displays a prompt to the user, waits for input, and returns the entered value as a string.
Example:
name = input("Enter your name: ")
print("Hello, " + name + "!")
When executed, this script will:
Display the prompt:
Enter your name:
Wait for the user to type their name and press Enter
Store the input in the variable
name
Print a greeting using the entered name
Sample Output:
Enter your name: Alice
Hello, Alice!
Understanding the Return Type
It's important to note that input()
always returns a string, regardless of what the user enters. If you need to work with numerical values, you'll have to convert the input accordingly.
Example:
age = input("Enter your age: ")
print("You are " + age + " years old.")
Even if the user enters a number like 25
, it's stored as the string "25"
.
Summary
The input()
function is a fundamental tool in Python for capturing user input, enabling interactive and dynamic programs. By understanding how to use input()
effectively and converting inputs to the necessary data types, you can create programs that respond to user data and perform various operations based on that input.
Common Errors and Troubleshooting
As you experiment, you might encounter some common errors:
Syntax Errors:
Missing Parentheses:
print "Hello, World!"
Error:
SyntaxError: Missing parentheses in call to 'print'
Solution: Ensure you're using parentheses with the
print()
function.Incorrect Indentation:
print("Hello, World!") print("This line is indented incorrectly.")
Error:
IndentationError: unexpected indent
Solution: Ensure consistent indentation. Python uses indentation to define code blocks.
Runtime Errors:
Misspelled Function Names:
prnt("Hello, World!")
Error:
NameError: name 'prnt' is not defined
Solution: Check for typos in function names.
Tips:
Read Error Messages Carefully: They often provide the exact line and nature of the error.
Use VS Code's IntelliSense: It offers real-time suggestions and error highlighting.
Conclusion
You've just taken your first hands-on steps into the world of Python programming. You’ve learned how to write a simple Python script, use the print()
function to display output, and gather input from users using the input()
function. These basic tools are the foundation of every Python program—whether it’s a simple calculator or a complex data processing pipeline.
Along the way, you also explored how to handle multiple inputs and became aware of common beginner errors like SyntaxError
, IndentationError
, and NameError
. Knowing how to identify and fix these issues early on will save you time and frustration as your programs become more sophisticated.
With this knowledge, you now have the power to create interactive programs that respond to user input—an essential skill for building real-world applications. Keep experimenting, try writing small scripts, and don't be afraid to make mistakes. Every error is a step forward in your learning journey.
Let’s keep coding!