Beginning Python

Table of Contents

1. Overview

What is programming, and what are some of the basics of approach and terminology? Using python as an example, basic concepts such as variables, loops, indexing and assignments are illustrated. We will also begin to examine how we can mix code with text in Emacs in preparing to generate reproducible research reports that mix code, data, text, and references. Our tool for that is called org babel.

2. General Programming Concepts Illustrated with Python

These general comments apply to more than just python. For some of the particular examples of code python is used as the example language.

Ga : Intro to Intro to Programming (i2c4p) from Britt Anderson on Vimeo.

2.1. A digression on programming (aka writing code)

I write below a few thoughts about what to think about before you start typing. In the video that follows I talk about some of the terminology and lingo that is often invoked when programming.

Gb : Some general comments on programming (i2c4p) from Britt Anderson on Vimeo.

A few general comments on programming, which boil down to know what you want to do and how you want to do it before you starting typing your code.

2.1.1. It is nothing deep: thinking before doing saves you time and improves the result.

You are writing a recipe. Do these things to get this result. Just like cooking has a certain vocabulary you need to know to follow or write a recipe, the same is true of programming.

Just like you have to think what you want to cook before you write a recipe, and you often have to go through several test dishes to perfect your recipe the same is true of programming.

  • Think what you want to do first. Another blog in support of this idea.
  • Sketch out how the different pieces of your plan need to go together.
  • Only then decide on which programming language and tools best suit this purpose. Sometimes this will be biased by what you know, but often it can be driven by what libraries and packages have already been written by other that will do some or most of what you want to do.
  • Select the tool for writing your code. Selection of an ide (integrated development environment) is a large IDEs to itself. It is more a matter of style,taste and your community than technical advantages; that is why there are so many of them. Here you will be using Emacs, but after this course most of you will probably settle on a different IDE. Shop around before you settle down.
  • Only now should you start typing your code. If you know what you want to happen, and how you want it to happen, then finding the right keyword in R or Python or most other languages is a comparatively easy task.

2.2. Compilation versus Interpretation

Are you simultaneously translating a conversation or translating a book?

2.2.1. Interpreters

are tools that take your code one line at a time and translate that into the language your computer speaks. It is like a simultaneous translation of a language. When you start a python or R interpreter you are asking that each line you type be immediately translated and executed. The advantages of this are that it can lead to quicker feedback, and encourage exploration. The disadvantage is that it is usually slower than compiled code, and it is not the sort of thing you can share with a non-technical user.

2.2.2. Compilers

are tools for taking an entire text of code (like a book) and translating that into machine code. Thus, compilers can exploit their knowledge of the whole document to make things run more efficiently or robustly. The advantage is you can share the result, your program or application, with a non-technical user and they can input their data or use the program via your coded interface. The downside is that development can be slower and more complex.

2.2.3. Which is better?

Neither. Different tasks require different tools. An R interpreter is a great way to conduct data exploration. A compiled c program might be the better way to tackle a large scale complex data analysis routine.

2.3. Coding styles

Makes your code easier to read by people using the same language.

Try to follow good programming style, and if available, language guides.

Python Style Guide

2.4. Org-babel

For those of you who prefer video you can watch this. All the information is contained in the following text though.

Gc : An Introduction to Org Babel (i2c4p) from Britt Anderson on Vimeo.

A somewhat longer demonstration of getting emacs set-up to use org and babel, and some simple demonstrations in python (and a few mistakes explained too).

Org-babel is a way to include code in an org document and have it properly execute. There is, of course, an Emacs manual : C-h i Search for org mode and in the org mode manual look for the "Library of Babel". A pretty extensive tutorial can be found here. A more recent workshop gives examples here.

We will want some basic text in our init.el file to make things work. See here as well for some hints on getting started with your init.el file.

(setq org-confirm-babel-evaluate nil)
(setq org-babel-python-command "python3")
  (org-babel-do-load-languages
  'org-babel-load-languages
  '((emacs-lisp . t) 
  (R . t)
  (python . t)))

Then you can create a source code block with


#+Begin_src <langname> :exports code
<code here ... code here>
#+End_src 

There are many optional arguments that can appear on the line with "Begin" that will affect how the output appears and how it gets updated. Many of these header arguments are different for different languages and are presented here.

Here is a functioning babel block for R 1

a = 2
b = c(1,2)
a + b

Note what we did there? We added the same number "2" to each of two other numbers that we put in a list.

And another code block for python

my_name = "britt"
my_name_as_list = list(my_name)
for i, j in enumerate(my_name_as_list):
    print((i, j))
