Setting up the Python Programming Environment and Writing Your First Python Code #
This detailed guide will walk you through the process of setting up a Python programming environment on your computer and writing your first Python code. Whether you’re using Windows, macOS, or Linux, this guide will cover all the necessary steps to get you started.
1. Installing Python #
Windows #
- Download Python:
- Go to the official Python website: python.org
- Navigate to the Downloads section and select the latest stable version of Python for Windows.
- Run the Installer:
- Double-click the downloaded executable file to run the installer.
- Make sure to check the box that says “Add Python to PATH” before clicking “Install Now.”
- Follow the prompts to complete the installation.
- Verify Installation:
- Open Command Prompt (search for
cmd
in the Start menu). - Type
python --version
and press Enter. You should see the installed version of Python.
- Open Command Prompt (search for
macOS #
- Download Python:
- Go to the official Python website: python.org
- Navigate to the Downloads section and select the latest stable version of Python for macOS.
- Run the Installer:
- Open the downloaded
.pkg
file and follow the installation instructions.
- Open the downloaded
- Verify Installation:
- Open Terminal (search for
Terminal
in Spotlight). - Type
python3 --version
and press Enter. You should see the installed version of Python.
- Open Terminal (search for
Linux #
- Using Package Manager:
- Most Linux distributions come with Python pre-installed. You can check the installed version by opening the terminal and typing
python3 --version
. - If Python is not installed, you can install it using the package manager. For example, on Ubuntu, you can run:
sudo apt update sudo apt install python3
- Most Linux distributions come with Python pre-installed. You can check the installed version by opening the terminal and typing
- Verify Installation:
- Open Terminal.
- Type
python3 --version
and press Enter. You should see the installed version of Python.
2. Setting Up an Integrated Development Environment (IDE) #
An IDE provides a user-friendly interface to write, debug, and execute your Python code. Some popular IDEs for Python include PyCharm, Visual Studio Code, and Jupyter Notebook.
PyCharm #
- Download and Install PyCharm:
- Go to the JetBrains website: jetbrains.com/pycharm
- Download the Community edition (free) or Professional edition.
- Run the installer and follow the installation instructions.
- Setting Up PyCharm:
- Open PyCharm and create a new project.
- Select the Python interpreter (usually detected automatically).
Visual Studio Code #
- Download and Install VS Code:
- Go to the official Visual Studio Code website: code.visualstudio.com
- Download the installer for your operating system and run it.
- Installing Python Extension:
- Open VS Code.
- Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window.
- Search for “Python” and install the extension by Microsoft.
- Setting Up Python Interpreter:
- Open the Command Palette (View > Command Palette).
- Type
Python: Select Interpreter
and select the appropriate Python interpreter.
Jupyter Notebook #
- Install Jupyter Notebook:
- Open your terminal or command prompt.
- Run the command:
pip install notebook
- Launch Jupyter Notebook:
- In the terminal or command prompt, run:
jupyter notebook
- A new browser window should open with the Jupyter interface.
- In the terminal or command prompt, run:
3. Writing Your First Python Code #
Using a Simple Text Editor #
- Open a Text Editor:
- You can use any text editor like Notepad, TextEdit, or a code editor like VS Code.
- Write the Code:
- Type the following code to print “Hello, World!”:
print("Hello, World!")
- Save the File:
- Save the file with a
.py
extension, for example,hello.py
.
- Save the file with a
- Run the Code:
- Open the terminal or command prompt.
- Navigate to the directory where you saved the file.
- Run the file by typing
python hello.py
(orpython3 hello.py
on macOS/Linux). - You should see the output:
Hello, World!
Using PyCharm #
- Create a New Project:
- Open PyCharm and select “Create New Project.”
- Choose a location for your project and click “Create.”
- Create a New Python File:
- Right-click on the project name in the Project Explorer.
- Select “New” > “Python File.”
- Name the file
hello.py
.
- Write the Code:
- Type the following code:
print("Hello, World!")
- Run the Code:
- Right-click on the file in the Project Explorer and select “Run ‘hello’.”
- You should see the output in the Run window:
Hello, World!
Using Visual Studio Code #
- Open VS Code and Create a New File:
- Open VS Code and select “File” > “New File.”
- Save the file with a
.py
extension, for example,hello.py
.
- Write the Code:
- Type the following code:
print("Hello, World!")
- Run the Code:
- Open the terminal in VS Code by selecting “View” > “Terminal.”
- In the terminal, navigate to the directory where you saved the file.
- Run the file by typing
python hello.py
(orpython3 hello.py
on macOS/Linux). - You should see the output:
Hello, World!
Using Jupyter Notebook #
- Open Jupyter Notebook:
- Launch Jupyter Notebook by running
jupyter notebook
in your terminal or command prompt. - A new browser window should open with the Jupyter interface.
- Launch Jupyter Notebook by running
- Create a New Notebook:
- Click on “New” and select “Python 3” to create a new notebook.
- Write the Code:
- In the first cell, type the following code:
print("Hello, World!")
- In the first cell, type the following code:
- Run the Code:
- Click on the “Run” button or press
Shift + Enter
. - You should see the output below the cell:
Hello, World!
- Click on the “Run” button or press
Using Google Colab #
- Open Google Colab:
- Go to the Google Colab website: colab.research.google.com.
- Sign in with your Google account if you haven’t already.
- Create a New Notebook:
- Click on “File” in the top-left corner.
- Select “New notebook.”
- Write the Code:
- In the first cell, type the following code:
print("Hello, World!")
- In the first cell, type the following code:
- Run the Code:
- Click on the “Run” button (a play icon) on the left side of the cell or press
Shift + Enter
. - You should see the output below the cell:
Hello, World!
- Click on the “Run” button (a play icon) on the left side of the cell or press
Conclusion #
Congratulations! You’ve successfully set up your Python programming environment and written your first Python code using various tools and platforms, including Google Colab. This setup will serve as the foundation for your Python programming journey, allowing you to develop, test, and execute Python programs efficiently. Happy coding!