{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Numbers, Strings, and Lists" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Python supports a number of built-in types and operations. This section covers the most common types, but information about additional types is available [here](https://docs.python.org/3/library/stdtypes.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A *type*, to a computer, is a pair of a domain (e.g., the real numbers) and a set of operations (e.g., addition, multiplication) on them.\n", "\n", "That is somewhat mathematical, and there's an important message:\n", "by choosing a type you're determining if operators and function work on them and what they do." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic numeric types" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The basic data numeric types are similar to those found in other languages, including:\n", "\n", "**Integers (``int``)**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "i = 1\n", "j = 219089\n", "k = -21231" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(i, j, k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note the brackets." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Floating point values (``float``)**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "a = 4.3\n", "b = -5.2111222\n", "c = 3.1e33" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(a, b, c)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Complex values (``complex``)**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = complex(4., -1.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(d)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Manipulating these behaves the way you would expect, so an operation (``+``, ``-``, ``*``, ``**``, etc.) on two values of the same type produces another value of the same type (with one, exception, ``/``, see below), while an operation on two values with different types produces a value of the more 'advanced' type:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Adding two integers gives an integer:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 + 3" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Multiplying two floats gives a float:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3. * 2." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Subtracting two complex numbers gives a complex number:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "complex(2., 4.) - complex(1., 6.)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Multiplying an integer with a float gives a float:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "3 * 9.2" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Multiplying a float with a complex number gives a complex number:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2. * complex(-1., 3.)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Multiplying an integer and a complex number gives a complex number:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "8 * complex(-3.3, 1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "However, the division of two integers gives a float:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3 / 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note you can also specifically request integer division:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3 // 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The operator for raising one value to the power of another is ``**``. Try calculating $4^3$, $2+3.4^2$, and $(1 + i)^2$. What is the type of the output in each case, and does it make sense?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# enter your solution here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Strings (``str``) are sequences of characters:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = \"roof tile roof roof\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "You can use either single quotes (``'``), double quotes (``\"``), or triple quotes (``'''`` or ``\"\"\"``) to enclose a string (the last one is used for multi-line strings). To include single or double quotes inside a string, you can either use the opposite quote to enclose the string:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "\"I'm\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "'\"hello\"'" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "or you can *escape* them:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "'I\\'m'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "\"\\\"hello\\\"\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "You can access individual characters or chunks of characters using the item notation with square brackets``[]``:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "s = \"roof tile roof roof\"\n", "s[5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that in Python, indexing is *zero-based*, which means that the first element in a list is zero:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s[0]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Note that strings are **immutable**, that is you cannot change the value of certain characters without creating a new string:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s[5] = 'r'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can easily find the length of a string:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(s)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "You can use the ``+`` operator to combine strings:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"hello,\" + \" \" + \"world!\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Finally, strings have many **methods** associated with them, here are a few examples:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s.upper() # An uppercase version of the string" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s.index('tile') # An integer giving the position of the sub-string" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s.split() # A list of strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "There are several kinds of ways of storing sequences in Python, the simplest being the ``list``, which is simply a sequence of *any* Python object." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "li = [4, 5.5, \"roof\"]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Accessing individual items is done like for strings" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "li[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "li[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "li[2]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Values in a list can be changed, and it is also possible to append or insert elements:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "li[1] = -2.2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "li" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "li.append(-3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "li" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "li.insert(1, 2.718)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "li" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As for strings, you can find the length of a list (the number of elements) with the ``len`` function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len([1,2,3,4,5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slicing" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We already mentioned above that it is possible to access individual elements from a string or a list using the square bracket notation. You will also find this notation for other object types in Python, for example tuples or Numpy arrays, so it's worth spending a bit of time looking at this in more detail.\n", "\n", "In addition to using positive integers, where ``0`` is the first item, it is possible to access list items with *negative* indices, which counts from the end: ``-1`` is the last element, ``-2`` is the second to last, etc:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "li = [4, 67, 4, 2, 4, 6]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "li[-1]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "You can also select **slices** from a list with the ``start:end:step`` syntax. Be aware that the last element is *not* included!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "li[0:2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "li[:2] # ``start`` defaults to zero" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "li[2:] # ``end`` defaults to the last element " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "li[::2] # specify a step size" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given a string such as the one below, make a new string that does not contain the word ``roof``:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = \"Hello, roof world!\"\n", "\n", "# enter your solution here\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try changing the string above to see if your solution works (you can assume that ``roof`` appears only once in the string)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A note on Python objects (demo)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Most things in Python are objects. But what is an object?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Every constant, variable, or function in Python is actually a object with a\n", "type and associated attributes and methods. An *attribute* a property of the\n", "object that you get or set by giving the ``.``, for example ``img.shape``. A *method* is a function that the object provides, for example ``img.argmax(axis=0)`` or ``img.min()``.\n", " \n", "Use tab completion in IPython to inspect objects and start to understand\n", "attributes and methods. To start off create a list of 4 numbers:\n", "\n", " li = [3, 1, 2, 1]\n", " li.\n", "\n", "This will show the available attributes and methods for the Python list\n", "``li``.\n", "\n", "**Using ````-completion and help is a very efficient way to learn and later\n", "remember object methods!**\n", "\n", " In [2]: li.\n", " li.append li.copy li.extend li.insert li.remove li.sort\n", " li.clear li.count li.index li.pop li.reverse \n", " \n", "If you want to know what a function or method does, you can use a question mark ``?``:\n", " \n", " In [9]: li.append?\n", " Type: builtin_function_or_method\n", " String Form:\n", " Docstring: L.append(object) -> None -- append object to end" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l=[3,4,5]\n", "l." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the following string, find out (with code) how many times the letter \"A\" appears." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = \"CAGTACCAAGTGAAAGAT\"\n", "\n", "# your solution here\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given two lists, try making a new list that contains the elements from both previous lists:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3]\n", "b = [4, 5, 6]\n", "\n", "# your solution here\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that there are several possible solutions!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dynamic typing" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "One final note on Python types: if you know languages like C, Java or, say, Pascal, you will remember you had to declare variables. Essentially, types are bound to names in such “statically typed” languages. In Python, on the other hand, types sit on the values, and names just point there; there's nothing that keeps you from having a single name point to differently typed values (except perhaps common sense in many cases). Jargon calls this *dynamic typing*." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 1\n", "type(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 2.3\n", "type(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 'hello'\n", "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Converting between types" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "There may be cases where you want to convert a string to a floating point value, and integer to a string, etc. For this, you can simply use the ``int()``, ``float()``, and ``str()`` functions:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "int('1')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "float('4.31')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "int('5') + float('4.31')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is different from:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'5' + '4.31'" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "Similarly:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "str(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str(4.5521)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str(3) + str(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Be aware of this for example when connecting strings with numbers, as you can only concatenate identical types this way:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'The value is ' + 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead do:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'The value is ' + str(3)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 1 }