Created
May 16, 2018 05:36
-
-
Save rohitranjan1991/d22a9a84700864839d07221f320a232e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Python :\n", | |
" \n", | |
"Python is a high-level, dynamically typed multiparadigm programming language. Python code is often said to be almost like pseudocode, since it allows you to express very powerful ideas in very few lines of code while being very readable. As an example, here is an implementation of the classic quicksort algorithm in Python:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 1, 2, 3, 6, 8, 10]\n" | |
] | |
} | |
], | |
"source": [ | |
"def quicksort(arr):\n", | |
" if len(arr) <= 1:\n", | |
" return arr\n", | |
" eip = arr[len(arr) // 2]\n", | |
" mlblr = [x for x in arr if x < eip]\n", | |
" eip_in = [x for x in arr if x == eip]\n", | |
" eip_out = [x for x in arr if x > eip]\n", | |
" return quicksort(mlblr) + eip_in + quicksort(eip_out)\n", | |
"\n", | |
"print(quicksort([3,6,8,10,1,2,1]))\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Python versions :\n", | |
" \n", | |
"There are currently two different supported versions of Python, 2.7 and 3.5. Somewhat confusingly, Python 3.0 introduced many backwards-incompatible changes to the language, so code written for 2.7 may not work under 3.5 and vice versa. For this class all code will use Python 3.5.\n", | |
"\n", | |
"You can check your Python version at the command line by running python --version.\n", | |
"\n", | |
"\n", | |
"Basic data types :\n", | |
" \n", | |
"Like most languages, Python has a number of basic types including integers, floats, booleans, and strings. These data types behave in ways that are familiar from other programming languages.\n", | |
"\n", | |
"Numbers: Integers and floats work as you would expect from other languages:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<class 'int'>\n", | |
"3\n", | |
"4\n", | |
"2\n", | |
"6\n", | |
"9\n", | |
"4\n", | |
"8\n", | |
"<class 'float'>\n", | |
"2.5 3.5 5.0 6.25\n" | |
] | |
} | |
], | |
"source": [ | |
"eip = 3\n", | |
"print(type(eip)) # Prints \"<class 'int'>\"\n", | |
"print(eip) # Prints \"3\"\n", | |
"print(eip + 1) # Addition; prints \"4\"\n", | |
"print(eip - 1) # Subtraction; prints \"2\"\n", | |
"print(eip * 2) # Multiplication; prints \"6\"\n", | |
"print(eip ** 2) # Exponentiation; prints \"9\"\n", | |
"eip += 1\n", | |
"print(eip) # Prints \"4\"\n", | |
"eip *= 2\n", | |
"print(eip) # Prints \"8\"\n", | |
"mlblr = 2.5\n", | |
"print(type(mlblr)) # Prints \"<class 'float'>\"\n", | |
"print(mlblr, mlblr + 1, mlblr * 2, mlblr ** 2) # Prints \"2.5 3.5 5.0 6.25\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Note that unlike many languages, Python does not have unary increment (x++) or decrement (x--) operators.\n", | |
"\n", | |
"Python also has built-in types for complex numbers; you can find all of the details in the documentation.\n", | |
"\n", | |
"Booleans: Python implements all of the usual operators for Boolean logic, but uses English words rather than symbols (&&, ||, etc.):" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<class 'bool'>\n", | |
"False\n", | |
"True\n", | |
"False\n", | |
"True\n" | |
] | |
} | |
], | |
"source": [ | |
"eip = True\n", | |
"mlblr = False\n", | |
"print(type(eip)) # Prints \"<class 'bool'>\"\n", | |
"print(eip and mlblr) # Logical AND; prints \"False\"\n", | |
"print(eip or mlblr) # Logical OR; prints \"True\"\n", | |
"print(not eip) # Logical NOT; prints \"False\"\n", | |
"print(eip != mlblr) # Logical XOR; prints \"True\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Strings: Python has great support for strings:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"hello\n", | |
"5\n", | |
"hello world\n", | |
"hello hello world 12\n" | |
] | |
} | |
], | |
"source": [ | |
"eip = 'hello' # String literals can use single quotes = \"world\" # or double quotes; it does not matter.\n", | |
"mlblr = \"world\"\n", | |
"print(eip) # Prints \"hello\"\n", | |
"print(len(eip)) # String length; prints \"5\"\n", | |
"mlblr = eip + ' ' + mlblr # String concatenation\n", | |
"print(mlblr) # prints \"hello world\"\n", | |
"mlblr12 = '%s %s %d' % (eip, mlblr, 12) # sprintf style string formatting\n", | |
"print(mlblr12) # prints \"hello world 12\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"String objects have a bunch of useful methods; for example:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello\n", | |
"HELLO\n", | |
" hello\n", | |
" hello \n", | |
"he(ell)(ell)o\n", | |
"world\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_in = \"hello\"\n", | |
"print(eip_in.capitalize()) # Capitalize a string; prints \"Hello\"\n", | |
"print(eip_in.upper()) # Convert a string to uppercase; prints \"HELLO\"\n", | |
"print(eip_in.rjust(7)) # Right-justify a string, padding with spaces; prints \" hello\"\n", | |
"print(eip_in.center(7)) # Center a string, padding with spaces; prints \" hello \"\n", | |
"print(eip_in.replace('l', '(ell)')) # Replace all instances of one substring with another;\n", | |
" # prints \"he(ell)(ell)o\"\n", | |
"print(' world '.strip()) # Strip leading and trailing whitespace; prints \"world\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"You can find a list of all string methods in the documentation.\n", | |
"\n", | |
"\n", | |
"Containers\n", | |
"Python includes several built-in container types: lists, dictionaries, sets, and tuples.\n", | |
"\n", | |
"\n", | |
"Lists\n", | |
"A list is the Python equivalent of an array, but is resizeable and can contain elements of different types:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[3, 1, 2] 2\n", | |
"2\n", | |
"[3, 1, 'foo']\n", | |
"[3, 1, 'foo', 'bar']\n", | |
"bar [3, 1, 'foo']\n" | |
] | |
} | |
], | |
"source": [ | |
"mlblr = [3, 1, 2] # Create a list\n", | |
"print(mlblr, mlblr[2]) # Prints \"[3, 1, 2] 2\"\n", | |
"print(mlblr[-1]) # Negative indices count from the end of the list; prints \"2\"\n", | |
"mlblr[2] = 'foo' # Lists can contain elements of different types\n", | |
"print(mlblr) # Prints \"[3, 1, 'foo']\"\n", | |
"mlblr.append('bar') # Add a new element to the end of the list\n", | |
"print(mlblr) # Prints \"[3, 1, 'foo', 'bar']\"\n", | |
"x_out = mlblr.pop() # Remove and return the last element of the list\n", | |
"print(x_out, mlblr) # Prints \"bar [3, 1, 'foo']\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"As usual, you can find all the gory details about lists in the documentation.\n", | |
"\n", | |
"Slicing: In addition to accessing list elements one at a time, Python provides concise syntax to access sublists; this is known as slicing:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0, 1, 2, 3, 4]\n", | |
"[2, 3]\n", | |
"[2, 3, 4]\n", | |
"[0, 1]\n", | |
"[0, 1, 2, 3, 4]\n", | |
"[0, 1, 2, 3]\n", | |
"[0, 1, 8, 9, 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_list = list(range(5)) # range is a built-in function that creates a list of integers\n", | |
"print(eip_list) # Prints \"[0, 1, 2, 3, 4]\"\n", | |
"print(eip_list[2:4]) # Get a slice from index 2 to 4 (exclusive); prints \"[2, 3]\"\n", | |
"print(eip_list[2:]) # Get a slice from index 2 to the end; prints \"[2, 3, 4]\"\n", | |
"print(eip_list[:2]) # Get a slice from the start to index 2 (exclusive); prints \"[0, 1]\"\n", | |
"print(eip_list[:]) # Get a slice of the whole list; prints \"[0, 1, 2, 3, 4]\"\n", | |
"print(eip_list[:-1]) # Slice indices can be negative; prints \"[0, 1, 2, 3]\"\n", | |
"eip_list[2:4] = [8, 9] # Assign a new sublist to a slice\n", | |
"print(eip_list) # Prints \"[0, 1, 8, 9, 4]\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We will see slicing again in the context of numpy arrays.\n", | |
"\n", | |
"Loops: You can loop over the elements of a list like this:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"cat\n", | |
"dog\n", | |
"monkey\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_list = ['cat', 'dog', 'monkey']\n", | |
"for animal in eip_list:\n", | |
" print(animal)\n", | |
"# Prints \"cat\", \"dog\", \"monkey\", each on its own line." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"If you want access to the index of each element within the body of a loop, use the built-in enumerate function:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"#1: cat\n", | |
"#2: dog\n", | |
"#3: monkey\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_list = ['cat', 'dog', 'monkey']\n", | |
"for idx, animal in enumerate(eip_list):\n", | |
" print('#%d: %s' % (idx + 1, animal))\n", | |
"# Prints \"#1: cat\", \"#2: dog\", \"#3: monkey\", each on its own line" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"List comprehensions: When programming, frequently we want to transform one type of data into another. As a simple example, consider the following code that computes square numbers" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0, 1, 4, 9, 16]\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_list = [0, 1, 2, 3, 4]\n", | |
"mlblr = []\n", | |
"for x in eip_list:\n", | |
" mlblr.append(x ** 2)\n", | |
"print(mlblr) # Prints [0, 1, 4, 9, 16]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"You can make this code simpler using a list comprehension:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0, 1, 4, 9, 16]\n" | |
] | |
} | |
], | |
"source": [ | |
"mlblr = [0, 1, 2, 3, 4]\n", | |
"eip_list = [eip_out ** 2 for eip_out in mlblr]\n", | |
"print(eip_list) # Prints [0, 1, 4, 9, 16]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"You can make this code simpler using a list comprehension:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0, 1, 4, 9, 16]\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_dict = [0, 1, 2, 3, 4]\n", | |
"eip_dict2 = [eip_dict3 ** 2 for eip_dict3 in eip_dict]\n", | |
"print(eip_dict2 ) # Prints [0, 1, 4, 9, 16]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"List comprehensions can also contain conditions:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0, 4, 16]\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_dict = [0, 1, 2, 3, 4]\n", | |
"eip_dict2 = [eip_dict3 ** 2 for eip_dict3 in eip_dict if eip_dict3 % 2 == 0]\n", | |
"print(eip_dict2) # Prints \"[0, 4, 16]\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"Dictionaries\n", | |
"A dictionary stores (key, value) pairs, similar to a Map in Java or an object in Javascript. You can use it like this:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"cute\n", | |
"True\n", | |
"wet\n", | |
"N/A\n", | |
"wet\n", | |
"N/A\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_dict = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data\n", | |
"print(eip_dict['cat']) # Get an entry from a dictionary; prints \"cute\"\n", | |
"print('cat' in eip_dict) # Check if a dictionary has a given key; prints \"True\"\n", | |
"eip_dict['fish'] = 'wet' # Set an entry in a dictionary\n", | |
"print(eip_dict['fish']) # Prints \"wet\"\n", | |
"# print(eip_dict['monkey']) # KeyError: 'monkey' not a key of d\n", | |
"print(eip_dict.get('monkey', 'N/A')) # Get an element with a default; prints \"N/A\"\n", | |
"print(eip_dict.get('fish', 'N/A')) # Get an element with a default; prints \"wet\"\n", | |
"del eip_dict['fish'] # Remove an element from a dictionary\n", | |
"print(eip_dict.get('fish', 'N/A')) # \"fish\" is no longer a key; prints \"N/A\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"You can find all you need to know about dictionaries in the documentation.\n", | |
"\n", | |
"Loops: It is easy to iterate over the keys in a dictionary:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"A {'person': 2, 'cat': 4, 'spider': 8} has 2 legs\n", | |
"A {'person': 2, 'cat': 4, 'spider': 8} has 4 legs\n", | |
"A {'person': 2, 'cat': 4, 'spider': 8} has 8 legs\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_dict = {'person': 2, 'cat': 4, 'spider': 8}\n", | |
"for eip_dict2 in eip_dict:\n", | |
" eip_dict3 = eip_dict[eip_dict2]\n", | |
" print('A %s has %d legs' % (eip_dict, eip_dict3))\n", | |
"# Prints \"A person has 2 legs\", \"A cat has 4 legs\", \"A spider has 8 legs\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"If you want access to keys and their corresponding values, use the items method:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"A person has 2 legs\n", | |
"A cat has 4 legs\n", | |
"A spider has 8 legs\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_dict = {'person': 2, 'cat': 4, 'spider': 8}\n", | |
"for eip_dict2, eip_dict3 in eip_dict.items():\n", | |
" print('A %s has %d legs' % (eip_dict2, eip_dict3))\n", | |
"# Prints \"A person has 2 legs\", \"A cat has 4 legs\", \"A spider has 8 legs\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"Dictionary comprehensions: These are similar to list comprehensions, but allow you to easily construct dictionaries. For example:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{0: 0, 2: 4, 4: 16}\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_dict = [0, 1, 2, 3, 4]\n", | |
"even_eip_dict_to_square = {x: x ** 2 for x in eip_dict if x % 2 == 0}\n", | |
"print(even_eip_dict_to_square) # Prints \"{0: 0, 2: 4, 4: 16}\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"Sets\n", | |
"A set is an unordered collection of distinct elements. As a simple example, consider the following:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"True\n", | |
"False\n", | |
"True\n", | |
"3\n", | |
"3\n", | |
"2\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_dict = {'cat', 'dog'}\n", | |
"print('cat' in eip_dict) # Check if an element is in a set; prints \"True\"\n", | |
"print('fish' in eip_dict) # prints \"False\"\n", | |
"eip_dict.add('fish') # Add an element to a set\n", | |
"print('fish' in eip_dict) # Prints \"True\"\n", | |
"print(len(eip_dict)) # Number of elements in a set; prints \"3\"\n", | |
"eip_dict.add('cat') # Adding an element that is already in the set does nothing\n", | |
"print(len(eip_dict)) # Prints \"3\"\n", | |
"eip_dict.remove('cat') # Remove an element from a set\n", | |
"print(len(eip_dict)) # Prints \"2\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"As usual, everything you want to know about sets can be found in the documentation.\n", | |
"\n", | |
"Loops: Iterating over a set has the same syntax as iterating over a list; however since sets are unordered, you cannot make assumptions about the order in which you visit the elements of the set:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"#1: cat\n", | |
"#2: dog\n", | |
"#3: fish\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_dict = {'cat', 'dog', 'fish'}\n", | |
"for idx, animal in enumerate(eip_dict):\n", | |
" print('#%d: %s' % (idx + 1, animal))\n", | |
"# Prints \"#1: fish\", \"#2: dog\", \"#3: cat\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Set comprehensions: Like lists and dictionaries, we can easily construct sets using set comprehensions:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{0, 1, 2, 3, 4, 5}\n" | |
] | |
} | |
], | |
"source": [ | |
"from math import sqrt\n", | |
"eip_dict = {int(sqrt(x)) for x in range(30)}\n", | |
"print(eip_dict) # Prints \"{0, 1, 2, 3, 4, 5}\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Tuples\n", | |
"A tuple is an (immutable) ordered list of values. A tuple is in many ways similar to a list; one of the most important differences is that tuples can be used as keys in dictionaries and as elements of sets, while lists cannot. Here is a trivial example:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<class 'tuple'>\n", | |
"5\n", | |
"1\n" | |
] | |
} | |
], | |
"source": [ | |
"eip_dict = {(x, x + 1): x for x in range(10)} # Create a dictionary with tuple keys\n", | |
"eip_dict_1 = (5, 6) # Create a tuple\n", | |
"print(type(eip_dict_1)) # Prints \"<class 'tuple'>\"\n", | |
"print(eip_dict[eip_dict_1]) # Prints \"5\"\n", | |
"print(eip_dict[(1, 2)]) # Prints \"1\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The documentation has more information about tuples.\n", | |
"\n", | |
"\n", | |
"Functions\n", | |
"Python functions are defined using the def keyword. For example:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"negative\n", | |
"zero\n", | |
"positive\n" | |
] | |
} | |
], | |
"source": [ | |
"def sign(mlblr):\n", | |
" if mlblr > 0:\n", | |
" return 'positive'\n", | |
" elif mlblr < 0:\n", | |
" return 'negative'\n", | |
" else:\n", | |
" return 'zero'\n", | |
"\n", | |
"for mlblr in [-1, 0, 1]:\n", | |
" print(sign(mlblr))\n", | |
"# Prints \"negative\", \"zero\", \"positive\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We will often define functions to take optional keyword arguments, like this:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello, Bob\n", | |
"HELLO, FRED!\n" | |
] | |
} | |
], | |
"source": [ | |
"def hello(mlblr, loud=False):\n", | |
" if loud:\n", | |
" print('HELLO, %s!' % mlblr.upper())\n", | |
" else:\n", | |
" print('Hello, %s' % mlblr)\n", | |
"\n", | |
"hello('Bob') # Prints \"Hello, Bob\"\n", | |
"hello('Fred', loud=True) # Prints \"HELLO, FRED!\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"There is a lot more information about Python functions in the documentation.\n", | |
"\n", | |
"\n", | |
"Classes\n", | |
"The syntax for defining classes in Python is straightforward:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello, Fred\n", | |
"HELLO, FRED!\n" | |
] | |
} | |
], | |
"source": [ | |
"class mlblr(object):\n", | |
"\n", | |
" # Constructor\n", | |
" def __init__(self, name):\n", | |
" self.name = name # Create an instance variable\n", | |
"\n", | |
" # Instance method\n", | |
" def eip(self, loud=False):\n", | |
" if loud:\n", | |
" print('HELLO, %s!' % self.name.upper())\n", | |
" else:\n", | |
" print('Hello, %s' % self.name)\n", | |
"\n", | |
"eip_1 = mlblr('Fred') # Construct an instance of the Greeter class\n", | |
"eip_1.eip() # Call an instance method; prints \"Hello, Fred\"\n", | |
"eip_1.eip(loud=True) # Call an instance method; prints \"HELLO, FRED!\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"You can read a lot more about Python classes in the documentation." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Numpy\n", | |
"Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. If you are already familiar with MATLAB, you might find this tutorial useful to get started with Numpy.\n", | |
"\n", | |
"\n", | |
"Arrays\n", | |
"A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.\n", | |
"\n", | |
"We can initialize numpy arrays from nested Python lists, and access elements using square brackets:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"a = np.array([1, 2, 3]) # Create a rank 1 array\n", | |
"print(type(a)) # Prints \"<class 'numpy.ndarray'>\"\n", | |
"print(a.shape) # Prints \"(3,)\"\n", | |
"print(a[0], a[1], a[2]) # Prints \"1 2 3\"\n", | |
"a[0] = 5 # Change an element of the array\n", | |
"print(a) # Prints \"[5, 2, 3]\"\n", | |
"\n", | |
"b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array\n", | |
"print(b.shape) # Prints \"(2, 3)\"\n", | |
"print(b[0, 0], b[0, 1], b[1, 0]) # Prints \"1 2 4\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment