Introduction to Python and IPython using Jupyter

What is Python?
¶

Python is a powerful and easy to use programming language. It has a large community of developers and given its open source nature, you can find many solutions, scripts, and help all over the web. It is easy to learn and code, and faster than other high-level programming languages...and did I mention it is free because it is open-source

Who uses Python?
¶

First steps
¶

Let's start by running some simple commands at the prompt to do some simple computations.

We can perform addition and substractions like $1 + 1 - 2 = $ {{1 + 1 - 2}}

In [1]:
1 + 1 - 2
Out[1]:
0

Multiplication and division like $3 * 2=${{3 * 2}} or $1/2=${{1/2}}

In [2]:
3*2
Out[2]:
6
In [3]:
1 / 2
Out[3]:
0.5

Compute powers $x^y$ as x ** y

In [4]:
3**2
Out[4]:
9

Python obeys the usual orders for operators, so exponentiation before multiplication/division, etc.

In [5]:
-1**2
Out[5]:
-1
In [6]:
3 * (3 - 2)
Out[6]:
3
In [7]:
3 * 3 - 2
Out[7]:
7

Getting help
¶

So what else can we do? Where do we start if we are new? You can use ? or help() to get help.

In [8]:
?
IPython -- An enhanced Interactive Python
=========================================

IPython offers a fully compatible replacement for the standard Python
interpreter, with convenient shell features, special commands, command
history mechanism and output results caching.

At your system command line, type 'ipython -h' to see the command line
options available. This document only describes interactive features.

GETTING HELP
------------

Within IPython you have various way to access help:

  ?         -> Introduction and overview of IPython's features (this screen).
  object?   -> Details about 'object'.
  object??  -> More detailed, verbose information about 'object'.
  %quickref -> Quick reference of all IPython specific syntax and magics.
  help      -> Access Python's own help system.

If you are in terminal IPython you can quit this screen by pressing `q`.


MAIN FEATURES
-------------

* Access to the standard Python help with object docstrings and the Python
  manuals. Simply type 'help' (no quotes) to invoke it.

* Magic commands: type %magic for information on the magic subsystem.

* System command aliases, via the %alias command or the configuration file(s).

* Dynamic object information:

  Typing ?word or word? prints detailed information about an object. Certain
  long strings (code, etc.) get snipped in the center for brevity.

  Typing ??word or word?? gives access to the full information without
  snipping long strings. Strings that are longer than the screen are printed
  through the less pager.

  The ?/?? system gives access to the full source code for any object (if
  available), shows function prototypes and other useful information.

  If you just want to see an object's docstring, type '%pdoc object' (without
  quotes, and without % if you have automagic on).

* Tab completion in the local namespace:

  At any time, hitting tab will complete any available python commands or
  variable names, and show you a list of the possible completions if there's
  no unambiguous one. It will also complete filenames in the current directory.

* Search previous command history in multiple ways:

  - Start typing, and then use arrow keys up/down or (Ctrl-p/Ctrl-n) to search
    through the history items that match what you've typed so far.

  - Hit Ctrl-r: opens a search prompt. Begin typing and the system searches
    your history for lines that match what you've typed so far, completing as
    much as it can.

  - %hist: search history by index.

* Persistent command history across sessions.

* Logging of input with the ability to save and restore a working session.

* System shell with !. Typing !ls will run 'ls' in the current directory.

* The reload command does a 'deep' reload of a module: changes made to the
  module since you imported will actually be available without having to exit.

* Verbose and colored exception traceback printouts. See the magic xmode and
  xcolor functions for details (just type %magic).

