{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Lists, dictionaries, functions, data types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variables are declared in Python using standard \"=\" operator, as shown below:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = 1\n", "b = 2.2\n", "c = 1 + 2j\n", "d = \"Hello world!\"\n", "e = \"Witaj świecie!\"\n", "f = 2 == 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we see, contrary to many other programming languages, there is need to declare datatypes. Based on the given value the most probable data type is assumed. We can always check the type using built-in \"type()\" function." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", "\n", "\n", "\n" ] } ], "source": [ "print(type(a))\n", "print(type(b))\n", "print(type(c))\n", "print(type(d))\n", "print(type(e))\n", "print(type(f))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If there is a need, we can set the datatype explicitely." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n" ] } ], "source": [ "a = float(1)\n", "b = str(2.2)\n", "print(type(a))\n", "print(type(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Knowing the datatype is very important, as operators such as +, -, \\*, \\*\\* (power) work differently depending on the data type." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex. 1\n", "Check the value and datatype of:\n", "1. sum of *int* and *float*\n", "2. product of *float* and *complex*\n", "3. sum of *str* and *str*\n", "4. product of *int* and *str*" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Here goes your code\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The native python structure for agregating variables is a list. We declare it using square brackets:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1.0, '2.2', (1+2j), 'Hello world!']\n" ] } ], "source": [ "l1 = [1,2,3]\n", "l2 = [a,b,c,d]\n", "print(l2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we see, a list does not require, that all it's elements are of the same type. We can define 2- or n-D lists, but they are nothing more than lists containing further lists. There is therefore no such thing as a dimension of a list." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2, 3, 4], [3, 4, 5]]\n", "[[2, 3, 4], [3, 4, 5], 'dfs', [2, [23, 3, [[23]]]]]\n" ] } ], "source": [ "l3 = [[2,3,4], [3,4,5]]\n", "print(l3) \n", "l4 = [[2,3,4], [3,4,5], \"dfs\", [2,[23,3,[[23]]]]]\n", "print(l4) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can concatenate lists exactly in the same way as we did with string." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2, 3, 4], [3, 4, 5], [2, 3, 4], [3, 4, 5], 'dfs', [2, [23, 3, [[23]]]]]\n", "[[2, 3, 4], [3, 4, 5], [2, 3, 4], [3, 4, 5], [2, 3, 4], [3, 4, 5]]\n" ] } ], "source": [ "print(l3 + l4)\n", "print(l3*3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is no point of having a list if we cannot acces its elements. Python gives us various methods:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 = [0,1,2,3,4,5,6,7,8,9,10]\n", "\n", "#Accesing a single element:\n", "l1[3]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5, 6, 7]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Accesing subsequence of elements:\n", "l1[3:8]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5, 6, 7, 8, 9, 10]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Accesing all elements starting from index 3:\n", "l1[3:]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Accesing all elements except last three:\n", "l1[:-3]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 3, 6, 9]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Accesing every third element:\n", "l1[::3]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[10, 7, 4, 1]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Accesing every third element going backwards:\n", "l1[::-3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Loops\n", "Python offers us standard **for** and **while** loops. The syntax is as folows: " ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "9\n", "8\n", "7\n", "6\n" ] } ], "source": [ "i = 10\n", "while i > 5:\n", " print(i)\n", " i -= 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two facts should be noted. Firstly, python syntax does not require braces or semicolons. On the other hand, it relies on proper indentation. The following code will therefore not work:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "ename": "IndentationError", "evalue": "expected an indented block (, line 3)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m print(i)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" ] } ], "source": [ "i = 10\n", "while i > 5:\n", "print(i)\n", "i -= 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Second observation refers to the **range()** function. It is commonly used and returns the object of type **iterable** containing the sequence of numbers. This is a special type designed for loop expressions. The syntax is **range(start = 0, end, step = 1)**. Note, that it is impossible to acces specific element of **range**." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in range(3,10):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "5\n", "7\n", "9\n" ] } ], "source": [ "for i in range(3,10,2):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "7\n", "4\n" ] } ], "source": [ "for i in range(10,1,-3):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for i in range(10,10):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are not limited to object of type **iterable**, we can iterate over normal lists as well." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3, 4]\n", "[3, 4, 5]\n", "dfs\n", "[2, [23, 3, [[23]]]]\n" ] } ], "source": [ "for i in l4:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Loop expressions may also serve us as an inline list constructor." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 5, 10, 17, 26, 37, 50, 65, 82]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l5 = [i**2 + 1 for i in range(10)]\n", "l5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex. 2\n", "Construct a list containing squares of all even positive integers less than 100. Then print every eleventh. " ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Here goes your code. Try to do it in two lines.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions\n", "In Python we don't have to declare neither the type which is returned by a function, nor the type of its arguments. The syntax is as follows:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n", "-2e+23\n", "Hello World! Hello World! Hello World! Hello World! Hello World! \n" ] } ], "source": [ "def multiply(a,b):\n", " return a*b\n", "\n", "print( multiply(2, 4) )\n", "print( multiply(-2, 1e23) )\n", "print( multiply(\"Hello World! \", 5) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions can return multiple values as well. " ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(6, 8)\n", "6\n", "8\n" ] } ], "source": [ "def sum_multiply(a,b):\n", " return a+b, a*b\n", "\n", "result = sum_multiply(2, 4)\n", "result1, result2 = sum_multiply(2, 4)\n", "_, result2 = sum_multiply(2, 4)\n", "\n", "print(result)\n", "print(result1)\n", "print(result2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex. 3\n", "Define a function which given an intiger $n$, returns a list with first $n$ elements of Fibonacci sequence." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Here goes your code.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionaries\n", "Python dictionary is an unordered set, in which every element is labeled. They are written with curly brackets and contain keys and values." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'123-456-789'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phonebook = {\"Joe\": \"123-456-789\", \"Mary\": \"222-333-444\", \"Jane\": \"987-654-321\"}\n", "phonebook[\"Joe\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionaries can be nested and do not require uniformity of entry type." ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Baker Streen 221\n", "222-333-444\n", "987-654-321\n" ] } ], "source": [ "phonebook = {}\n", "phonebook[\"Joe\"] = {\"tel\": \"123-456-789\" , \"address\": \"Baker Streen 221\"}\n", "phonebook[\"Mary\"] = {\"tel\": \"222-333-444\"}\n", "phonebook[\"Jane\"] = \"987-654-321\"\n", "\n", "print(phonebook[\"Joe\"][\"address\"])\n", "print(phonebook[\"Mary\"][\"tel\"])\n", "print(phonebook[\"Jane\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is quite often that we use dictionaries to store data arrays." ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[400, 361, 324, 289, 256, 225, 196, 169, 144, 121, 100, 81, 64, 49, 36, 25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]\n" ] } ], "source": [ "parabola = {\"x\": [x for x in range(-20,21)], \"y\": [x**2 for x in range(-20,21)]}\n", "print(parabola[\"y\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " A key doesn't have to be string, it can be every *hashable* object, such as for example an integer or float." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1156" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares = {}\n", "for i in range(100):\n", " squares[i] = i**2\n", "\n", "squares[34]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can learn if a key is already in the dictionary in three ways:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n", "dict_keys([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])\n", "4\n" ] }, { "ename": "KeyError", "evalue": "101", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;31m#Or in a primitive way:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msquares\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msquares\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m101\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 101" ] } ], "source": [ "# Checking a single element\n", "print(2 in squares)\n", "print(101 in squares)\n", "\n", "# printing all keys\n", "print(squares.keys())\n", "\n", "#Or in a primitive way:\n", "print(squares[2])\n", "print(squares[101])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex. 4\n", "Define a dictionary, which for every integer less than 1000 gives a number of it's divisors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Here goes your code\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tuples\n", "Tuples are ordered collections which are very similar to the lists. One of the few differences is, that elements of tuple are unchangeable." ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "100\n", "2\n" ] }, { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtab\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtup\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mtup\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "tab = [1,2,3]\n", "tup = (1,2,3)\n", "print(tab[1])\n", "tab[1] = 100\n", "print(tab[1])\n", "print(tup[1])\n", "tup[1] = 100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is important to remember, that if you want to create single element tuple it requires adding a comma after an element." ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n" ] } ], "source": [ "thistuple = (\"apple\",)\n", "print(type(thistuple))\n", "\n", "#NOT a tuple\n", "thistuple = (\"apple\")\n", "print(type(thistuple))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex. 5\n", "Check if joining two tuples, accesing a range of values and negative indexing works in the same way with tuples as with lists." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Here goes your code\n" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda env:anaconda2]", "language": "python", "name": "conda-env-anaconda2-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 2 }