(0, 'b')
(1, 'r')
(2, 'i')
(3, 't')
(4, 't')

2.5. Some programming vocabulary

Many of the examples and much of the text that is written below is also reviewed in this companion video.

Gd : Common programming terms (i2c4p) from Britt Anderson on Vimeo.

An overview of a few basic and fundamental programming terms.

2.5.1. Data often come in Types

Some languages dynamically type whereas other statically type. Python tries to figure out whether you meant "1" to be a character or a numeral and may interpret it as either depending on context. That can make it easier for you to just get working and not worry about all the details. In a statically typed language you may have to tell your compiler or interpreter exactly how you want it to interpret a particular "1". This can make writing your code slower and more wordy, but gives you a lower risk of errors later on.

  1. Some common types
    Integers
    1, 2, …
    Doubles/Floats
    10.3, pi; basically numbers that have decimal points. Thus, 1.000000 is not the same as the integer 1.
    Booleans
    True , False NB: some languages, e.g. R, use TRUE.
    Lists and Tuples
    Tuples
    (1,2), ('a',10.34,False) Have a fixed number of slots, can be different types. Define with parentheses usually, but check your programming languages specifications.
    Lists
    [1,2,3,4] Have a potentially infinite number of slots, but must all be same type. Define with square brackets in python.
    Dictionaries
    {'firstName' : 'Britt', 'lastName' : 'Anderson'}; Also referred to as key:value pairs. In python the curly braces are used.
    Comments
    Not really code, but allows you to put stuff in your programs for other users and yourself to read. In python the lines start with a hash "#"

2.5.2. Constants and Variables

A conceptual difference more than a implementation difference in python. Do you think the value will always be the same, like hours in the day, then it is a "constant". Do you think that the value will mutate over the course your program runs, like the time on a clock, then it is a variable. Both may be defined in python with an equal sign. Some people use the typography to distinguish the two, but note that some programming languages are case sensitive meaning ONE is different from one.

NUM_DAY_PER_YEAR = 365
x = NUM_DAY_PER_YEAR
days_this_year = x + 1
days_this_year
366

Here we treat the number of days in a regular year as a constant while allowing daysThisYear to be a variable which can change with a leap year.

2.5.3. Assignment and Equality

= is different from ==

a = 2
print(a == 3)
False

2.5.4. Loops

We saw a quick demonstration of this above. The intuition remains that of a recipe: "stir egg whites until peaked" or "simmer for 30 minutes".

  1. For Loop

    Python refers to things called "iterables." To iterate is another way of saying something you can keep doing the same thing over and over to. Imagine a bowl of ice cream. It is "eatable". You take one spoon, and keep taking spoonfuls until the bowl is empty.

    1. Indexing

      You can get the location of an element in a list by referring to its index. Think of the row or column number in a spread sheet program, but indexes can be more powerful, and can be nested easily. In many programming languages, but sadly not all, indexes start at 0. Different programming languages will have slightly different syntax.

      name_dict = {'firstName' : 'Britt', 'lastName' : 'Anderson'}
      my_list = list(range(1, 10))
      
      print(name_dict['firstName'])
      
      print(my_list)
      
      print(my_list[0])
      
      print(my_list[-1])
      
      print(my_list[0:4])
      
      Britt
      [1, 2, 3, 4, 5, 6, 7, 8, 9]
      1
      9
      [1, 2, 3, 4]
      

      The use of the -1 as an index is a python trick for getting the last element in a list. Think of the list as a circle. If you count backwards from a list you will get to the beginning eventually (index 0) if you went back one more step (-1) you would circle back to the end of the list. Test what happens when you try -2. Does it keep going? A lot of learning how to program is just doing stuff to see what happens.

      for ml in my_list:
          print(ml)
      
      
      for i, ml in enumerate(my_list):
          print("The {0}th element was {1}".format(i, ml))
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      The 0th element was 1
      The 1th element was 2
      The 2th element was 3
      The 3th element was 4
      The 4th element was 5
      The 5th element was 6
      The 6th element was 7
      The 7th element was 8
      The 8th element was 9
      
  2. While Loop

    These are like for loops in that they do stuff over and over, but unlike for loops they do things indefinitely, until that is, you tell them to stop. How do you do that? You use a predicate that they test for each time through the loop.

  3. Conditionals

    This is where you test whether something is or is not True. Note that Python, but not all computer languages, treats 0 the same as False, and all non-zero values as True.

    if 2 == 3:
        print("Wha.....?\n\n")
    elif 3 == 2:
        print("Now that is odd")
    else:
        print("2 does not equal 3.")
    

    NB: note the use of colon (:) at the end of the for and while lines.

    i = 0
    while i <= 10:
        print("brittAnderson"[i])
        i = i + 1
    
    b
    r
    i
    t
    t
    A
    n
    d
    e
    r
    s
    

