Step 2¶
In step 2, we have two objectives:
- Get familiar with the VSCode interface, including the file explorer, terminal, and editor.
- Introduce ourselves to the building blocks of Python, including environments, libraries, syntax, and programming terminology.
Exercise: Intro to VSCode¶
Visually navigating VSCode¶
Visual Studio Code (VSCode) is a powerful code editor. If you didn't install it yet, go back to Step 1
and follow the instructions.
Here's a quick overview of its main components:
- Activity Bar: Located on the far left, it lets you switch between views and gives you access to different features.
- Side Bar: Shows different views like the Explorer, Search, Source Control, etc.
- Editor: The main area where you edit your files.
- Panel: Located at the bottom, it shows output, terminal, problems, etc.
- Status Bar: Displays information about the opened project and the current file.
Open up a new terminal¶
-
Open the Terminal:
- Go to the top menu and select
Terminal > New Terminal
. - In the future, you can use the shortcut that is shown near
New Terminal
. Shortcuts are great! - If you toggle open the pannel (using the pannel button in the top right corner of the terminal), you can also create a new terminal from there by pushing the big
+
button.
- Go to the top menu and select
-
Print your current directory path: Use the
pwd
command to print the path of your current directory:This will show you the path to your current directory, remember this is like the "address" of where you are in your computer. When we create files and folders (new directories!) we will be doing so in this location.pwd
Code Blocks
Above, you'll notice a gray box with text inside. This is a code block. You can copy the text inside the block with the "copy" button on the far right of the box, and paste it into your terminal to run the command.
-
Get to know your directory structure in the terminal: Use the
ls
command to list the contents of your current directory:ls
-
Create a new directory called
a_folder_is_a_directory
: Use themkdir
command:mkdir a_folder_is_a_directory
mkdir
stands for "make directory" and is used to create new directories (or, "folders"). -
Create a new directory called
a_folder_is_a_directory/this_is_a_directory_within_a_directory
: Use themkdir
command again:mkdir a_folder_is_a_directory/this_is_a_directory_within_a_directory
This creates a directory within a directory, also known as a subdirectory.
-
Create a file called
test_1.md
in thethis_is_a_directory_within_a_directory
directory: Use theNew-Item
command:New-Item -Name "test_1.md" -ItemType File
New-Item
is a command that tells the computer we want to make a new file.-Name
and-ItemType
are both parameters that you need to feed theNew-Item
command in order for it to work. In this case, we feed each parameter an "argument" in the space after it is presented, where "test_1.md" is the argument for the parameter-Name
that specifies the file name.By default, this will create the file in your current directory. You can add an input parameter called "
-Path
" if you want to create a file in a particular location, like this: -Path"C:\Users\wirthtd\downloads"
Navigate to the a_folder_is_a_directory
directory in the VSCode file explorer¶
- Open the Explorer:
You should be able to see the directory/folder structure in the file explorer on the left side of the VSCode window. If you can't see it:
-
Click on the Explorer icon in the Activity Bar (double-page icon in the top left of VSCode) or use the shortcut:
Ctrl + Shift + E
(Cmd + Shift + E
on Mac). -
Navigate to the directory:
- Click on the
a_folder_is_a_directory
folder to expand it. - Then, click on the
this_is_a_directory_within_a_directory
.Note: because there is only one directory within
a_folder_is_a_directory
, the two directories might appear stacked on a single line. If we added more content toa_folder_is_a_directory
, it would open up in a way that appears more "normal". There are settings to change this behavior, but we won't worry about that right now.
- Click on the
Open the test_1.md
file in the VSCode editor¶
- Open the file:
- Double-click on
test_1.md
to open it in the editor. - This is a markdown (
.md
) file, you can learn more about markdown here
Intro to Python¶
Python is a high-level, interpreted programming language known for its readability and versatility. It's widely used in various fields, including web development, data analysis, artificial intelligence, and scientific computing.
Python Environment¶
A Python environment is a setup that includes the Python interpreter, libraries, and other tools necessary to run Python scripts. Understanding and setting up a Python environment is crucial for ensuring that your Python projects run smoothly and are well-organized.
Components of a Python Environment¶
- Python Interpreter: The core component that reads and executes Python code, telling your hardware what to do.
- Libraries and Packages: Collections of pre-written code that perform common tasks, such as NumPy for numerical operations and Pandas for data manipulation. Think of these as special-purpose tools that you can use to build your projects. We'll go over some examples of libraries further down.
- Virtual Environment: A self-contained directory that includes a specific version of Python and a set of libraries. This helps isolate projects from each other, preventing conflicts between dependencies.
Benefits of Using a Python Environment¶
- Isolation: Each project can have its own dependencies, avoiding conflicts with other projects.
- Reproducibility: Ensures that the code runs consistently across different machines.
- Organization: Keeps projects clean and manageable.
Using Python Environments on a project-to-project basis is a good practice to get into early on, and it is a critical concept in scientific software development!
Reflect: Why might this be important in scientific software development?
Python "Libraries"¶
Python has a rich ecosystem of "libraries" that extend its capabilities - this is one of the greatest advantages of Python, and is a result of it being the largest free and open source programming language in the world.
Remember, a library is a collection of pre-written code that performs common tasks. Libraries are a critical component of your Python environment.
Here are some Python Libraries that are particularly common in scientific computing:
NumPy¶
NumPy is a library for numerical computing in Python. It provides support for arrays, matrices, and many mathematical functions.
Pandas¶
Pandas is a library for data manipulation and analysis. It provides data structures like DataFrames, which are essential for handling structured data.
Matplotlib¶
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python.
Some more basic programming terminology¶
Syntax¶
Syntax refers to the set of rules that define the structure of a programming language - in other words, syntax tells you the necessary rules for writing code.
In Python, syntax is designed to be readable and straightforward.
Variables¶
Variables are used to store data that can be referenced and manipulated in a program. You got a taste of variables in Exercise 1.
In Python, you can create a variable by assigning a value to it:
x = 10
Exercise¶
Go to this Google Colab Notebook and work through importing a Python library, and exploring some Python syntax & variables.
Bonus¶
Setting Up a Python Environment
This is a bit advanced - we'll do this together eventually, but if you want to play around with environment set up you should go ahead!!
-
Install Python: Download and install Python from the official website.
-
Create a Virtual Environment:
- Open your terminal or command prompt.
- Navigate to your project directory.
- Run
python -m venv env
(whereenv
is the name of your environment).
-
Activate the Virtual Environment:
- On Windows:
.\env\Scripts\activate
- On macOS/Linux:
source env/bin/activate
- On Windows:
-
Install Libraries: Use
pip install <library_name>
to add necessary libraries.
Example
# Create a virtual environment
python -m venv myenv
# Activate the virtual environment
source myenv/bin/activate # On macOS/Linux
.\myenv\Scripts\activate # On Windows
# Install a library
pip install numpy