#!/usr/bin/env python # coding: utf-8 # ## Getting started # # - Open a console window: look in the **Start** menu for **Anaconda** then **Anaconda prompt**. # - Notice it says `(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 # ``` # # - The prompt changes to reflect your environment. # - Now you can start Jupyter: # # ``` # jupyter lab # ``` # # - Away you go! # - A common source of confusion is that the environment is not connected to the folder or files you are using (the Python and Jupyter notebook files). You can use your environment on any project or group of projects. You don't have to be in a particular folder to use an environment. # # ### Making a new environment # # 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. # # ### Other ways to run Python # # 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 # # 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: # In[1]: print("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**. # ## Maths! # # Let's write some code... # In[3]: # This is a code comment. Python ignores it. a = 5 # a is a 'variable' or 'name'. Variables store data. a + 3.14 # In[2]: 4 * 5 - 6 # In[3]: 11 // 3 # This is integer division # In[5]: 3e5 # This is a float, like 3.14, but it means '3 times 10 to the power of 5' # In[6]: 2**3 # This is 2 to the power of 3, note it is NOT 2^3 # For more maths you might need to import the `math` module, which is built into Python. It contains lots of mathematical functions e.g. # In[4]: import math math.sqrt(5) # In practice, though, we are often using NumPy, which has all the same things as `math` plus 1 million other things: # In[7]: import numpy as np np.sqrt(5) # ## Strings! # # Strings are ordered sequences of characters. # In[9]: s = "Sandstone" print(s) # In[10]: s[2] # Indexing. This means 'give me the 3rd character in s (Python counts from 0)' # In[11]: s[2:6] # Slicing. This means 'give me characters from index 2 up to (not including) index 6' # Strings have 'methods' that are functions that are sort of 'attached' to the strings you make: # In[12]: s.upper() # In[13]: s.replace('nds', '***') # In[14]: s.find('d') # At which index is the 'd'? # ## Lists! # # Sometimes (often) you want to store ordered sequences of other things - not just characters. Python's `list` object can store anything (including more lists!). # In[21]: mylist = [5, 10, 15, 20, 25, 30] # In[22]: mylist[2] # Indexing and slicing are just like they are for strings # In[23]: mylist[-1] # You can index backward from the end with negative numbers. # In[24]: mylist.append(35) # Adds to the end of the list mylist # In[ ]: