Skip to content

Instantly share code, notes, and snippets.

@AbdealiLoKo
Created September 24, 2016 10:24
Show Gist options
  • Save AbdealiLoKo/05b8d2e6ded9bcb58e10deb16c7bacd5 to your computer and use it in GitHub Desktop.
Save AbdealiLoKo/05b8d2e6ded9bcb58e10deb16c7bacd5 to your computer and use it in GitHub Desktop.
WIkimedia Hackathon - Bits Pilani Hyderabad Campus
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Tutorial\n",
"\n",
"Python is a language which prides itself on simplicity of reading and understanding. If you'd like to read more about python go to <https://en.wikipedia.org/wiki/Python_(programming_language)> !\n",
"\n",
"If you'd like to read further than this, or a more basic guide I would recommend: https://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's an example python program to print something on the screen:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('Hello, World!')\n",
"# This is a comment, it isn't run as code, but often they are helpful"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Arithmetic\n",
"\n",
"Like every programming language, Python is a good calculator. Run the block of code below to make sure the answer is right!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"1 + 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The order of operations you learned in school applies, BEDMAS (brackets, exponents, division, multiplication, addition, subtraction):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"8 + 6*2*3 - (15 - 13)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numbers are valid Python code as are the common operators, +, /, * and -. You can write different types of numbers including integers, real numbers (floating point) and negative integers."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"42 + 3.149 + -1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since 42 is literally 42, we call these numbers *literals*. You are literally writing number in your Python code."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Variables\n",
"\n",
"\n",
"In python, variables are similar to C, Java and other languages - they store a certain value. Specifically, in python variables do not have a type. So, a string can also become an integer, and then even into a function!\n",
"Literals are given types of string, integer, float, lists, objects, etc. But the variable's type can change."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = 200.00\n",
"print(\"x =\", x, \"and is of type\", type(x))\n",
"x = \"Hello World!\"\n",
"print(\"x =\", x, \"and is of type\", type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3. Exceptions in Python\n",
"\n",
"Python only understands certain code. When you write something Python doesn't understand it throws an exception and tries to explain what went wrong, but it can only speak in a broken Pythonesque english. Let's see some examples by running these code blocks"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"variable_that_is_undefined"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('Hello'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"2000 / 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python tries to tell you where it stopped understanding, but in the above examples, each program is only 1 line long. \n",
"\n",
"It also tries to show you where on the line the problem happened with caret (\"^\"). \n",
"\n",
"Finally it tells you the type of thing that went wrong, (NameError, SyntaxError, ZeroDivisionError) and a bit more information like \"name 'gibberish' is not defined\" or \"unexpected EOF while parsing\".\n",
"\n",
"Unfortunately you might not find \"unexpected EOF while parsing\" too helpful. EOF stands for End of File, but what file? What is parsing? Python does it's best, but it does take a bit of time to develop a knack for what these messages mean. If you run into an error you don't understand please ask."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 4. Strings\n",
"\n",
"We have already seen a string literal in Python, \"Hello, World!\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"Hello, World!\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Text literals are surrounded by quotes. Without the quotes Hello by itself would be viewed as a variable name. You can use either double quotes (\") or single quotes (') for text literals. Note that in python a character is just a string with 1 character and isn't a different data type.\n",
"\n",
"Let's use strings:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"Hello \" * 5)\n",
"print(\"Hello\" + \"World\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Strings in Python are a bit more complicated because they have their own functions (operations to perform on them) which we can call to modify them:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"Uppercase:\", \"Hello world\".upper())\n",
"print(\"Lowercase:\", \"Hello world\".lower())\n",
"print()\n",
"\n",
"print(\"Strip all whitespace at end of string:\", \"Hello \".strip(), \"world\")\n",
"print(\"Find location of 'world' in string:\", \"Hello world\".find('world'))\n",
"print()\n",
"\n",
"# C like string formatting where the \"%s\" is replaced by the values given\n",
"val = \"Hello %s\"\n",
"replaced_val = val % \"world\"\n",
"print(\"String formatting with %s like C:\", replaced_val)\n",
"\n",
"# We can have template strings where parts are specified on the fly\n",
"val = \"Hello {name}\"\n",
"replaced_val = val.format(name=\"world\")\n",
"print(\"String formatting with .format():\", replaced_val)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To convert another data type like float, integer, etc into a string, you can use the `str()` function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"str(1.0) + \" cm\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Indexed by Zero\n",
"\n",
"For better or worse, everything in Python is index by 0 like C/C++ or Java. We will see this over and over again but for now if you call format like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"{0} like {1}\".format(\"I\", 'Python')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We would call `\"I\"` the 0th string passed into the function `.format()` and `'Python'` the 1st."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Multi line strings\n",
"\n",
"Frequently you will have a string which spans multiple lines. There are 3 ways to handle this in python:\n",
"\n",
" - Use a backslash\n",
" - Triple quotes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Using a backslash is similar to C, where the compiler knows the line is being continued in the next line\n",
"print(\"this is \\\n",
"a multiline string\")\n",
"\n",
"# In multiline strings, even newlines (\\n) is added as a character. So, a newline gets printed.\n",
"print(\"\"\"this is\n",
"a multiline string\"\"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise: Use the help() function\n",
"To find more about a specific enity in python, simply do `help(<entity>)`. Try it out!\n",
" - Find the help for the find() function we used earlier by typing `help(\"abcd\".find)`. How many arguments does it have ?\n",
" - Find the help for the find() function and check what the function `split()` does ?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"help(int)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise: String functions\n",
" - Use the `<str>.replace()` function replace the text in \"Hello world\" to \"Hello python\"\n",
" - Use the `<str>.zfill()` function to take an integer and pad it with zeroes to make it 5 characters long: Define `a = 512` and convert it to `\"00512\"`\n",
" - Use the `<str>.startswith()` function to check if a string starts with a vowel. Run it on `\"python\"` and `\"india\"`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"help(\"abc\".replace)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 5. If Else"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like all languages, Python allows us to conditionally run code.\n",
"\n",
"To have an if condition we need the idea of something being true and something being false. We have `True` or `False` as \"boolean\" values. True would represent OK where as false would represent No or Cancel."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"False is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"True is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"true is False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can write expressions with operations too."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"1 > 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"Cool\".startswith(\"C\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"Cool\".endswith(\"C\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"oo\" in \"Cool\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"42 == 1 # note the double equals sign for equality"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to write an \"if\" statement we need code that spans multiple lines\n",
"\n",
" if condition:\n",
" print(\"Condition is True\")\n",
" else:\n",
" print(\"Condition is False\")\n",
"\n",
"Some things to notice. The if condition ends in a colon (\":\"). In Python blocks of code are indicated with a colon (\":\") and are grouped by white space. Notice the else also ends with a colon (\":\"), \"else:\". Let's try changing the condition and see what happens."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"condition = (1 > 2)\n",
"if condition:\n",
" print(\"Condition is True\")\n",
"else:\n",
" print(\"Condition is False\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"About that white space, consider the following code:\n",
"\n",
" if condition:\n",
" print(\"Condition is True\")\n",
" else:\n",
" print(\"Condition is False\")\n",
" print(\"Condition is True or False, either way this is outputted\")\n",
"\n",
"Since the last print statement isn't indented it gets run after the if block or the else block.\n",
"\n",
"You can play with this. Try indenting the last print statement below and see what happens."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"condition = True\n",
"if condition:\n",
" print(\"Condition is True\")\n",
"else:\n",
" print(\"Condition is False\")\n",
"print(\"Condition is True or False, either way this is outputted\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also use \"and\", \"or\", \"not\" to combine conditions (No ugly && and || here):\n",
"\n",
" True and True is True\n",
" True and False is False\n",
" False and True is False\n",
" False and False is False\n",
" not True is False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise - Boolean values\n",
"Below change the values of the three variables to make the entire \"if condition\" true."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Edit the values of these 3 variables\n",
"boolean_literal = False\n",
"number = 8\n",
"string_literal = \"I like to count sheep before bed.\"\n",
"\n",
"# Don't change the code below this\n",
"if number > 10 and boolean_literal and \"cows\" in string_literal:\n",
" print(\"Success!\")\n",
"else:\n",
" print(\"Try again!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 6. Lists\n",
"\n",
"So far we have numbers, strings, and conditional if statements. Now for our first container - a list.\n",
"\n",
"A list in Python is just like an array or a linked list. They have a defined order and you can add to it or remove from it.\n",
"Let's take a look at some simple lists."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# The empty list\n",
"empty_list = []\n",
"print(empty_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"breakfast_list = [\"bread\", \"butter\", \"jam\"]\n",
"print(breakfast_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"[1,2,3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List literals are all about square brackets (\"[ ]\") and commas (\",\"). You can create a list of literals by wrapping them in square brackets and separating them with commas.\n",
"\n",
"As python doesn't care too much about data types, you can even mix different types of things into the same list; numbers, strings, booleans (unlike arrays)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"[True, 0, \"String\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can put variables into a list and set a variable to a list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"wikipedia = \"wikipedia.org\"\n",
"wiki_sites_list = [\"wikidata.org\", wikipedia]\n",
"print(wiki_sites_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like strings, lists have their own operations. \"append\" is an interesting one. \"append\" lets you add an item to the end of a list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"wiki_sites_list = [\"wikidata.org\", \"wikipedia.org\"]\n",
"wiki_sites_list.append(\"wikisource.org\")\n",
"print(wiki_sites_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"wiki_sites_list[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There is that 0 indexing again. The first element of the list is given index value 0."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"These are some of the wikimedia sites: {0}, {1}, {2}\".format(wiki_sites_list[0], wiki_sites_list[1], wiki_sites_list[2]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Length\n",
"We can also find the lenght of the list using the `len` function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"len(wiki_sites_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Splicing\n",
"Parts of a list can also be gotten using the splice operator. To use the splice operator, square brackets with color (`:`) is used:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"nums = [0,1,2,3,4,5]\n",
"print(\"2:4 ->\", nums[2:4]) # Takes everything 2nd to 3rd term\n",
"print(\"1:3 ->\", nums[1:3]) # Takes everything from 1st to 2nd term\n",
"print(\"2: ->\", nums[2:]) # Takes everything from 2nd to last term\n",
"print(\":4 ->\", nums[:4]) # Takes everything from start to the 3rd term\n",
"print(\":-2 ->\", nums[:-2]) # Takes everything from start to the 3rd last term"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise - Use the functions .join() and .split()\n",
" - Use the `<str>.join()` function to join a list of items using a comma: Convert `[\"bread\", \"butter\", \"jam\"]` -> `\"bread, butter, jam\"`\n",
" - Use the `<str>.split()` function to split a string into a list: Convert back the earlier `\"bread, butter, jam\"` -> `[\"bread\", \"butter\", \"jam\"]`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"help(str.join)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 7. Loops\n",
"\n",
"Indexes are useful, but lists really shine when you start looping.\n",
"\n",
"Loops let you do something for each item in a list. They look like this:\n",
"\n",
" for item in my_list:\n",
" print(item) # Do any action per item in the list\n",
"\n",
"\"for\" and \"in\" are required. \"my_list\" can be any variable or literal which is like a list. \"item\" is the name you want to give each item of the list in the indented block as you iterate through. We call each step where item has a new value an iteration."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for num in [1, 2, 3]:\n",
" print(num)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In python, we don't use constructs like `for ( int i = 0; i < maximum; i++ )` which are just confusing, rather we create a \"range\" of integers from 0 to maximum and loop over that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"maximum = 10\n",
"print(\"This is the range object:\", range(0, maximum))\n",
"print(\"This is what it creates when it's used in a loop or other functions:\", list(range(0, maximum)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And then we can use that with a loop to print a list of squares."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for number in range(0, 10):\n",
" print(\"{0} squared is {1}\".format(number, number*number))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 8. Dictionaries\n",
"\n",
"We have come a long way! Just one more section. Dictionaries are another container like lists, but instead of being index by a number like 0 or 1 it is indexed by a key which can be almost anything. The name comes from being able to use it to represent a dictionary.\n",
"\n",
"List literals use square brackets (\"[]\") but dictionaries use braces (\"{}\").\n",
"\n",
" {\"wikipedia.org\": \"The free encyclopedia\", \n",
" \"wikisource.org\": \"The free library\"}\n",
"\n",
"In a dictionary the key comes first followed by a colon (\":\") than the value then a comma (\",\") then another key and so on. This is one situation where a colon doesn't start a block."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"wiki_sites = {\n",
" \"wikipedia.org\": \"The free encyclopedia\", \n",
" \"wikisource.org\": \"The free library\"\n",
"}\n",
"wiki_sites[\"wikipedia.org\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can loop over the keys in a dictionary to list all of our definitions..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"for key in wiki_sites:\n",
" print('The Key is \"{0}\" and the value is \"{1}\"'.format(key, wiki_sites[key]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In fact, dictionaries can contain any type of values. Hence, you can have a list as the value of a dictionary:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"wiki_language_sites = {\n",
" \"wikipedia.org\": [\"en.wikipedia.org\", \"ml.wikipedia.org\", \"ta.wikipedia.org\"], \n",
" \"wikisource.org\": [\"en.wikisource.org\", \"ml.wikisource.org\", \"ta.wikisource.org\"]\n",
"}\n",
"wiki_language_sites[\"wikipedia.org\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also get the keys and values as lists:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"Keys:\", list(wiki_sites.keys()))\n",
"print(\"Values:\", list(wiki_sites.values()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise: Loop over the keys, values, and items\n",
" - Loop over a dict using `for key in <dict>:` and print all the keys. Verify that the same result is got when using `for key in <dict>.keys()`.\n",
" - Loop over the values of a dict by first getting all the values using the `<dict>.values()` function.\n",
" - Loop over the keys and values of a dict by requesting 2 items from the list. Do `for key, val in dict.items():` and try printing both the key and value side by side."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 9. Functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To avoid code repetition and to break code into smaller intelligible blocks, functions are useful. A function takes in certain variables (arguments) and gives out a return value. The arguments and return value do not need to define a data type again, as python isn't too worried about data types."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A function is defined using the `def` keyword:\n",
"\n",
" def <function name>(<arg1>, <arg2>, <arg3>, ..., <argN>):\n",
" <function body>\n",
" return <value to return>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def sum2(x1, x2):\n",
" return x1 + x2\n",
"\n",
"print(\"2 + 3 =\", sum2(2, 3))\n",
"print('\"a\" + \"b\" =', sum2(\"a\", \"b\"))\n",
"print('[1] + [2,3] =', sum2([1], [2, 3]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because there's no data type for the arguments and return type, we can give strings, ints, floats, lists, etc. Anything that supports the `+` operator in our example. But if the arguments given cannot be operated on by `+` it gives an error. And it is the job of the developer of the function to ensure that the arguments given are sane:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def sum2(x1, x2):\n",
" return x1 + x2\n",
"\n",
"print('1 + \"s\" =', sum2(1, \"s\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python also supports assigning values using the argument name. When using argument names (also called keyword arguments - kwargs) the order of the arguments do not matter as the name of the argument is used. This gives flexibility, as you don't have to remember which argument comes first and which comes second! Let's see it in action:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def difference2(x1, x2):\n",
" return x1 - x2\n",
"\n",
"print(\"2 - 3 =\", difference2(2, 3))\n",
"print(\"2 - 3 =\", difference2(x1=2, x2=3))\n",
"print(\"2 - 3 =\", difference2(x2=3, x1=2)) # We give x2 before x1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Not all functions need to return a value. And by default if a return value is not given, it returns `None`.\n",
"\n",
"Functions can also have default values for arguments:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def power(base, exponent=2):\n",
" return base ** exponent\n",
"\n",
"print('5 power -1 is', power(5, -1))\n",
"print('power(5) uses exponent 2 by default:', power(5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise - Fibonacci function\n",
"Write a function that prints the values of the fibonacci series until a given maximum value. For example, `print_fibonacci(10)` would print:\n",
"\n",
" >>> print_fibonacci(10)\n",
" 1, 1, 2, 3, 5, 8\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 10. Classes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python also has Object Oriented Programming (OOP) in it's structure. A class is a template which holds class variables and functions (methods) and operates on the class itself.\n",
"\n",
"To use a class, objects which conform to the class template need to be created:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class WebSite:\n",
" def __init__(self):\n",
" self.url = \"\"\n",
" self.description = \"\"\n",
"\n",
"w1 = WebSite() # w1 is an object of class WebSite\n",
"print(\"Type of w1:\", type(w1))\n",
"w1.description = \"The free encyclopedia\"\n",
"w1.url = \"wikipedia.org\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `__init__()` function is the class constructor and can not return anything. The first argument `self` is the object that's being created. Hence, in our above class, when the object is created, it has 2 member variables `url` and `description` which are assigned to empty string (`\"\"`) in the constructor.\n",
"\n",
"Other than the variables created in the class, python can dynamically add more variables in the class:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"w1.visits = 1000\n",
"w1.subsites = [\"en.wikipedia.org\", \"ml.wikipedia.org\", \"ta.wikipedia.org\"]\n",
"print(w1.visits, w1.subsites)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Classes can also define functions which can be used to perform functions using the object variables.\n",
"\n",
"Object methods start with the keyword `self` normally. When `w1.function()` is used, the object before the period (`w1`) is passed to `function()`'s first argument. Hence, `function()` would be defined as `def function(self):` where `self` is a reference to the object itself.\n",
"\n",
"Here's an example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class WebSite:\n",
" def __init__(self, description=\"\", url=\"\"):\n",
" self.url = url\n",
" self.description = description\n",
"\n",
" # A class method or a class function which can be called usingt the object using: <obj>.subsite(\"arg\")\n",
" # Which the class receives as: substitute(<obj>, \"arg\")\n",
" def subsite(self, subname):\n",
" return subname + \".\" + self.url\n",
"\n",
"w1 = WebSite(description=\"The free encyclopedia\", url=\"wikipedia.org\") # w1 is an object of class WebSite\n",
"w1.subsites = [w1.subsite('en'), w1.subsite('ml'), w1.subsite('ta')]\n",
"print(w1.subsites)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise - Create a WebSite object for each item in the `wiki_sites` dictionary\n",
"\n",
"Loop over every (key, value) pair in the dictionary `wiki_sites` and create a list of `WebSite` objects using the `WebSite` class.\n",
"\n",
" - The key of the dictionary should be stored in the `url` of the object.\n",
" - The value of the dictionary should be stored in the `description` of the object."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Leave the definitions of the class and dictionary as is\n",
"class WebSite:\n",
" def __init__(self, description=\"\", url=\"\"):\n",
" self.url = url\n",
" self.description = description\n",
"\n",
"wiki_sites = {\n",
" \"wikipedia.org\": \"The free encyclopedia\", \n",
" \"wikisource.org\": \"The free library\"\n",
"}\n",
"\n",
"# Write below. Loop over every item in wiki_sites and create a list of WebSite objects."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise - Create a class to store a mediawiki page\n",
"Create a class which can store a mediawiki page (has content and name of the page) and create 2 functions to:\n",
"\n",
" 1. Check if the page is empty (the content is an empty string `\"\"`): `<obj>.empty()` which returns `True` or `False`\n",
" 2. Generate the URL of the page using a base url: `<obj>.url()` returns `\"wikipedia.org/wiki/<page name>\"`)\n",
"\n",
"Below you will find some example code to test your class"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Add the class definition here\n",
"\n",
"# The following should work:\n",
"page = WikiPage(name=\"MyPage\", content=\"Hello there ! This is a small example page.\")\n",
"print(\"The name of the page is:\", page.name)\n",
"print(\"The page has the following content:\")\n",
"print(page.content)\n",
"print(\"The url of the page is:\", page.url())\n",
"if page.empty():\n",
" print(\"The page is empty\")\n",
"else:\n",
" print(\"The page is not empty\")"
]
}
],
"metadata": {
"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.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment