Introduction to Running Python Code: A Step-by-Step Tutorial
Python is a versatile and popular programming language known for its readability and wide range of applications. Whether you’re interested in web development, data science, machine learning, or scripting, Python provides the tools you need. This tutorial will guide you through the essential steps of writing and running your first Python code.
1. Install Python:
Before you can run Python code, you need to install the Python interpreter on your system. The interpreter is the program that reads and executes your Python code.
- Visit the Official Python Website: Go to https://www.python.org/downloads/.
- Choose Your Operating System: Select the appropriate installer for your operating system (Windows, macOS, or Linux).
- Download the Installer: Click on the latest stable Python release (e.g., Python 3.11, Python 3.12). Avoid “alpha” or “beta” releases unless you specifically need them for testing purposes.
- Run the Installer:
- Windows: Double-click the downloaded
.exe
file. Crucially, check the box that says “Add Python 3.x to PATH” (or a similar option, like “Add Python to environment variables”). This allows you to run Python from your command line (or terminal) without having to specify the full path to the Python executable every time. Follow the on-screen instructions. - macOS: Double-click the downloaded
.pkg
file and follow the on-screen instructions. PATH configuration is usually handled automatically. - Linux: Python is often pre-installed on many Linux distributions. You can check by opening a terminal and typing
python3 --version
. If it’s not installed or you need a specific version, use your distribution’s package manager (e.g.,apt-get install python3.x
on Debian/Ubuntu,yum install python3.x
on Fedora/CentOS,pacman -S python
on Arch Linux). Replace3.x
with the desired version.
- Windows: Double-click the downloaded
- Verify Installation: After installation, open your command prompt (Windows) or terminal (macOS/Linux) and type:
bash
python --version
(or, if that doesn’t work, trypython3 --version
). You should see the Python version you installed printed on the screen (e.g., “Python 3.11.5”). If you get an error like “python is not recognized,” double-check that you added Python to your PATH during installation (Windows). You might need to restart your computer.
2. Choose a Text Editor or IDE:
You’ll need a way to write your Python code. You can use a simple text editor or a more powerful Integrated Development Environment (IDE).
-
Simple Text Editors (Good for Beginners):
- Notepad++ (Windows): A free and popular text editor with syntax highlighting.
- Sublime Text (Windows, macOS, Linux): A powerful and customizable text editor. Technically, it has a free evaluation period, but you can use it indefinitely.
- TextEdit (macOS): The built-in macOS text editor. Make sure to use “Plain Text” mode (Format > Make Plain Text).
- gedit (Linux): A common text editor included with many Linux distributions.
- Visual Studio Code (VS Code) (Windows, macOS, Linux): While technically an IDE, VS Code is very lightweight and easy to use as a simple text editor with great Python support through extensions. Highly recommended, even for beginners, due to its ease of use and extensibility.
-
Integrated Development Environments (IDEs – More Advanced Features):
- PyCharm (Windows, macOS, Linux): A powerful and popular IDE specifically designed for Python development. There’s a free “Community” edition.
- Visual Studio Code (VS Code) (Windows, macOS, Linux) with Python extension: As mentioned above, VS Code becomes a full-fledged Python IDE with the official Python extension. This is a great option.
- Spyder (Windows, macOS, Linux): An IDE often used for scientific computing and data science.
- Thonny (Windows, macOS, Linux): A beginner-friendly IDE specifically designed for learning Python.
For this tutorial, we’ll assume you’re using a simple text editor or VS Code (without extensions, initially).
3. Write Your First Python Code:
Open your chosen text editor and create a new file. Type the following code:
python
print("Hello, world!")
This single line of code is a complete Python program! The print()
function is a built-in function that displays output to the console. In this case, it will print the text “Hello, world!”.
4. Save the File:
Save the file with a .py
extension. This tells the operating system and other tools that it’s a Python file. For example, save the file as hello.py
. Choose a location that’s easy to remember, like your Desktop or a dedicated “PythonProjects” folder.
5. Run the Code (Using the Command Line/Terminal):
Now for the exciting part: running your code!
- Open your command prompt (Windows) or terminal (macOS/Linux).
- Navigate to the directory where you saved
hello.py
. You use thecd
command (change directory) to do this. For example:- If you saved it on your Desktop (Windows), you might type:
cd Desktop
- If you saved it in a folder called “PythonProjects” on your Desktop (macOS/Linux):
cd Desktop/PythonProjects
- Use the
ls
command (Linux/macOS) ordir
command (Windows) to list the files in the current directory to verify you’re in the right place.
- If you saved it on your Desktop (Windows), you might type:
- Run the code using the
python
(orpython3
) command followed by the filename:
bash
python hello.py
(or, if that doesn’t work,python3 hello.py
) - See the Output: You should see the text “Hello, world!” printed on the console. Congratulations, you’ve just run your first Python program!
6. Run the Code (Using an IDE – VS Code Example):
If you’re using an IDE like VS Code, the process is often simpler:
- Open the
hello.py
file in VS Code. - Install the Python Extension: If you haven’t already, go to the Extensions view (usually a square icon on the left sidebar) and search for “Python.” Install the official Python extension from Microsoft.
- Run the Code: There are several ways to run the code:
- Right-click in the editor and select “Run Python File in Terminal”. This will open a terminal within VS Code and run the code.
- Click the green “play” button (usually in the top-right corner).
- Use the keyboard shortcut Ctrl+Shift+P (Cmd+Shift+P on macOS) to open the Command Palette, type “Python: Run Python File in Terminal”, and press Enter.
- See the Output: The output will be displayed in the integrated terminal within VS Code.
7. Understanding the Code (Brief Explanation):
print()
: This is a function. Functions are reusable blocks of code that perform a specific task. Theprint()
function takes an argument (the thing you want to display) inside the parentheses."Hello, world!"
: This is a string. A string is a sequence of characters enclosed in double quotes (or single quotes).
8. Next Steps:
- Experiment: Change the text inside the
print()
function to something else and run the code again. Try adding moreprint()
statements. - Learn More:
- Official Python Tutorial: https://docs.python.org/3/tutorial/
- Codecademy: https://www.codecademy.com/learn/learn-python-3
- freeCodeCamp: https://www.freecodecamp.org/learn/scientific-computing-with-python/
- Coursera, edX, Udacity: Offer various Python courses.
- Practice: The key to learning any programming language is practice. Start with small, simple programs and gradually work your way up to more complex projects. Good luck, and have fun learning Python!