Class 2: intro to notebooks and python

Agenda:

  1. review Data Science

  2. jupyter notebook

  3. python: conditionals and functions

Jupyter Notebooks

To launch a Jupyter notebook, in your Anaconda prompt on Windows or terminal on Linux or Mac:

cd dir/you/want/to/work/in
jupyter notebook

A Jupyter notebook has two modes. When you first open, it is in command mode. The border is blue in command mode.

screenshot of a code cell in command mode

When you press a key in command mode it works like a shortcut. For example p shows the command search menu.

screenshot of the command menu

If you press enter (or return) or click on the cell it changes to edit mode. The border is green in edit mode

screenshot of a code cell in edit mode

Type code or markdown into boxes called cells. There are two type of cells that we will used: code and markdown. You can change that in command mode with y for code and m for markdown or on the cell type menu at the top of the notebook.

screenshot of the cell type menu

You can treat markdown cells like plain text, or use special formatting. Here is a markdown cheatsheet

Code cells can run like a calculator. If there is a value returned by the last line of a cell, it will be displayed.

4+5
9

For example, when we assign, python “returns” None so there is no output from this cell

a = 9

But this one does display the value of the cell

a
9
b = 4
b
4
a
9

Getting Help in Python and Jupyter

The standard way to get help in the help function

help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

There are two special ways to get help in Jupyter, one dynamically while you’re working and one that stays displayed for a while

Python comments are indicated by the # symbol.

print() # shift +tab to view help

That will look like this: screenshot of the docstring from shift+tab help

Press tab twice for the longer version.

A Question mark puts it in a popup window that stays until you close it

print?

screenshot of the docstring from ? help

This means you can then use the displayed help to remember how to call the function

print(a,b, 'hello',sep='-')
9-4-hello
a
b
4

If Statements

:::{warning} if the concept of an if in programming is new to you, you should talk to Dr. Brown. Basic programming is a prerequisite to this course, we’re reviewing basic ideas but only at a level of detail to serve as a reminder. :::

The synta

if a >b:
    print('greater')
greater
if b > a:
    print('greater')

Tip

this is updated to include things that were skipped in class and discussed after the breakouts

You can check the contents of a string with the in keyword

name = 'sarah'
if 'a' in name:
    print(name, 'has an a')
sarah has an a

if we copy and change the name we get no output

name = 'Beibhinn'
if 'a' in name:
    print(name, 'has an a')

Functions

Tip

this is also updated to include things that were skipped in class and discussed after the breakouts

How to write functions in python:

  • the def keyword starts a function definition

  • then the function name

  • then the parameters it accepts in ()

  • end that line with a :

  • the body of the function is spaced over one tab, but Jupyter will do it automatically for you. if it doesn’t you might have forgotten the :

def greeting(name):
    '''
    a function that greets the person name by printing

    parameters
    ----------
    name: string
      a name to be greeeted

    Returns
    -------
    nothing
    '''
    print('hello', name)
greeting('sarah')
hello sarah

A better version of that function might be:

def greeting(name):
    '''
    a function that greets the person name by printing

    parameters
    ----------
    name: string
      a name to be greeeted

    Returns
    -------
    nothing
    '''
    return 'hello ' + name

Try it Yourself!

Write a function that checks if a string has a space in it and returns “please rename” if there is a space.

Remember a docstring. Call your function a couple of times to confirm it works.

Unhide the cell below to see the answer.

def check_string(text):
    '''
    this function checks for spaces in text

    Parameters
    ----------
    text : string
      the text to be checked

    Returns
    -------
    None or "please rename"
    '''
    if ' ' in text:
        return "please rename"

This is what some calls of the function look lik

check_string("my data.csv")
'please rename'

If there’s no string we see no output

check_string("my_data.csv")

What does python actually return?

type(check_string("my_data.csv"))
NoneType

it returns None, which is the python empty/null data type