First Steps with Python and Jupyter
Part 1 of "A Gentle Introduction to Programming with Python"
This tutorial is the first in a series of beginner-friendly tutorials on programming using the Python language. These tutorials take a practical coding-based approach, and the best way to learn the material is to execute the code and experiment with the examples. Check out the full series here:
- First Steps with Python and Jupyter
- A Quick Tour of Variables and Data Types
- Branching using Conditional Statements and Loops
- Writing Reusable Code Using Functions
- Reading from and Writing to Files
- Object Oriented Programming with Classes
The following topics are covered in this tutorial:
- Performing arithmetic operations using Python
- Solving multi-step problems using variables
- Evaluating conditions using Python
- Combining conditions with logical operators
- Adding text styles using Markdown
How to run the code
This tutorial is an executable Jupyter notebook hosted on Jovian.ml, a platform for sharing data science projects online (don't worry if these terms seem unfamiliar, we'll learn more about them soon). You can "run" this tutorial and experiment with the code examples in a couple of ways: using free online resources (recommended) or on your own computer.
Option 1: Running using free online resources (1-click, recommended)
The easiest way to start executing this notebook is to click the "Run" button at the top of this page, and select "Run on Binder". This will run the notebook on mybinder.org, a free online service for running Jupyter notebooks. You can also select "Run on Colab" or "Run on Kaggle", but you'll need to create an account on Google Colab or Kaggle to use these platforms.
Option 2: Running on your computer locally
To run this notebook on your computer locally, you'll need to set up Python and download the notebook. We recommend using the Conda distribution of Python. Here's what you need to do to get started:
Install Conda by following these instructions. Make sure to add Conda binaries to your system
PATHto be able to run the
condacommand line tool from your Mac/Linux terminal or Windows command prompt.
Create and activate a Conda virtual environment called
zerotopandaswhich you can use for this tutorial series, by running the follwing commands on your terminal / command prompt:
conda create -n intro-to-python -y python=3.8
conda activate intro-to-python
You'll need to create the environment only once, but you'll have to activate it every time want to run the notebook. When the environment is activated, you should be able to see a prefix
(intro-to-python) within your terminal or command prompt.
- Install the required Python libraries within the environment by the running the following command:
pip install jovian jupyter numpy pandas matplotlib seaborn --upgrade
- Download the notebook for this tutorial using the
jovian clone aakashns/first-steps-with-python
The notebook is downloaded to the directory
first-steps-with-python. You can also use the "Download Zip" option on the page instead of using the
jovian clone command.
- Enter the project directory and start the Jupyter notebook:
- You can now access Jupyter's web interface by clicking the link that shows up on the terminal or by visiting http://localhost:8888 on your browser. Click on the notebook
first-steps-with-python.ipynbto open it and run the code. If you want to type out the code yourself, you can also create a new notebook using the "New" button.
Jupyter Notebooks: This tutorial is a Jupyter notebook - a document made of "cells", which can contain explanations in text or code written in Python. Code cells can be executed and their outputs e.g. numbers, messages, graphs, tables, files etc. can be viewed within the notebook, which makes it a really powerful platform for experimentation and analysis. Don't afraid to experiment with the code & break things - you'll learn a lot by encoutering and fixing errors. You can use the "Kernel > Restart & Clear Output" menu option to clear all outputs and start again from the top of the notebook.
Performing Arithmetic Operations using Python
Let's begin by using Python as a calculator. You can write and execute Python using a code cell within Jupyter.
Working with Cells: To create a new cell within Jupyter, you can select "Insert > Insert Cell Below" from the menu bar or just press the "+" button on the toolbar. You can also use the keyboard shortcut
Esc+Bto create a new cell. Once a cell is created, click on it to select it. You can then change the cell type to code or markdown (text) using "Cell > Cell Type" menu option. You can also use the keyboard shortcuts
Esc+M. Double click a cell to edit the content within the cell. To apply your changes and run a cell, use the "Cell > Run Cells" menu option or click the "Run" button on the toolbar or just use the keyboard shortcut
Shift+Enter. You can see a full list of keyboard shortcuts using the "Help > Keyboard Shortcuts" menu option.
Run the code cells below to perform calculations and view their result. Try changing the numbers and run the changed cells again to see updated results. Can you guess what the
** operators are used for?
2 + 3 + 9
99 - 73