Python Getting Started: How to Install Python and Run Your First Program
Are you ready to start programming with Python?
Before developing applications, you need to understand how to write Python code, where to execute it, and how to view the results.
The good news is that you can begin without installing any software by using an online Python editor. Alternatively, you can install Python on your computer and develop programs locally.
In this tutorial, you will learn how to get started with Python, check whether it is installed, create a Python file, and execute your first program.
What Do You Need to Start Learning Python?
You can begin learning Python with just a few basic requirements:
-
A computer or compatible device.
-
An internet connection if you want to use an online editor or download Python.
-
A text editor or code editor.
-
A Python interpreter if you want to execute programs locally.
You do not need previous programming experience to begin.
Method: Install Python on Your Computer
If you want to create and execute Python programs locally, install Python on your computer.
Step 1: Download Python
Visit the official Python website:
Choose the appropriate installer for your operating system.
Always obtain the installer from the official website or another trusted distribution source.
Step 2: Install Python on Windows
If you are using Windows:
-
Download the appropriate Python installer.
-
Open the downloaded installer.
-
Follow the installation instructions.
-
If the installer offers an option to add Python to PATH, enable it when appropriate.
-
Complete the installation.
Adding Python to PATH makes it easier to launch Python from Command Prompt or PowerShell.
The exact installer options may differ depending on the Python version and installation method.
Step 3: Verify the Installation
Open Command Prompt or PowerShell.
Enter:
python --version
Depending on your installation, you might see output similar to:
Python 3.x.x
The exact version will depend on the version installed on your computer.
If the python command is unavailable, try:
py --version
On macOS or Linux, you can commonly check using:
python3 --version
If none of the expected commands works, check whether Python was installed correctly and whether its executable is available through your system's PATH configuration.
Step 4: Create Your First Python File
Now that Python is installed, create a program.
You can use a basic text editor or a code editor such as Visual Studio Code.
Create a file named:
hello.py
Enter the following code:
print("Hello, World!")
print("Welcome to Python programming.")
Save the file.
Important: Ensure the filename ends in .py, not .txt.
Step 5: Run the Python File
Open Command Prompt or PowerShell and navigate to the directory containing hello.py.
For example, if the file is stored in a folder named PythonProjects on your Desktop, the command could be:
cd Desktop\PythonProjects
The exact path depends on where you saved the file.
Run the program:
python hello.py
If your Windows installation uses the Python launcher, you can also try:
py hello.py
On macOS or Linux, a common command is:
python3 hello.py
Expected Output
Hello, World!
Welcome to Python programming.
Python executes the statements in the program and displays the resulting text.
Understanding Your First Python Program
Consider this statement:
print("Hello, World!")
Let's break it down.
| Component | Explanation |
|---|---|
print |
Name of the built-in function |
() |
Parentheses used to pass arguments to the function |
"Hello, World!" |
A string containing the text to display |
" |
Quotation marks marking the beginning and end of the string |
Python evaluates the function call and displays the string in the output.
Step 6: Perform a Simple Calculation
Now let's write a program that performs arithmetic.
number1 = 20
number2 = 10
sum_result = number1 + number2
print("First number:", number1)
print("Second number:", number2)
print("Sum:", sum_result)
Expected Output
First number: 20
Second number: 10
Sum: 30
How Does This Program Work?
-
number1 = 20assigns the value20to a variable. -
number2 = 10assigns the value10to another variable. -
number1 + number2adds the two values. -
The result is assigned to
sum_result. -
The
print()statements display the values.
This example demonstrates how Python can process data rather than simply display text.
Step 7: Take Input From the User
Python programs can also receive input from users.
Consider this example:
name = input("Enter your name: ")
print("Welcome,", name)
If the user enters Priya, the interaction will look like this:
Enter your name: Priya
Welcome, Priya
The input() function displays a prompt and reads text entered by the user.
By default, the value returned by input() is a string.
For numerical input, you may need to convert the entered text into an integer or another appropriate numeric type.
Example: Add Two Numbers Entered by a User
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
total = number1 + number2
print("Total:", total)
Example interaction:
Enter the first number: 15
Enter the second number: 25
Total: 40
Here, int() converts the entered text into integers before addition.
If a user enters text that cannot be converted to an integer, Python raises a ValueError. Input validation can be added later to handle such situations.
Step 8: Check the Python Version From a Program
You can also inspect the Python version from within a Python program.
import sys
print(sys.version)
This program imports the sys module and displays information about the running Python interpreter.
The exact output depends on the installed version and environment.
Common Problems When Getting Started
Problem 1: Python Command Not Found
Possible reason: Python may not be installed, or its executable may not be available through PATH.
What to do: Verify the installation and try the appropriate command for your operating system, such as py on Windows or python3 on macOS and Linux.
Problem 2: File Not Found
Possible reason: The terminal is not in the directory containing your Python file, or the filename is incorrect.
What to do: Check the file location and navigate to the correct directory before running the program.
Problem 3: SyntaxError
Consider this incorrect code:
print("Hello, World!)
The closing quotation mark is missing.
A corrected version is:
print("Hello, World!")
Python requires strings and other expressions to follow valid syntax rules.
Problem 4: Incorrect File Extension
If you accidentally save your program as hello.py.txt, your command may not find the expected hello.py file.
Check the full filename and ensure that the extension is .py.
Next: Python Syntax | EDUNAVO
Conclusion
Getting started with Python is straightforward. You can experiment in an online editor or install Python locally to create and execute your own programs.
Begin by displaying text, performing calculations, working with variables, and accepting user input. Once these concepts become familiar, you can progress to conditions, loops, functions, and larger applications.