End interactive session 1A
This is a short exercise to introduce you to the IPython REPL within a JupyterLab session hosted on a Posit Workbench server. This exercise will help you become familiar with the interactive environment we will use throughout the class (and throughout your time in the MEDS program) as well as an introduction to some basic Python operations.
Exercise: Introduction to IPython REPL in JupyterLab
Objective: Learn how to use the IPython REPL in JupyterLab for basic Python programming and explore some interactive features.
The Read-Eval-Print Loop (REPL) is an interactive programming environment that allows users to execute Python code line-by-line, providing immediate feedback and facilitating rapid testing, debugging, and learning.
Step 1: Access JupyterLab
Log in to the Posit Workbench Server
- Open a web browser and go to workbench-1.bren.ucsb.edu.
- Enter your login credentials to access the server.
Select JupyterLab
- Once logged in, click on the βNew Sessionβ button, and select βJupyterLabβ from the list of options
Start JupyterLab Session
- Click the βStart Sessionβ button in the lower right of the modal window. You donβt need to edit either the Session Name or Cluster.
Wait for the Session to Launch
- Your browser will auto join the session as soon as the server starts it up.
Step 2: Open the IPython REPL
When the session launches you should see an interface that looks like this:
- Start a Terminal
- Select βTerminalβ from the list of available options in the Launcher pane. This will open a new terminal tab.
- Launch IPython
- In the terminal, type
ipython
and press Enter to start the IPython REPL.
- In the terminal, type
Step 3: Basic IPython Commands
In the IPython REPL, try the following commands to get familiar with the environment:
- Basic Arithmetic Operations
Calculate the sum of two numbers:
3 + 5
Multiply two numbers:
4 * 7
Divide two numbers:
10 / 2
- Variable Assignment
Assign a value to a variable and use it in a calculation:
= 10 x = 5 y = x * y result result
- Built-in Functions
Use a built-in function to find the maximum of a list of numbers:
= [3, 9, 1, 6, 2] numbers max(numbers)
- Interactive Help
Use the
help()
function to get more information about a built-in function:help(print)
Use the
?
to get a quick description of an object or function:len?
Step 4: Explore IPython Features
- Tab Completion
Start typing a command or variable name and press
Tab
to auto-complete or view suggestions:# Press Tab here num
- Magic Commands
Use the
%timeit
magic command to time the execution of a statement:%timeit sum(range(1000))
- History
View the command history using the
%history
magic command:%history
- Clear the Console
Clear the current console session with:
%clear
Step 5: Exit the IPython REPL
- To exit the IPython REPL, type
exit()
or pressCtrl+D
.
Wrap-Up
Congratulations! You have completed the introduction to the IPython REPL in JupyterLab. You learned how to perform basic operations, use interactive help, explore magic commands, and utilize IPython features.
Feel free to explore more IPython functionalities or ask questions if you need further assistance.