Setting Up Python and Writing Your First Script
Course Title: Modern Python Programming: Best Practices and Trends
Section Title: Introduction to Python and Environment Setup
Topic: Install Python, set up a virtual environment, and write your first Python script. (Lab topic)
Introduction:
Welcome to the hands-on portion of our course, Modern Python Programming: Best Practices and Trends. In this lab, we will walk through the process of installing Python, setting up a virtual environment, and writing your first Python script. By the end of this lab, you will have a solid foundation in the basics of Python development and be ready to dive deeper into the world of Python programming.
Step 1: Installing Python
Before we begin, make sure you have Python installed on your system. You can download the latest version of Python from the official Python website: <https://www.python.org/downloads/>
Follow the installation instructions for your operating system:
- For Windows: <https://docs.python.org/3/using/windows.html>
- For macOS: <https://docs.python.org/3/using/mac.html>
- For Linux: <https://docs.python.org/3/using/unix.html>
Once you have installed Python, verify the installation by opening a terminal or command prompt and typing python --version
. This should display the version of Python you just installed.
Step 2: Setting up a Virtual Environment
A virtual environment is a self-contained Python environment that allows you to manage dependencies and isolate your project from other Python projects on your system. We will use the venv
module, which is part of the Python standard library, to create a virtual environment.
Open a terminal or command prompt and navigate to the directory where you want to create your virtual environment. Then, run the following command:
python -m venv myenv
This will create a new virtual environment called myenv
. To activate the virtual environment, run:
On Windows:
myenv\Scripts\activate
On macOS and Linux:
source myenv/bin/activate
Once activated, your command prompt should indicate that you are now operating within the virtual environment.
Step 3: Writing Your First Python Script
With your virtual environment set up, let's write a simple Python script to test everything. Create a new file called hello.py
and add the following code:
# hello.py
def greet(name: str) -> None:
"""Prints a personalized greeting message."""
print(f"Hello, {name}!")
if __name__ == "__main__":
greet("World")
This script defines a function called greet
that takes a name as input and prints out a greeting message. The if __name__ == "__main__":
block is used to ensure that the greet
function is only executed when the script is run directly.
Running Your Script
To run your script, navigate to the directory where you created the hello.py
file and type:
python hello.py
You should see the following output:
Hello, World!
Congratulations! You have just written and run your first Python script.
Key Takeaways:
- Python can be downloaded from the official Python website.
- Virtual environments can be created using the
venv
module. - Activating a virtual environment allows you to manage dependencies for your project.
- Python scripts can be run using the
python
command.
What's Next:
In the next topic, we will explore Python's built-in data types, including lists, tuples, dictionaries, and sets. This will lay the foundation for more advanced topics in the course.
If you have any questions or need help with this lab, please leave a comment below. We'd be happy to assist you.
Images

Comments