* Input caching system:

  IPython offers numbered prompts (In/Out) with input and output caching. All
  input is saved and can be retrieved as variables (besides the usual arrow
  key recall).

  The following GLOBAL variables always exist (so don't overwrite them!):
  _i: stores previous input.
  _ii: next previous.
  _iii: next-next previous.
  _ih : a list of all input _ih[n] is the input from line n.

  Additionally, global variables named _i<n> are dynamically created (<n>
  being the prompt counter), such that _i<n> == _ih[<n>]

  For example, what you typed at prompt 14 is available as _i14 and _ih[14].

  You can create macros which contain multiple input lines from this history,
  for later re-execution, with the %macro function.

  The history function %hist allows you to see any part of your input history
  by printing a range of the _i variables. Note that inputs which contain
  magic functions (%) appear in the history with a prepended comment. This is
  because they aren't really valid Python code, so you can't exec them.

* Output caching system:

  For output that is returned from actions, a system similar to the input
  cache exists but using _ instead of _i. Only actions that produce a result
  (NOT assignments, for example) are cached. If you are familiar with
  Mathematica, IPython's _ variables behave exactly like Mathematica's %
  variables.

  The following GLOBAL variables always exist (so don't overwrite them!):
  _ (one underscore): previous output.
  __ (two underscores): next previous.
  ___ (three underscores): next-next previous.

  Global variables named _<n> are dynamically created (<n> being the prompt
  counter), such that the result of output <n> is always available as _<n>.

  Finally, a global dictionary named _oh exists with entries for all lines
  which generated output.

* Directory history:

  Your history of visited directories is kept in the global list _dh, and the
  magic %cd command can be used to go to any entry in that list.

* Auto-parentheses and auto-quotes (adapted from Nathan Gray's LazyPython)

  1. Auto-parentheses
        
     Callable objects (i.e. functions, methods, etc) can be invoked like
     this (notice the commas between the arguments)::
       
         In [1]: callable_ob arg1, arg2, arg3
       
     and the input will be translated to this::
       
         callable_ob(arg1, arg2, arg3)
       
     This feature is off by default (in rare cases it can produce
     undesirable side-effects), but you can activate it at the command-line
     by starting IPython with `--autocall 1`, set it permanently in your
     configuration file, or turn on at runtime with `%autocall 1`.

     You can force auto-parentheses by using '/' as the first character
     of a line.  For example::
       
          In [1]: /globals             # becomes 'globals()'
       
     Note that the '/' MUST be the first character on the line!  This
     won't work::
       
          In [2]: print /globals    # syntax error

     In most cases the automatic algorithm should work, so you should
     rarely need to explicitly invoke /. One notable exception is if you
     are trying to call a function with a list of tuples as arguments (the
     parenthesis will confuse IPython)::
       
          In [1]: zip (1,2,3),(4,5,6)  # won't work
       
     but this will work::
       
          In [2]: /zip (1,2,3),(4,5,6)
          ------> zip ((1,2,3),(4,5,6))
          Out[2]= [(1, 4), (2, 5), (3, 6)]

     IPython tells you that it has altered your command line by
     displaying the new command line preceded by -->.  e.g.::
       
          In [18]: callable list
          -------> callable (list)

  2. Auto-Quoting
    
     You can force auto-quoting of a function's arguments by using ',' as
     the first character of a line.  For example::
       
          In [1]: ,my_function /home/me   # becomes my_function("/home/me")

     If you use ';' instead, the whole argument is quoted as a single
     string (while ',' splits on whitespace)::
       
          In [2]: ,my_function a b c   # becomes my_function("a","b","c")
          In [3]: ;my_function a b c   # becomes my_function("a b c")

     Note that the ',' MUST be the first character on the line!  This
     won't work::
       
          In [4]: x = ,my_function /home/me    # syntax error
In [9]:
help()
Welcome to Python 3.9's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.9/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)".  Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.

If you want information about a command, say mycommand you can use help(mycommand), mycommand? or mycommand?? to get information about how it is used or even see its code.

In [10]:
help(sum)
Help on built-in function sum in module builtins:

sum(iterable, /, start=0)
    Return the sum of a 'start' value (default: 0) plus an iterable of numbers
    
    When the iterable is empty, return the start value.
    This function is intended specifically for use with numeric values and may
    reject non-numeric types.

In [11]:
sum?
Signature: sum(iterable, /, start=0)
Docstring:
Return the sum of a 'start' value (default: 0) plus an iterable of numbers

When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
Type:      builtin_function_or_method
In [12]:
sum??
Signature: sum(iterable, /, start=0)
Docstring:
Return the sum of a 'start' value (default: 0) plus an iterable of numbers

When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
Type:      builtin_function_or_method

Numbers, variables, strings, and other objects
¶

Python has various basic types of elements:¶

Numbers¶

Any number is interpreted by Python as such. You can have integers, floats, etc. E.g.,

1, 2, 3.1425, 1000, 1e-10
In [13]:
1
Out[13]:
1
In [14]:
2
Out[14]:
2
In [15]:
3.1425
Out[15]:
3.1425
In [16]:
1000
Out[16]:
1000
In [17]:
1e-10
Out[17]:
1e-10

Variables¶

  • Variables are names assigned to Python objects so they can be called and used in computations.
  • Variables are assigned using an equal sign
  • Variables should be written in lower case
  • Variable names cannot contain spaces or mathematical symbols
  • Variable names should provide information on what it is.

It is common to use multiple words in lower case connected by underscores. E.g.,

this_is_my_most_important_variable = (5 - 10)**2

In [18]:
a = 5
In [19]:
a
Out[19]:
5
In [20]:
b = 10
c = (a + b) * (a - b)
c
Out[20]:
-75

Strings¶

Anything that is enclosed between a pair of single or double quotation marks is treated as a string. This includes numbers or any other element. E.g.,

'Hello', "5", '[ exp(3) - 16]', "World", '"a quote"'
In [21]:
'Hello'
Out[21]:
'Hello'
In [22]:
"5"
Out[22]:
'5'
In [23]:
'[ exp(3) - 16]'
Out[23]:
'[ exp(3) - 16]'
In [24]:
"World"
Out[24]:
'World'
In [25]:
'"a quote"'
Out[25]:
'"a quote"'

Operations on strings¶

  • Strings can be added together using +
  • Strings can be multiplied by a number using *
In [26]:
string1 = "Hello"
string2 = "World"
string1 + ' ' + string2
Out[26]:
'Hello World'
In [27]:
5 * (string1 + ' ')
Out[27]:
'Hello Hello Hello Hello Hello '
Careful: There is no division, substraction, or power operation with strings.
In [28]:
string1 ** 5
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [28], in <cell line: 1>()
----> 1 string1 ** 5

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

Properties of strings¶

  • Most objects in Python have specific properties related to them.
  • These properties can be functions, constants, variables, or other objects.
  • To access the properties variable_name.property if it is a variable or constant, or variable_name.property() if it is a function.

E.g., strings have the following properties:

Method Description
capitalize( ) Converts the first character to upper case
lower( ) Converts a string into lower case
upper( ) Converts a string into upper case
startswith( ) Returns true if the string starts with the specified value
In [29]:
string1.upper()
Out[29]:
'HELLO'
In [30]:
string1.startswith('h')
Out[30]:
False
In [31]:
'This is a sentence that we will split at the spaces'.split(' ')
Out[31]:
['This',
 'is',
 'a',
 'sentence',
 'that',
 'we',
 'will',
 'split',
 'at',
 'the',
 'spaces']

Lists¶

  • Anything enclosed between square brackets is a list.
  • Elements of a list must be separated by a comma.
  • Lists can contain any other Python object, including other lists.
  • Lists are used to store multiple items in a object.
In [32]:
["some text", 5, ["another", "list"], (2,2)]
Out[32]:
['some text', 5, ['another', 'list'], (2, 2)]

Operations on Lists¶

  • Lists can be added together using +
  • Lists can be multiplied by a number using *
In [33]:
list1 = ['train', 'car']
list2 = ['bike', 'skates']
list1 + list2
Out[33]:
['train', 'car', 'bike', 'skates']
In [34]:
5 * list1
Out[34]:
['train',
 'car',
 'train',
 'car',
 'train',
 'car',
 'train',
 'car',
 'train',
 'car']
Careful: There is no division, substraction, or power operation with strings.
In [46]:
list1 - list1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [46], in <cell line: 1>()
----> 1 list1 - list1

TypeError: unsupported operand type(s) for -: 'list' and 'list'

Properties of lists¶

As explianed above, most objects in Python have specific properties related to them. These properties can be functions, constants, variables, or other objects. To access the properties variable_name.property if it is a variable or constant, or variable_name.property() if it is a function.

E.g., lists have the following properties:

Method Description
sort( ) Sort elements of the list
append(x) Add an element x to the list
remove(x) Drop element x from the list
count(x) Count number of occurrances of x in list
In [35]:
list3 = list1 + list2
list3
Out[35]:
['train', 'car', 'bike', 'skates']
In [36]:
list3.sort()
list3
Out[36]:
['bike', 'car', 'skates', 'train']

Sets¶

  • Any list of objects enclosed in in curly brackets is a set
  • Sets are similar to their mathematical equivalent
    • There are no duplicates
    • Same operations
In [47]:
set_of_cities = {'Washington, D.C.', 'Bogotá', 'Berlin', 'Dallas'}
set_of_cities
Out[47]:
{'Berlin', 'Bogotá', 'Dallas', 'Washington, D.C.'}
In [48]:
set_of_capitals = {'Washington, D.C.', 'Bogotá', 'Berlin'}
set_of_capitals
Out[48]:
{'Berlin', 'Bogotá', 'Washington, D.C.'}

Properties of sets¶

Most objects in Python have specific properties related to them. These properties can be functions, constants, variables, or other objects. To access the properties variable_name.property if it is a variable or constant, or variable_name.property() if it is a function.

E.g., sets have the following properties:

Method Description
union(A) Get union of set with set A
intersection(A) Get intersection with set B
difference(A) Get set difference with set B
In [38]:
set_of_cities.difference(set_of_capitals)
Out[38]:
{'Dallas'}
In [39]:
set_of_cities.intersection(set_of_capitals)
Out[39]:
{'Berlin', 'Bogotá', 'Washington, D.C.'}

Dictionaries¶

  • Dictionaries are used to store data values in linked pairs.
  • They are created by enclosing pairs of the form key:value between curly braces.
  • This allows us to retrieve information using the key.

E.g., we could use a dictionary to link for each artist their best album

artist_best_album = { 'Depeche Mode':'Violator',
                       'U2':'Achtung Baby',
                       'Brooklyn Funk Essentials':'In the Buzz Bag',
                      }
In [49]:
artist_best_album = { 'Depeche Mode':'Violator',
                       'U2':'Achtung Baby',
                       'Brooklyn Funk Essentials':'In the Buzz Bag',
                      }
artist_best_album
Out[49]:
{'Depeche Mode': 'Violator',
 'U2': 'Achtung Baby',
 'Brooklyn Funk Essentials': 'In the Buzz Bag'}
In [41]:
artist_best_album['U2']
Out[41]:
'Achtung Baby'

A dictionary of bands with information on each member (taken from Wikipedia)

In [42]:
top_bands = {'The Beatles':{'John':["vocals", "guitars", "keyboards", 'harmonica', "bass", 
                                    "(1960–1969; died 1980)"],
                            'Paul':["vocals", 'bass', 'guitars', 'keyboards', 'drums', "(1960–1970)"],
                            'George':["guitars", 'vocals', 'sitar', 'keyboards', 'bass', 
                                      "(1960–1970; died 2001)"],
                            'Ringo':["drums", 'percussion', 'vocals', "(1962–1970)"]
                           },
             'The Rolling Stones':{'Mick':["lead and backing vocals", 'harmonica', 'rhythm guitar', 
                                           'percussion', 'keyboards', 'bass guitar', "(1962–present)"],
                                   'Keith':['rhythm and lead guitars', 'bass guitar', 'keyboards', 
                                            'percussion', 'backing and lead vocals', '(1962–present)'],
                                   'Ronnie':['lead and rhythm guitars', 'bass guitar', 'backing vocals', 
                                             '(1975–present)']
                                  }
            }
In [51]:
top_bands['The Beatles']
Out[51]:
{'John': ['vocals',
  'guitars',
  'keyboards',
  'harmonica',
  'bass',
  '(1960–1969; died 1980)'],
 'Paul': ['vocals', 'bass', 'guitars', 'keyboards', 'drums', '(1960–1970)'],
 'George': ['guitars',
  'vocals',
  'sitar',
  'keyboards',
  'bass',
  '(1960–1970; died 2001)'],
 'Ringo': ['drums', 'percussion', 'vocals', '(1962–1970)']}
In [52]:
top_bands['The Beatles']['Ringo']
Out[52]:
['drums', 'percussion', 'vocals', '(1962–1970)']

Getting Elements in Strings, Lists, etc.¶

  • We can get elements of any strings or lists.
  • E.g., to get the first element of a list, we just execute list_name[0].
  • More generally, if we want the $n^{th}$ element, we execute list_name[n-1].
  • Let's use this to get the third element of string1, which we created above.

Remember string1={{string1}}

In [53]:
string1[1]
Out[53]:
'e'
Note: In Python elements are counted starting from zero (0) not one (1).

While we cannot get elements of sets in this way, we can:

  • convert sets into lists
  • access the element of the list

E.g., we can get the last element of our set of capitals by executing

In [45]:
list(set_of_capitals)[-1]
Out[45]:
'Washington, D.C.'
Note: We used the function list( ) to convert our set into a list.
Note: We can access an element by counting backwards, so list_name[-n] gives us the $n^{th}$ to last element.

Exercises
¶

Exercise 1: Create the list favorite_artists with your 10 favorite artists.
Exercise 2: Create the list favorite_songs with your 10 favorite songs.
Exercise 3: Create the dictionary artists_best_song which for each of your favorite artists links their best song (according to you or some other source).
Exercise 4: Create the dictionary favorite_song_artist which links each of your favorite songs with the name of the artist.

Notebook written by Ömer Özak for his students in Economics at Southern Methodist University. Feel free to use, distribute, or contribute.