2.5.5. Functions

We encountered an example of this earlier. Think of a function as a machine that grinds meat. You pour in a cow. You get out hamburger. Input -> Output. Note that arguments are "local". They are not referring to variables outside the function, in the program globally, but only make sense locally in the function. You drop values into those slots, and then you can use those names in your function, because until you use it, your function doesn't know what it will be getting.

def my_add(x, y):
   return(x + y)
my_add(2, 3)

2.5.6. Libraries

Lots of people use python. Python has been around a long time. Almost anything you might think to do at this point has been done by someone else. Look for a library and use someone else's code if you can. Use your time and effort to perfect your particular project, not to reinvent the wheel. An advantage of popular libraries is that they are likely to have withstood the tests of many users and will be more robust and correct than something you are likely to write as a first effort.

  1. What are some popular libraries?

    Here are 20 recommended ones (and a more recent list).

    Of particular note for us are:

    1. Numpy
    2. Scipy
    3. Matplotlib
    4. Pillow
    5. Sympy

2.5.7. Programs

Programs are simply collections of all of the above. Complex programs may have smaller programs as components or span many files and subsidiary libraries.

2.5.8. Debugging and Basic Working Methods

Debugging refers to the general process of finding the mistakes in your code and fixing them. Bugs can be mistakes that keep your code from running or they can be mistakes in your logic. Then the code runs, but it doesn't do what you want. Those are the worst, and the hardest to fix.

Many programming environments will provide sophisticated tools for debugging, but to get going a fairly simple technique is to embed many print statements into your code so that you can see what happening and whether your variables are actually changing like you think they should. When all is working well go back into your code and comment out or delete the print statements.

2.5.9. IDEs

What does IDE stand for: Integrated development environment.

Two popular python IDEs are:

  1. Spyder
  2. pyCharm
  1. Emacs as a python IDE

    For this course I recommend using emacs.2 A simple demonstration of vanilla emacs.

    1. Open up a blank file with a name that ends in .py
    2. Type in some lines (e.g. a = 2, b = 3, print(a+b))
    3. Type C-c C-c on the first line.
    4. Read the error message
    5. Fix it.
    6. Keep C-c C-c'ing on each line and look at what is happening in your console.
    7. When your cursor is on a python word, like print, look in the mode line.
    8. Try M-x linum-mode
    9. Looking for something more full featured? Try elpy. Instructions to install found here.

2.6. Pip to Install Libraries and Virtual Environments

In general, try to use one packaging system for your installation. If you chose to get your packages via Ubuntu repositories (i.e. apt) then try to get as many from there as you can. Otherwise the pip tool is convenient.

2.6.1. Pip

pip is the python install package program. There have been many ways to install python packages over the years and you will find a lot of tracks on the internet. For now stick with pip (ubuntu also has many of these packages, but I find it better to try and not to mix package managers. Use your choice; mine is pip).

2.6.2. Virtual Environments

When you install software to your system there is always a chance that you will install packages that conflict with each other. One way to reduce the risk of this problem is to install your programs and packages into small isolated sandboxes. The idea is that each sandbox exists independent of each of the others and only sees the packages that are installed locally to it. Virtual environments are a way to achieve this sandboxing, and python has built in tools to support this idea. The use of venv and similar ideas is beyond the scope of what we have time for here, but it is not overly complicated to set up and use. You should read further on this topic if you plan to use python more extensively in the future. Another related approach is to use conda/anaconda.

2.6.3. Beginning Python Assessment 2

3. Assessments

I have found that these assignments are where many people feel like things start getting tough so I recorded a little video with some hints and encouragment.

Ge : Hints and Overview of the Two Beginning Python Assesments (i2c4p) from Britt Anderson on Vimeo.

Beginning python assessments: hints and words of encouragement.

3.1. For Loop with Python

3.1.1. Task

Write a function that will print a list of characters unsorted, sorted, and combined. To complete this task you will need to be familiar with how to write a for loop, use python's indexing, and also how to use the print format command 3

3.1.2. What it should look like

I run your function with "trib". It prints t, r, i, b, then prints b, i, r, t, and then prints something like "The first letter of list one is 't', but it is 'b' in the sorted list… and so on through all the letters.

