{ "metadata": { "name": "", "signature": "sha256:5ec57016ec3e2dc49a63145537e2828ab29171d08b9d6f6aa03675bcae082fd8" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 1: Getting started" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-- *A Python Course for the Humanities by Folgert Karsdorp and Maarten van Gompel*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"Ready, set, GO!\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Everyone can learn how to program and the best way to learn is by doing. In this tutorial you will be asked to write a lot of code. Click any block of code in this tutorial, such as the one above, and press ctrl+enter to run it. Let's begin right away and write our first little program! " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the code box below, write a simple program that calculates how many minutes there are in seven weeks." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert your code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-----------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! You have written your first little program and you've done it without any help! So, can we now go beyond using our programming language as a simple calculator? Before we ask you to write another program, we will first have to explain something about assignment'.\n", "\n", "We can assign values to variables using the = operator. A variable is just a name we give to a particular value, you can imagine it as a box you put a certain value into, and on which you write a name with a black marker. The following code block contains two operations. First, we assign the value 2 to the name x. After that x will hold the value 2. You might say Python stored the value 2 in x. Finally we print the value using the print() command. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 2\n", "print(x)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we stored the value 2 in x, we can use the variable x to do things like the following:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(x * x)\n", "print(x == x)\n", "print(x > 6)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you figure out what is happening here? \n", "\n", "Variables are not just numbers. They can also be text. These are called strings. For example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "book = \"The Lord of the Flies\"\n", "print(book)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A string in Python must be enclosed with quotes (either single or double quotes). Without those quotes Python thinks its dealing with variables that have been defined earlier. book is a variable to which we assign the string \"The Lord of the Flies\", but that same string is not a variable but a value!\n", "\n", "Variable names can be chosen arbitrarily. *We* give a certain value a name, and we are free to pick one to our liking. It is, however, recommended to use senseful names as we will use the variable names in our code directly and not the values they hold." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# not recommended...\n", "banana = \"The Lord of the Flies\"\n", "print(banana)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You are free to use the name banana to hold the title \"The Lord of the Flies\" but you will agree that this naming is not transparent. \n", "\n", "Variables can vary and we can update our variables. Say we have counted how many books we have in our office:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "number_of_books = 100" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, when we obtain a new book, we can update the number of books accordingly:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "number_of_books = number_of_books + 1\n", "print(number_of_books)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Updates like these happen a lot. Python therefore provides a shortcut and you can write the same thing using +=:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "number_of_books += 5\n", "print(number_of_books)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For now the final interesting thing we would like to mention about variables is that we can assign the value of one variable to another variable. We will explain more about this later on, but here you just need to understand the basic mechanism. Before you evaluate the following code block, can you predict what Python will print?" ] }, { "cell_type": "code", "collapsed": true, "input": [ "book = \"The Lord of the Flies\"\n", "reading = book\n", "print(reading)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that you understand all about assigning values to variables, it is time for our second programming quiz. We want you to write some code that defines a variable, *name*, and assign to it a string that is your name. If your first name is shorter than 5 characters, use your last name. If your last name is also shorter than 5 characters, use the combination of you first and last name." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert your code here\n", "print(name)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------------" ] }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "What we have learnt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To finish this section, here is an overview of the concepts you have learnt. Go through the list and make sure you understand all the concepts.\n", "\n", "- variable\n", "- value\n", "- assignment to variables\n", "- difference between variables and values\n", "- strings\n", "- integers\n", "- varying variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "------" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "String manipulation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many disciplines within the humanities work on texts. Quite naturally programming for the humanities will focus a lot on manipulating texts. In the last quiz you were asked to define a variable that points to a string that represents your name. We have already seen some basic arithmetic in our very first calculation. Not only numbers, but also strings can be added, or, more precisely, *concatenated*, together as well:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "book = \"The Lord of the Flies\"\n", "print(name + \" likes \" + book + \"?\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This string consists of a number of characters. We can access the individual characters with the help of indexing. For example, to find only the first letter of your name, you can type in:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "first_letter = name[0]\n", "print(first_letter)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that to access the first letter, we use the index 0. This might seem odd, but just remember that indexes in Python start at zero." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, if you know the length of your name you can ask for the last letter of your name:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "last_letter = name[# fill in the last index of your name (tip indexes start at 0)]\n", "print(last_letter)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is rather inconvenient having to know how long our strings are if we want to find out what its last letter is. Python provides a simple way of accessing a string from the rear:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "last_letter = name[-1]\n", "print(last_letter)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, there is the function len() which returns the length of a string:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(len(name))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do you understand the following?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(name[len(name)-1])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now can you write some code that defines a variable but_last_letter and assign to it the second-to-last letter of your name?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "but_last_letter = # insert your code here\n", "print(but_last_letter)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You're starting to become a real expert in indexing strings. Now what if we would like to find out what the last two or three letters of our name are? In Python we can use so-called slice-indexes or slices for short. To find the first two letters of our name we type in:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "first_two_letters = name[0:2]\n", "print(first_two_letters)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The 0 index is optional, so we could just as well type in name[:2]. This says take all characters of name until you reach index 2. We can also start at index 2 and leave the end index unspecified:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "without_first_two_letters = name[2:]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because we did not specify the end index, Python continues until it reaches the end of our string. If we would like to find out what the last two letters of our name are, we can type in:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "last_two_letters = name[-2:]\n", "print(last_two_letters)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take a look at the following picture. Do you fully understand it? \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you define a variable middle_letters and assign to it all letters of your name except for the first two and the last two?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "middle_letters = # insert your code here\n", "print(middle_letters)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given the following two words, can you write code that prints out the word *humanities* using only slicing and concatenation? (So, no quotes are allowed in your code.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "word1 = \"human\"\n", "word2 = \"opportunities\"\n", "# insert your code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----------" ] }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "What we have learnt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To finish this section, here is an overview of what we have learnt. Go through the list and make sure you understand all the concepts.\n", "\n", "- concatenation (e.g. addition of strings)\n", "- indexing\n", "- slicing\n", "- len()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-------" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider the sentence below:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sentence = \"Python's name is derived from the television series Monty Python's Flying Circus.\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Words are made up of characters, and so are string objects in Python. As we will see, it is always to be prefered to represent our data as naturally as possible. Now for the sentence above, it seems more natural to describe it in terms of words than in terms of characters. Say we want to access the first word in our sentence. If we type in:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "first_word = sentence[0]\n", "print(first_word)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python only prints the first letter of our sentence. (Think about this if you do not understand why.) We can transform our sentence into a list of words (represented by strings) using the split() function as follows: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "words = sentence.split()\n", "print(words)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By issuing the function split on our sentence, Python splits the sentence on spaces and returns a list of words. In many ways a list functions like a string. We can access all of its components using indexes and we can use slice indexes to access parts of the list. Let's try it!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a small program that defines a variable first_word and assign to it the first word of our word list. Play around a little with the indexes to see if you really understand how it works." ] }, { "cell_type": "code", "collapsed": false, "input": [ "first_word = # insert your code here\n", "print(first_word)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A list acts like a container where we can store all kinds of information. We can access a list using indexes and slices. We can also add new items to a list. For that you use the method append. Let's see how it works. Say we want to keep a list of all our good reads. We start with an empty list and we will add some good books to it:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#start with an empty list\n", "good_reads = []\n", "good_reads.append(\"The Hunger games\")\n", "good_reads.append(\"A Clockwork Orange\")\n", "print(good_reads)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, if for some reason we don't like a particular book anymore, we can change it as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "good_reads[0] = \"Pride and Prejudice\"\n", "print(good_reads)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's another small Quiz! Try to change the title of the second book in our good reads collection." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert your code here\n", "print(good_reads)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-----------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We just changed one element in a list. Note that if you do the same thing for a string, you will get an error:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name = \"Pythen\"\n", "name[4] = \"o\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is because strings (and some other types) are *immutable*. That is, they cannot be changed, as opposed to lists which *are* mutable. Let's explore some other ways in which we can manipulate lists." ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "remove()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's assume our good read collection has grown a lot and we would like to remove some of the books from the list. Python provides the method remove that acts upon a list and takes as its argument the items we would like to remove. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "good_reads = [\"The Hunger games\", \"A Clockwork Orange\", \n", " \"Pride and Prejudice\", \"Water for Elephants\",\n", " \"The Shadow of the Wind\", \"Bel Canto\"]\n", "\n", "good_reads.remove(\"Water for Elephants\")\n", "\n", "print(good_reads)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we try to remove a book that is not in our collection, Python raises an error (don't be afraid, your computer won't break ;-))" ] }, { "cell_type": "code", "collapsed": false, "input": [ "good_reads.remove(\"White Oleander\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a variable good_reads as an empty list. Now add some of your favorite books to it (at least three) and print the last two books you added. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert your code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just as with strings, we can concatenate two lists. Here is an example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#first we specify two lists of strings:\n", "good_reads = [\"The Hunger games\", \"A Clockwork Orange\", \n", " \"Pride and Prejudice\", \"Water for Elephants\",\n", " \"The Shadow of the Wind\", \"Bel Canto\"]\n", "\n", "bad_reads = [\"Fifty Shades of Grey\", \"Twilight\"]\n", "\n", "all_reads = good_reads + bad_reads\n", "print(all_reads)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "sort()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is always nice to organise your bookshelf. We can sort our collection with the following expression:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "good_reads.sort()\n", "print(good_reads)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "nested lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Up to this point, our lists only have consisted of strings. However, a list can contain all kinds of data types, such as integers and even lists! Do you understand what is happening in the following example?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "nested_list = [[1, 2, 3, 4], [5, 6, 7, 8]]\n", "print(nested_list[0])\n", "print(nested_list[0][0])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can put this to use to enhance our good read collection with a score for every book we have. An entry in our collection will consist of a score within the range of 1 and 10 and the title of our book. The first element is the title; the second the score: [title, score]. We initialize an empty list:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "good_reads = []" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And add two books to it:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "good_reads.append([\"Pride and Prejudice\", 8])\n", "good_reads.append([\"A Clockwork Orange\", 9])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Update the good_reads collection with some of your own books and give them all a score. Can you print out the score you gave to the first book in the list? (Tip: you can pile up indexes)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert your code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-----------" ] }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "What we have learnt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To finish this section, here is an overview of the new concepts and functions you have learnt. Go through them and make sure you understand them all.\n", "\n", "- list\n", "- *mutable* versus *immutable*\n", "- .split()\n", "- .append()\n", "- nested lists\n", "- .remove()\n", "- .sort()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-------------" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our little good reads collection is starting to look good and we can perform all kinds of manipulations on it. Now, imagine that our list is large and we would like to look up the score we gave to a particular book. How are we going to find that book? For this purpose Python provides another more appropriate data structure, named dictionary. A dictionary is similar to the dictionaries you have at home. It consists of entries, or keys, that hold a value. Let's define one:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_dict = {\"book\": \"physical objects consisting of a number of pages bound together\",\n", " \"sword\": \"a cutting or thrusting weapon that has a long metal blade\",\n", " \"pie\": \"dish baked in pastry-lined pan often with a pastry top\"}" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take a close look at the new syntax. Notice the curly brackets and the colons. Keys are located at the left side of the colon; values at the right side. To look up the value of a given key, we 'index' the dictionary using that key:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "description = my_dict[\"sword\"]\n", "print(description)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We say 'index', because we use the same syntax with square brackets when indexing lists or strings. The differences is that we don't use a position number to index a dictionary, but a key. Like lists, dictionaries are mutable which means we can add and remove entries from it. Let's define an empty dictionary and add some books to it. The titles will be our keys and the scores their values. Watch the syntax to add a new entry:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "good_reads = {}\n", "good_reads[\"Pride and Prejudice\"] = 8\n", "good_reads[\"A Clockwork Orange\"] = 9" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In a way this is similar to what we have seen before when we altered our book list. There we indexed the list using a integer to access a particular book. Here we directly use the title of the book. Can you imagine why this is so useful?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Update the new good reads datastructure with your own books. Try to print out the score you gave for one of the books." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert your code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "keys(), values()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To retrieve a list of all the books we have in our collection, we can ask the dictionary to return its keys as a list:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "good_reads.keys()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly we can ask for the values:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "good_reads.values()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---------" ] }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "What we have learnt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To finish this section, here is an overview of the new concepts and functions you have learnt. Make sure you understand them all.\n", "\n", "- dictionary\n", "- indexing or accessing keys of dictionaries\n", "- adding items to a dictionary\n", "- .keys()\n", "- .values()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Conditions" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Simple conditions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A lot of programming has to do with executing a certain piece of code if a particular condition holds. We have already seen two conditions at the very beginning of the chapter. Here we give a brief overview. Can you figure our what all of the conditions do?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"2 < 5 =\", 2 < 5)\n", "print(\"3 > 7 =\", 3 >= 7)\n", "print(\"3 == 4 =\", 3 == 4)\n", "print(\"school == homework =\", \"school\" == \"homework\")\n", "print(\"Python != perl =\", \"Python\" != \"perl\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "if, elif and else" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The dictionary is a much better data structure for our good reads collections. However, even with dictionaries we might forget which books we added to the collection. What happens if we try to get the score of a book that is not in our collection (and hopefully never will be...)?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "good_reads[\"Folgert's awesomeness\"]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We get an error. A KeyError, which basically means \"the key you asked me to look up is not in the dictionary\". We will learn a lot more about error handling later, but for now we would like to prevent our program from giving it in the first place. Let's write a little program that prints \"X is in the collection\" if a particular book is in the collection and \"X is NOT in the collection\" if it is not." ] }, { "cell_type": "code", "collapsed": false, "input": [ "book = \"A Clockwork Orange\"\n", "if book in good_reads:\n", " print(book + \" is in the collection\")\n", " print(\"A lot more\")\n", " print(\"Still more to come\")\n", "else:\n", " print(book + \" is NOT in the collection\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A lot of new syntax here. Let's go through it step by step. First we ask if the value we assigned to book is in our collection. The part after if evaluates to either True or to False. Let's type that in:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "book in good_reads" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because our book is not in the collection, Python returns False. Let's do the same thing for a book that we know is in the collection:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"A Clockwork Orange\" in good_reads" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indeed, it is in the collection. Back to our if statement. If the expression after if evaluates to True, our program will go on to the next line and print book + \" is in the collection\". Let's try that as well:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "if \"A Clockwork Orange\" in good_reads:\n", " print(\"Found it!\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "if book in good_reads:\n", " print(\"Found it!\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the print statement in the last code block is not executed. That is because the value we assigned to book is not in our collection and thus the part after if did not evaluate to True. In our little program above we used another statement besides if, namely else. It shouldn't be too hard to figure out what's going on here. The part after else will be executed if the if statement evaluated to False. In English: if the book is not in the collection, print that it is not." ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Indentation!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before we continue, we must first explain to you that the layout of our code is not optional. Unlike in other languages, Python does not make use of curly braces to mark the start and end of expressions. The only delimiter is a colon (:) and the indentation of the code. This indentation must be used consistently throughout your code. The convention is to use 4 spaces as indentation. This means that after you have used a colon (such as in our if statement) the next line should be indented by four spaces more than the previous line." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes we have various conditions that should all evaluate to something different. For that Python provides the elif statement. We use it similar to if and else. Note however that you can only use elif after an if statement! Above we asked whether a book was in the collection. We can do the same thing for parts of strings or for items in a list. For example we could test whether the letter *a* is in the word *banana*:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"a\" in \"banana\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Likewise the following evaluates to False:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"z\" in \"banana\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's use this in an if-elif-else combination:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "word = \"rocket science\"\n", "if \"a\" in word:\n", " print(word + \" contains the letter a\")\n", "elif \"s\" in word:\n", " print(word + \" contains the letter s\")\n", "else:\n", " print(\"What a weird word!\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's practice our new condition skills a little. Write a small program that defines a variable weight. If the weight is > 50 pounds, print \"There is a \$25 charge for luggage that heavy.\" If it is not, print: \"Thank you for your business.\" Change the value of weight to see both statements. (Tip: make use of the < or > operators)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert your code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-------" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "and, or, not" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Up to this point, our conditions have consisted of single expresssions. However, quite often we would like to test for multiple conditions and then execute a particular piece of code. Python provides a number of ways to do that. The first is with the and statement. and allows us to juxtapose two expressions that need to be true in order to make the entire expression evaluate to True. Let's see how that works:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "word = \"banana\"\n", "if \"a\" in word and \"b\" in word:\n", " print(\"Both a and b are in \" + word)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If one of the expressions evaluates to False, nothing will be printed:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "if \"a\" in word and \"z\" in word:\n", " print(\"Both a and z are in \" + word)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Replace and with or in the if statement below. What happens? " ] }, { "cell_type": "code", "collapsed": false, "input": [ "word = \"banana\"\n", "if \"a\" in word and \"z\" in word:\n", " print(\"Both a and b are in \" + word)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the code block below, can you add an else statement that prints that none of the letters were found?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "if \"a\" in word and \"z\" in word:\n", " print(\"Both a and z are in \" + word)\n", "# insert your code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally we can use not to test for conditions that are not true. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "if \"z\" not in word:\n", " print(\"z is not in \" + word)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Objects, such as strings or integers of lists are True because they exist. Empty strings, lists, dictionaries etc on the other hand are False because in a way they do *not* exist. We can use this principle to, for example, only execute a piece of code if a certain list contains any values:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "numbers = [1, 2, 3, 4]\n", "if numbers:\n", " print(\"I found some numbers!\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now if our list were empty, Python wouldn't print anything:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "numbers = []\n", "if numbers:\n", " print(\"I found some numbers!\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you write code that prints \"This is an empty list\" if the provided list does not contain any values?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "numbers = []\n", "# insert your code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you do the same thing, but this time using the function len()?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "numbers = []\n", "# insert your code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-----------" ] }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "What we have learnt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To finish this section, here is an overview of the new functions, statements and concepts we have learnt. Go through them and make sure you understand what their purpose is and how they are used.\n", "\n", "- conditions\n", "- indentation\n", "- if\n", "- elif\n", "- else\n", "- True\n", "- False\n", "- empty objects are false\n", "- not\n", "- in\n", "- and\n", "- or\n", "- multiple conditions\n", "- ==\n", "- <\n", "- >\n", "- !=\n", "- KeyError" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---------" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Programming is most useful if we can perform a certain action on a range of different elements. For example, given a list of words, we would like to know the length of all words, not just one. Now you *could* do this by going through all the indexes of a list of words and print the length of the words one at a time, taking up as many lines of code as you have indices. Needless to say, this is rather cumbersome. \n", "\n", "Python provides the so-called for-statements that allow us to iterate through any iterable object and perform actions on its elements. The basic format of a for-statement is: \n", "\n", " for X in iterable:\n", "\n", "That reads almost like English. We can print all letters of the word *banana* as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for letter in \"banana\":\n", " print(letter)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The code in the loop is executed as many times as their are letters, with a different value for the variable letter at each iteration. Read the previous sentence again." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Likewise we can print all the items that are contained in a list:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "colors = [\"yellow\", \"red\", \"green\", \"blue\", \"purple\"]\n", "for whatever in colors:\n", " print(\"This is color \" + whatever)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since dictionaries are iterable objects as well, we can iterate through our good reads collection as well. This will iterate over the *keys* of a dictionary:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for book in good_reads:\n", " print(book)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also iterate over both the keys and the values of a dictionary, this is done as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "good_reads.items()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for x, y in good_reads.items():\n", " print(x + \" has score \" + str(y))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using items() will, at each iteration, return a nice pair of the key and the value. In the example above the variable book will loop over the keys of the dictionary, and the variable score loops over the respective values.\n", "\n", "The above way is the most elegant way of looping over dictionaries, but try to see if you understand the following alternative as well:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for book in good_reads:\n", " print(book, \"has score\", good_reads[book])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function len() returns the length of an iterable item:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(\"banana\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use this function to print the length of each word in the color list. Write your code in the box below:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "colors = [\"yellow\", \"red\", \"green\", \"blue\", \"purple\"]\n", "# insert your code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now write a small program that iterates through the list colors and appends all colors that contain the letter *r* to the list colors_with_r. (Tip: use colors_with_r.append)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "colors = [\"yellow\", \"red\", \"green\", \"blue\", \"purple\"]\n", "colors_with_r = []\n", "# insert you code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-----------" ] }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "What we have learnt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is an overview of the new concepts, statements and functions we have learnt in this section. Again, go through the list and make sure you understand them all.\n", "\n", "- loop\n", "- for statement\n", "- iterable objects\n", "- variable assignment in a for loop" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Final Quiz!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have covered a lot of ground. Now it is time to put all what we learned together. The following quiz might be quite hard and we would be very impressed if you get it right! \n", "\n", "What we want you to do is write code that counts the number of word tokens in which the letter *a* is present in a small corpus. You need to do this on the basis of a frequency distribution of words that is represented by a dictionary. In this dictionary frequency_distribution, keys are words and values are the frequencies. Assign your value to the variable number_of_as`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "frequency_distribution = {\"Beg\": 1, \"Goddard's\": 1, \"I\": 3, \"them\": 2, \"absent\": 1, \"already\": 1,\n", " \"alteration\": 1, \"amazement\": 2, \"appeared\": 1, \"apprehensively\": 1, \n", " \"associations\": 1, 'clever': 1, 'clock': 1, 'composedly': 1, \n", " 'deeply': 7, 'do': 7, 'encouragement': 1, 'entrapped': 1,\n", " 'expressed': 1, 'flatterers': 1, 'following': 12, 'gone': 9, \n", " 'happening': 4, 'hero': 2, 'housekeeper': 1, 'ingratitude': 1, \n", " 'like': 1, 'marriage': 15, 'not': 25, 'opportunities': 1,\n", " 'outgrown': 1, 'playfully': 2, 'remain': 1, 'required': 2, \n", " 'ripening': 1, 'slippery': 1, 'touch': 1, 'twenty-five': 1,\n", " 'ungracious': 2, 'unwell': 1, 'verses': 1, 'yards': 5}\n", "number_of_as = 0\n", "# insert your code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You've reached the end of the chapter. Ignore the code below, it's just here to make the page pretty:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"styles/custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "/*\n", "Placeholder for custom user CSS\n", "\n", "mainly to be overridden in profile/static/custom/custom.css\n", "\n", "This will always be an empty file in IPython\n", "*/\n", "\n", "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "