{ "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", "
Python Programming for the Humanities by http://fbkarsdorp.github.io/python-course is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at https://github.com/fbkarsdorp/python-course.