(base)
in the prompt. This is the virtual environment. You don't want to use base
, but switch to your own environment. Type this: conda activate hackathon
jupyter lab
If you want to make a new environment, e.g. for a new project, do this **replacing my_env
with whatever name you want:
conda create -n my_env python=3.7 anaconda
conda activate my_env
python -m ipykernel install --user --name my_env
When Python 3.8 comes out in a few weeks, you can start using that. The last bit (with ipykernel
) installs a Jupyter kernel (the Python runtime) using the new environment, so you can use it from within Jupyter.
Some people like the Anaconda Navigator — have a look for it in the Start menu.
Check out Spyder as well (also installed with Anaconda). It's an 'IDE' a bit like MATLAB or R Studio. Some people like it.
Notebooks are documents that can be rendered as HTML by the Jupyter server that's running on your computer when you start Jupyter. (The actual document, the .ipynb
file, is stored in a format called JSON.)
Notebooks are useful because they combine code blocks with text blocks (like this one). Double click on a text block to edit it. Shift + Enter to render it.
The text blocks are written in a mark-up language called Markdown.
The code blocks are written in Python. Here's one:
print("Hello world")
Hello world
Click on a code block to edit it. Press Shift + Enter to run it.
You can add more blocks with the + button, or by pressing Esc then B.
Let's write some code...
# This is a code comment. Python ignores it.
a = 5 # a is a 'variable' or 'name'. Variables store data.
a + 3.14
8.14
4 * 5 - 6
14
11 // 3 # This is integer division
3
3e5 # This is a float, like 3.14, but it means '3 times 10 to the power of 5'
300000.0
2**3 # This is 2 to the power of 3, note it is NOT 2^3
8
For more maths you might need to import the math
module, which is built into Python. It contains lots of mathematical functions e.g.
import math
math.sqrt(5)
2.23606797749979
In practice, though, we are often using NumPy, which has all the same things as math
plus 1 million other things:
import numpy as np
np.sqrt(5)
2.23606797749979
Strings are ordered sequences of characters.
s = "Sandstone"
print(s)
Sandstone
s[2] # Indexing. This means 'give me the 3rd character in s (Python counts from 0)'
'n'
s[2:6] # Slicing. This means 'give me characters from index 2 up to (not including) index 6'
'ndst'
Strings have 'methods' that are functions that are sort of 'attached' to the strings you make:
s.upper()
'SANDSTONE'
s.replace('nds', '***')
'Sa***tone'
s.find('d') # At which index is the 'd'?
3
Sometimes (often) you want to store ordered sequences of other things - not just characters. Python's list
object can store anything (including more lists!).
mylist = [5, 10, 15, 20, 25, 30]
mylist[2] # Indexing and slicing are just like they are for strings
15
mylist[-1] # You can index backward from the end with negative numbers.
30
mylist.append(35) # Adds to the end of the list
mylist
[5, 10, 15, 20, 25, 30, 35]