3.1.3. Detailed Instructions

  1. Create a list of at least 8 individual characters.
  2. Make sure they are not in alphabetical order
  3. Print the letters one at a time.
  4. Print the letters sorted alphabetically one at a time, but do not overwrite your original list.
  5. Print the letters from both lists with a format command that says which position the letter is in.

3.1.4. Comments

  1. Hints

    Remember not to re-invent the wheel. For example, does python have a function for sorting lists? Can you turn a "string" into a python list that would allow you to use indices? Lastly, note that a for loop can use more than one variable at a time. For my version I have a line that reads,

    for i, l in enumerate(zip(my_list, sorted(my_list))):
    

3.1.5. How I Will Grade

I will run your program from the command line e.g. python firstLast2LstsSorted.py. It will either spit out the correct lists or it won't. If it does you get full credit, and if not I will give partial credit and you can try to fix it for more points.

3.2. Hangman in Python (starter kit)

3.2.1. Words of Encouragement

This is the first non-trivial thing we will have done in the programming line. Do not get too frustrated if it takes you several submissions to get things right.

Remember our goal is to learn some programming basics, not to write a production quality game of hangman. It is better to spend your time perfecting your ability to get the basic pieces

We all start as beginners. Be patient with yourself.

It is fine to collaborate, but don't just turn in a working piece of code you got from someone, because while that will give you points, it will not provide you an education. Make sure you can write it yourself from scratch even if that is only after you have worked on it together with someone.

How to work. I like the idea of having a console open on one side for testing and another editor window where I can write code. I write in one window. Test in the other, and go back and forth to get something I can use. Ask me to demo in class if you are not clear on what this is supposed to look like.

3.2.2. Task

To write the beginning pieces of a hangman game in python. This version will hard code the word to be guessed. It will not produce any sort of graphic, You will ask the user for letters and report if the player spells out the word or not. Depending on how fast you master that, I may suggest further improvements when you submit.

3.2.3. Detail - tackle this in steps.

  1. Look up how to get user input from python on the command line. Write a script4 that when run from the terminal (command line) asks for a word, and prints it out.
  2. Write a function that takes as input a word. The function asks the user to guess a letter and returns a list of indices of the positions where the letter exists in the word. An empty list means the letter is not to be found in the word. Otherwise, the elements of this list are the indices where that letter is found. For example, if the word were "tree" and the letter "e" was guessed you would get back [2,3] from this function, but you would get back [] if the user supplied the letter were "s".5
  3. Write a function that accepts as input a number, which we will interpret as the maximum number of guesses a player is allowed (call it max_times). This function will use a word that you have hard coded as a constant and will loop through the guessing function above for maxtimes or until all the letter in the word have been guessed.
  4. If the number of user guesses is greater than max_times your program should print "You lose." If the user guesses all the letters print "You win". And exit the program.

3.2.4. Comments

  1. How I Will Grade

    I will run your program from the terminal, and it will work as above or it will not. If it does, I will give you full marks, if not, I will give partial credit and allow you to try and fix the mistakes.

  2. Hints

    You might find it useful to learn about python's in operator, and to think about how you might use the enumerate function we used in the for loop in python assessment. Remember to eliminate the letters from your word as they are guessed. For example if the word is tree, and I guess "e", your word should become "tr". That means you may find it easier to keep track of the indices that are not equal to the guessed letters. If a brute force solution comes quickly think about re-writing your answer using a list comprehension. If you have more of a programming background try adding in some additional features of the game. Can you get a random word from the internet? Can you let the player give the word and the computer guess letters? Could you output where the guessed characters appear in the word before the player has to make their next guess. Could you generate some simple ascii art of the hangman character?

Footnotes:

1

For this to work you will have to have R installed. If you have not done so already try sudo apt install r-base from a terminal, and then re-run the command.

2

Why use emacs as your python ide? There is a lot to learn in this course. If you choose a different tool for every job you will greatly increase your cognitive load. By using emacs for everything you can gradually learn to use one tool and focus your thinking on the stuff you are trying to learn and not the tool you need to master it. That said, you may later on choose to use something other than Emacs. Fine. But at least you will be familiar with the Swiss Army Knife of computing. It may not be the best tool for every job, but you can use it to get almost anything done, and that is a handy tool to have in your kit.

3

Not shown in lecture, but explained in many places. Look for the string.format() method.

4

Why is it 2,3 and not 3,4?

5

A script is a file that ends with .py and that I can call from the terminal. If your script is named hangman.py then things should work when I do python hangman.py in my terminal window.

Date: 2023-02-07 Tue 00:00

Author: Britt Anderson

Created: 2023-04-23 Sun 11:53

Validate