diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/04-Lists-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/04-Lists-checkpoint.ipynb new file mode 100644 index 000000000..774174e2d --- /dev/null +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/04-Lists-checkpoint.ipynb @@ -0,0 +1,782 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lists\n", + "\n", + "Earlier when discussing strings we introduced the concept of a *sequence* in Python. Lists can be thought of the most general version of a *sequence* in Python. Unlike strings, they are mutable, meaning the elements inside a list can be changed!\n", + "\n", + "In this section we will learn about:\n", + " \n", + " 1.) Creating lists\n", + " 2.) Indexing and Slicing Lists\n", + " 3.) Basic List Methods\n", + " 4.) Nesting Lists\n", + " 5.) Introduction to List Comprehensions\n", + "\n", + "Lists are constructed with brackets [] and commas separating every element in the list.\n", + "\n", + "Let's go ahead and see how we can construct lists!" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Assign a list to an variable named my_list\n", + "my_list = [1,2,3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We just created a list of integers, but lists can actually hold different object types. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_list = ['A string',23,100.232,'o']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Just like strings, the len() function will tell you how many items are in the sequence of the list." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(my_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Indexing and Slicing\n", + "Indexing and slicing work just like in strings. Let's make a new list to remind ourselves of how this works:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_list = ['one','two','three',4,5]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'one'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab element at index 0\n", + "my_list[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['two', 'three', 4, 5]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab index 1 and everything past it\n", + "my_list[1:]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one', 'two', 'three']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab everything UP TO index 3\n", + "my_list[:3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also use + to concatenate lists, just like we did for strings." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one', 'two', 'three', 4, 5, 'new item']" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list + ['new item']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note: This doesn't actually change the original list!" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one', 'two', 'three', 4, 5]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You would have to reassign the list to make the change permanent." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Reassign\n", + "my_list = my_list + ['add new item permanently']" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one', 'two', 'three', 4, 5, 'add new item permanently']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also use the * for a duplication method similar to strings:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one',\n", + " 'two',\n", + " 'three',\n", + " 4,\n", + " 5,\n", + " 'add new item permanently',\n", + " 'one',\n", + " 'two',\n", + " 'three',\n", + " 4,\n", + " 5,\n", + " 'add new item permanently']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Make the list double\n", + "my_list * 2" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one', 'two', 'three', 4, 5, 'add new item permanently']" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Again doubling not permanent\n", + "my_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Basic List Methods\n", + "\n", + "If you are familiar with another programming language, you might start to draw parallels between arrays in another language and lists in Python. Lists in Python however, tend to be more flexible than arrays in other languages for a two good reasons: they have no fixed size (meaning we don't have to specify how big a list will be), and they have no fixed type constraint (like we've seen above).\n", + "\n", + "Let's go ahead and explore some more special methods for lists:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create a new list\n", + "list1 = [1,2,3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Use the **append** method to permanently add an item to the end of a list:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Append\n", + "list1.append('append me!')" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 'append me!']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Show\n", + "list1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Use **pop** to \"pop off\" an item from the list. By default pop takes off the last index, but you can also specify which index to pop off. Let's see an example:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Pop off the 0 indexed item\n", + "list1.pop(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3, 'append me!']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Show\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Assign the popped element, remember default popped index is -1\n", + "popped_item = list1.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'append me!'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "popped_item" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Show remaining list\n", + "list1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It should also be noted that lists indexing will return an error if there is no element at that index. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mlist1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mIndexError\u001b[0m: list index out of range" + ] + } + ], + "source": [ + "list1[100]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can use the **sort** method and the **reverse** methods to also effect your lists:" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "new_list = ['a','e','x','b','c']" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'e', 'x', 'b', 'c']" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Show\n", + "new_list" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Use reverse to reverse order (this is permanent!)\n", + "new_list.reverse()" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['c', 'b', 'x', 'e', 'a']" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_list" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Use sort to sort the list (in this case alphabetical order, but for numbers it will go ascending)\n", + "new_list.sort()" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'b', 'c', 'e', 'x']" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Nesting Lists\n", + "A great feature of of Python data structures is that they support *nesting*. This means we can have data structures within data structures. For example: A list inside a list.\n", + "\n", + "Let's see how this works!" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Let's make three lists\n", + "lst_1=[1,2,3]\n", + "lst_2=[4,5,6]\n", + "lst_3=[7,8,9]\n", + "\n", + "# Make a list of lists to form a matrix\n", + "matrix = [lst_1,lst_2,lst_3]" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Show\n", + "matrix" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can again use indexing to grab elements, but now there are two levels for the index. The items in the matrix object, and then the items inside that list!" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3]" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab first item in matrix object\n", + "matrix[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab first item of the first item in the matrix object\n", + "matrix[0][0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# List Comprehensions\n", + "Python has an advanced feature called list comprehensions. They allow for quick construction of lists. To fully understand list comprehensions we need to understand for loops. So don't worry if you don't completely understand this section, and feel free to just skip it since we will return to this topic later.\n", + "\n", + "But in case you want to know now, here are a few examples!" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Build a list comprehension by deconstructing a for loop within a []\n", + "first_col = [row[0] for row in matrix]" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 7]" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_col" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We used a list comprehension here to grab the first element of every row in the matrix object. We will cover this in much more detail later on!\n", + "\n", + "For more advanced methods and features of lists in Python, check out the Advanced Lists section later on in this course!" + ] + } + ], + "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": 1 +} diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/05-Dictionaries-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/05-Dictionaries-checkpoint.ipynb new file mode 100644 index 000000000..0dfc8cd10 --- /dev/null +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/05-Dictionaries-checkpoint.ipynb @@ -0,0 +1,454 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Dictionaries\n", + "\n", + "We've been learning about *sequences* in Python but now we're going to switch gears and learn about *mappings* in Python. If you're familiar with other languages you can think of these Dictionaries as hash tables. \n", + "\n", + "This section will serve as a brief introduction to dictionaries and consist of:\n", + "\n", + " 1.) Constructing a Dictionary\n", + " 2.) Accessing objects from a dictionary\n", + " 3.) Nesting Dictionaries\n", + " 4.) Basic Dictionary Methods\n", + "\n", + "So what are mappings? Mappings are a collection of objects that are stored by a *key*, unlike a sequence that stored objects by their relative position. This is an important distinction, since mappings won't retain order since they have objects defined by a key.\n", + "\n", + "A Python dictionary consists of a key and then an associated value. That value can be almost any Python object.\n", + "\n", + "\n", + "## Constructing a Dictionary\n", + "Let's see how we can construct dictionaries to get a better understanding of how they work!" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Make a dictionary with {} and : to signify a key and a value\n", + "my_dict = {'key1':'value1','key2':'value2'}" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'value2'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Call values by their key\n", + "my_dict['key2']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Its important to note that dictionaries are very flexible in the data types they can hold. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['item0', 'item1', 'item2']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Let's call items from the dictionary\n", + "my_dict['key3']" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'item0'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Can call an index on that value\n", + "my_dict['key3'][0]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ITEM0'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Can then even call methods on that value\n", + "my_dict['key3'][0].upper()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can affect the values of a key as well. For instance:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "123" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_dict['key1']" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Subtract 123 from the value\n", + "my_dict['key1'] = my_dict['key1'] - 123" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Check\n", + "my_dict['key1']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A quick note, Python has a built-in method of doing a self subtraction or addition (or multiplication or division). We could have also used += or -= for the above statement. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-123" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Set the object equal to itself minus 123 \n", + "my_dict['key1'] -= 123\n", + "my_dict['key1']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also create keys by assignment. For instance if we started off with an empty dictionary, we could continually add to it:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create a new dictionary\n", + "d = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create a new key through assignment\n", + "d['animal'] = 'Dog'" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Can do this with any object\n", + "d['answer'] = 42" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'animal': 'Dog', 'answer': 42}" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Show\n", + "d" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Nesting with Dictionaries\n", + "\n", + "Hopefully you're starting to see how powerful Python is with its flexibility of nesting objects and calling methods on them. Let's see a dictionary nested inside a dictionary:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Dictionary nested inside a dictionary nested inside a dictionary\n", + "d = {'key1':{'nestkey':{'subnestkey':'value'}}}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Wow! That's a quite the inception of dictionaries! Let's see how we can grab that value:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'value'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Keep calling the keys\n", + "d['key1']['nestkey']['subnestkey']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## A few Dictionary Methods\n", + "\n", + "There are a few methods we can call on a dictionary. Let's get a quick introduction to a few of them:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create a typical dictionary\n", + "d = {'key1':1,'key2':2,'key3':3}" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['key1', 'key2', 'key3'])" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Method to return a list of all keys \n", + "d.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([1, 2, 3])" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Method to grab all values\n", + "d.values()" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('key1', 1), ('key2', 2), ('key3', 3)])" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Method to return tuples of all items (we'll learn about tuples soon)\n", + "d.items()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Hopefully you now have a good basic understanding how to construct dictionaries. There's a lot more to go into here, but we will revisit dictionaries at later time. After this section all you need to know is how to create a dictionary and how to retrieve values from it." + ] + } + ], + "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": 1 +} diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/06-Tuples-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/06-Tuples-checkpoint.ipynb new file mode 100644 index 000000000..5c3be3e4c --- /dev/null +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/06-Tuples-checkpoint.ipynb @@ -0,0 +1,268 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tuples\n", + "\n", + "In Python tuples are very similar to lists, however, unlike lists they are *immutable* meaning they can not be changed. You would use tuples to present things that shouldn't be changed, such as days of the week, or dates on a calendar. \n", + "\n", + "In this section, we will get a brief overview of the following:\n", + "\n", + " 1.) Constructing Tuples\n", + " 2.) Basic Tuple Methods\n", + " 3.) Immutability\n", + " 4.) When to Use Tuples\n", + "\n", + "You'll have an intuition of how to use tuples based on what you've learned about lists. We can treat them very similarly with the major distinction being that tuples are immutable.\n", + "\n", + "## Constructing Tuples\n", + "\n", + "The construction of a tuples use () with elements separated by commas. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create a tuple\n", + "t = (1,2,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Check len just like a list\n", + "len(t)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('one', 2)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Can also mix object types\n", + "t = ('one',2)\n", + "\n", + "# Show\n", + "t" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'one'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Use indexing just like we did in lists\n", + "t[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Slicing just like a list\n", + "t[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Basic Tuple Methods\n", + "\n", + "Tuples have built-in methods, but not as many as lists do. Let's look at two of them:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Use .index to enter a value and return the index\n", + "t.index('one')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Use .count to count the number of times a value appears\n", + "t.count('one')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Immutability\n", + "\n", + "It can't be stressed enough that tuples are immutable. To drive that point home:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m \u001b[1;34m'change'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t[0]= 'change'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Because of this immutability, tuples can't grow. Once a tuple is made we can not add to it." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'tuple' object has no attribute 'append'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'nope'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" + ] + } + ], + "source": [ + "t.append('nope')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## When to use Tuples\n", + "\n", + "You may be wondering, \"Why bother using tuples when they have fewer available methods?\" To be honest, tuples are not used as often as lists in programming, but are used when immutability is necessary. If in your program you are passing around an object and need to make sure it does not get changed, then a tuple becomes your solution. It provides a convenient source of data integrity.\n", + "\n", + "You should now be able to create and use tuples in your programming as well as have an understanding of their immutability.\n", + "\n", + "Up next Files!" + ] + } + ], + "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": 1 +} diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/09-Objects and Data Structures Assessment Test-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/09-Objects and Data Structures Assessment Test-checkpoint.ipynb index 149e61af2..f6521fa8e 100644 --- a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/09-Objects and Data Structures Assessment Test-checkpoint.ipynb +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/09-Objects and Data Structures Assessment Test-checkpoint.ipynb @@ -55,10 +55,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2**3" + ] }, { "cell_type": "markdown", @@ -66,25 +79,38 @@ "source": [ "Answer these 3 questions without typing code. Then type code to check your answer.\n", "\n", - " What is the value of the expression 4 * (6 + 5)\n", + " What is the value of the expression 4 * (6 + 5) = 44\n", " \n", - " What is the value of the expression 4 * 6 + 5 \n", + " What is the value of the expression 4 * 6 + 5 = 29\n", " \n", - " What is the value of the expression 4 + 6 * 5 " + " What is the value of the expression 4 + 6 * 5 = 34" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "34" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "4 + 6 * 5" + ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "What is the *type* of the result of the expression 3 + 1.5 + 4?

" + "What is the *type* of the result of the expression 3 + 1.5 + 4?

float" ] }, { @@ -100,7 +126,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Square root:\n" + "# Square root: x**0.5\n" ] }, { @@ -109,7 +135,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Square:\n" + "# Square: x**2\n" ] }, { @@ -128,13 +154,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "e\n" + ] + } + ], "source": [ "s = 'hello'\n", "# Print out 'e' using indexing\n", - "\n" + "\n", + "print(s[1])" ] }, { @@ -146,13 +181,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "olleh\n" + ] + } + ], "source": [ "s ='hello'\n", "# Reverse the string using slicing\n", - "\n" + "print(s[::-1])\n" ] }, { @@ -164,25 +207,42 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "o\n" + ] + } + ], "source": [ "s ='hello'\n", "# Print out the 'o'\n", "\n", "# Method 1:\n", + "print(s[-1:])\n", "\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "o\n" + ] + } + ], "source": [ "# Method 2:\n", - "\n" + "print(s[4:])" ] }, { @@ -201,20 +261,49 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 0, 0]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Method 1:\n" + "# Method 1:\n", + "li = [0,0,0]\n", + "li" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 0, 0]" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Method 2:\n" + "# Method 2:\n", + "li = []\n", + "li.append(0)\n", + "li.append(0)\n", + "li.append(0)\n", + "li" ] }, { @@ -226,12 +315,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, [3, 4, 'goodbye']]" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list3 = [1,2,[3,4,'hello']]\n", - "\n" + "\n", + "list3[2][2] = 'goodbye'\n", + "list3" ] }, { @@ -243,12 +345,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 3, 4, 5, 6]" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list4 = [5,3,4,6,1]\n", - "\n" + "list4.sort()\n", + "list4\n" ] }, { @@ -267,34 +381,66 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello\n" + ] + } + ], "source": [ "d = {'simple_key':'hello'}\n", - "# Grab 'hello'\n" + "# Grab 'hello'\n", + "print(d['simple_key'])" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'k2': 'hello'}\n", + "hello\n" + ] + } + ], "source": [ "d = {'k1':{'k2':'hello'}}\n", - "# Grab 'hello'\n" + "# Grab 'hello'\n", + "print(d['k1'])\n", + "print(d['k1']['k2'])" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello'" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Getting a little tricker\n", "d = {'k1':[{'nest_key':['this is deep',['hello']]}]}\n", "\n", - "#Grab hello\n" + "#Grab hello\n", + "d['k1'][0]['nest_key'][1][0]" ] }, { @@ -311,7 +457,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Can you sort a dictionary? Why or why not?

" + "Can you sort a dictionary? Why or why not?

can't sort dic since it's unordered list" ] }, { @@ -325,14 +471,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "What is the major difference between tuples and lists?

" + "What is the major difference between tuples and lists?

tuples are immutable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "How do you create a tuple?

" + "How do you create a tuple?

tu = (1,2,3)" ] }, { @@ -358,13 +504,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4, 11, 22, 33}" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list5 = [1,2,2,33,4,4,11,22,3,3,2]\n", "\n", - "\n" + "set(list5)\n" ] }, { @@ -456,12 +613,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Answer before running cell\n", - "3.0 == 3" + "3.0 == 3\n", + "type(3.0)" ] }, { @@ -483,16 +652,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# two nested lists\n", "l_one = [1,2,[3,4]]\n", "l_two = [1,2,{'k1':4}]\n", "\n", "# True or False?\n", - "l_one[2][0] >= l_two[2]['k1']" + "l_one[2][0] >= l_two[2]['k1'] #false" ] }, { @@ -520,7 +700,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/playground-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/playground-checkpoint.ipynb new file mode 100644 index 000000000..2fd64429b --- /dev/null +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/playground-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/00-Python Object and Data Structure Basics/01-Variable Assignment.ipynb b/00-Python Object and Data Structure Basics/01-Variable Assignment.ipynb index c5af87b91..d20caa82e 100644 --- a/00-Python Object and Data Structure Basics/01-Variable Assignment.ipynb +++ b/00-Python Object and Data Structure Basics/01-Variable Assignment.ipynb @@ -30,7 +30,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_dogs = 2" @@ -58,25 +60,27 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": {}, + "execution_count": 2, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ - "my_dogs = ['Sammy', 'Frankie']" + "my_dogs = ['Sammy', 'Frankie', 0.5]" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['Sammy', 'Frankie']" + "['Sammy', 'Frankie', 0.5]" ] }, - "execution_count": 4, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -110,7 +114,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = 5" @@ -146,7 +152,9 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = 10" @@ -210,7 +218,9 @@ { "cell_type": "code", "execution_count": 10, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = a + 10" @@ -246,7 +256,9 @@ { "cell_type": "code", "execution_count": 12, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a += 10" @@ -275,7 +287,9 @@ { "cell_type": "code", "execution_count": 14, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a *= 2" @@ -340,7 +354,9 @@ { "cell_type": "code", "execution_count": 17, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = (1,2)" @@ -377,7 +393,9 @@ { "cell_type": "code", "execution_count": 19, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_income = 100\n", @@ -429,7 +447,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/02-Strings.ipynb b/00-Python Object and Data Structure Basics/02-Strings.ipynb index 1ef2117d2..11a34ec3e 100644 --- a/00-Python Object and Data Structure Basics/02-Strings.ipynb +++ b/00-Python Object and Data Structure Basics/02-Strings.ipynb @@ -35,20 +35,9 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello'" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Single word\n", "'hello'" @@ -56,20 +45,9 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'This is also a string'" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Entire phrase \n", "'This is also a string'" @@ -77,20 +55,9 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'String built with double quotes'" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# We can also use double quote\n", "\"String built with double quotes\"" @@ -98,18 +65,9 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (, line 2)", - "output_type": "error", - "traceback": [ - "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m ' I'm using single quotes, but this will create an error'\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" - ] - } - ], + "outputs": [], "source": [ "# Be careful with quotes!\n", "' I'm using single quotes, but this will create an error'" @@ -124,20 +82,9 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"Now I'm ready to use the single quotes inside a string!\"" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"Now I'm ready to use the single quotes inside a string!\"" ] @@ -160,20 +107,9 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello World'" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# We can simply declare a string\n", "'Hello World'" @@ -181,20 +117,9 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello World 2'" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Note that we can't output multiple strings this way\n", "'Hello World 1'\n", @@ -210,23 +135,9 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World 1\n", - "Hello World 2\n", - "Use \n", - " to print a new line\n", - "\n", - "\n", - "See what I mean?\n" - ] - } - ], + "outputs": [], "source": [ "print('Hello World 1')\n", "print('Hello World 2')\n", @@ -251,20 +162,9 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "11" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "len('Hello World')" ] @@ -288,8 +188,10 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Assign s as a string\n", @@ -298,20 +200,9 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello World'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "#Check\n", "s" @@ -319,17 +210,9 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World\n" - ] - } - ], + "outputs": [], "source": [ "# Print the object\n", "print(s) " @@ -344,20 +227,9 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'H'" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Show first element (in this case a letter)\n", "s[0]" @@ -365,40 +237,18 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'e'" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s[1]" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'l'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s[2]" ] @@ -412,20 +262,9 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'ello World'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Grab everything past the first term all the way to the length of s which is len(s)\n", "s[1:]" @@ -433,20 +272,9 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello World'" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Note that there is no change to the original s\n", "s" @@ -454,20 +282,9 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hel'" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Grab everything UP TO the 3rd index\n", "s[:3]" @@ -482,23 +299,13 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello World'" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "#Everything\n", - "s[:]" + "#s[:]\n", + "s == s[:]" ] }, { @@ -510,20 +317,9 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'d'" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Last letter (one index behind 0 so it loops back around)\n", "s[-1]" @@ -531,20 +327,9 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello Worl'" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Grab everything but the last letter\n", "s[:-1]" @@ -559,20 +344,9 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello World'" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Grab everything, but go in steps size of 1\n", "s[::1]" @@ -580,20 +354,9 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'HloWrd'" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Grab everything, but go in step sizes of 2\n", "s[::2]" @@ -601,20 +364,9 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'dlroW olleH'" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# We can use this to print a string backwards\n", "s[::-1]" @@ -632,44 +384,21 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 3, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello World'" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "s" + "s = \"anh le\"" ] }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 5, "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'str' object does not support item assignment", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Let's try to change the first letter to 'x'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'x'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" - ] - } - ], + "outputs": [], "source": [ "# Let's try to change the first letter to 'x'\n", - "s[0] = 'x'" + "#s[0] = 'x'" ] }, { @@ -683,38 +412,28 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello World'" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s" ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 2, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'Hello World concatenate me!'" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" + "ename": "NameError", + "evalue": "name 's' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Concatenate strings!\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ms\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m' concatenate me!'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mNameError\u001b[0m: name 's' is not defined" + ] } ], "source": [ @@ -724,8 +443,10 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# We can reassign s completely though!\n", @@ -734,37 +455,18 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World concatenate me!\n" - ] - } - ], + "outputs": [], "source": [ "print(s)" ] }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello World concatenate me!'" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s" ] @@ -778,8 +480,10 @@ }, { "cell_type": "code", - "execution_count": 32, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "letter = 'z'" @@ -787,20 +491,9 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'zzzzzzzzzz'" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "letter*10" ] @@ -824,36 +517,25 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 8, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello World concatenate me!'" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "s" + "s = \"he he anh le day\"" ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'HELLO WORLD CONCATENATE ME!'" + "'HE HE ANH LE DAY'" ] }, - "execution_count": 35, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -865,16 +547,16 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'hello world concatenate me!'" + "'he he anh le day'" ] }, - "execution_count": 36, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -886,16 +568,16 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['Hello', 'World', 'concatenate', 'me!']" + "['he', 'he', 'anh', 'le', 'day']" ] }, - "execution_count": 37, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -907,16 +589,16 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['Hello ', 'orld concatenate me!']" + "['he he anh le day']" ] }, - "execution_count": 38, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -946,7 +628,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -955,7 +637,7 @@ "'Insert another string with curly brackets: The inserted string'" ] }, - "execution_count": 39, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -995,7 +677,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/03-Print Formatting with Strings.ipynb b/00-Python Object and Data Structure Basics/03-Print Formatting with Strings.ipynb index 7661e9f74..e0ca74fb2 100644 --- a/00-Python Object and Data Structure Basics/03-Print Formatting with Strings.ipynb +++ b/00-Python Object and Data Structure Basics/03-Print Formatting with Strings.ipynb @@ -705,7 +705,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/04-Lists.ipynb b/00-Python Object and Data Structure Basics/04-Lists.ipynb index 332c404b2..18d054b49 100644 --- a/00-Python Object and Data Structure Basics/04-Lists.ipynb +++ b/00-Python Object and Data Structure Basics/04-Lists.ipynb @@ -23,8 +23,10 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Assign a list to an variable named my_list\n", @@ -40,8 +42,10 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_list = ['A string',23,100.232,'o']" @@ -56,20 +60,9 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "len(my_list)" ] @@ -84,8 +77,10 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_list = ['one','two','three',4,5]" @@ -93,20 +88,9 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'one'" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Grab element at index 0\n", "my_list[0]" @@ -114,20 +98,9 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['two', 'three', 4, 5]" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Grab index 1 and everything past it\n", "my_list[1:]" @@ -135,20 +108,9 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['one', 'two', 'three']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Grab everything UP TO index 3\n", "my_list[:3]" @@ -163,20 +125,9 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['one', 'two', 'three', 4, 5, 'new item']" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_list + ['new item']" ] @@ -190,20 +141,9 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['one', 'two', 'three', 4, 5]" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_list" ] @@ -217,8 +157,10 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Reassign\n", @@ -227,20 +169,9 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['one', 'two', 'three', 4, 5, 'add new item permanently']" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_list" ] @@ -254,31 +185,9 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['one',\n", - " 'two',\n", - " 'three',\n", - " 4,\n", - " 5,\n", - " 'add new item permanently',\n", - " 'one',\n", - " 'two',\n", - " 'three',\n", - " 4,\n", - " 5,\n", - " 'add new item permanently']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Make the list double\n", "my_list * 2" @@ -286,20 +195,9 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['one', 'two', 'three', 4, 5, 'add new item permanently']" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Again doubling not permanent\n", "my_list" @@ -318,8 +216,10 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": {}, + "execution_count": 5, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a new list\n", @@ -335,8 +235,10 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": {}, + "execution_count": 6, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Append\n", @@ -345,7 +247,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -354,7 +256,7 @@ "[1, 2, 3, 'append me!']" ] }, - "execution_count": 16, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -373,7 +275,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -382,7 +284,7 @@ "1" ] }, - "execution_count": 17, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -394,7 +296,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -403,7 +305,7 @@ "[2, 3, 'append me!']" ] }, - "execution_count": 18, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -415,8 +317,10 @@ }, { "cell_type": "code", - "execution_count": 19, - "metadata": {}, + "execution_count": 10, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Assign the popped element, remember default popped index is -1\n", @@ -425,7 +329,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -434,7 +338,7 @@ "'append me!'" ] }, - "execution_count": 20, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -445,7 +349,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -454,7 +358,7 @@ "[2, 3]" ] }, - "execution_count": 21, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -473,21 +377,9 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "ename": "IndexError", - "evalue": "list index out of range", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mlist1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mIndexError\u001b[0m: list index out of range" - ] - } - ], + "outputs": [], "source": [ "list1[100]" ] @@ -501,8 +393,10 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "new_list = ['a','e','x','b','c']" @@ -510,20 +404,9 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['a', 'e', 'x', 'b', 'c']" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "#Show\n", "new_list" @@ -531,8 +414,10 @@ }, { "cell_type": "code", - "execution_count": 25, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Use reverse to reverse order (this is permanent!)\n", @@ -541,28 +426,19 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['c', 'b', 'x', 'e', 'a']" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "new_list" ] }, { "cell_type": "code", - "execution_count": 27, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Use sort to sort the list (in this case alphabetical order, but for numbers it will go ascending)\n", @@ -571,20 +447,9 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['a', 'b', 'c', 'e', 'x']" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "new_list" ] @@ -601,8 +466,10 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Let's make three lists\n", @@ -616,7 +483,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -625,7 +492,7 @@ "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]" ] }, - "execution_count": 30, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -644,20 +511,9 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3]" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Grab first item in matrix object\n", "matrix[0]" @@ -665,20 +521,9 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Grab first item of the first item in the matrix object\n", "matrix[0][0]" @@ -696,26 +541,26 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Build a list comprehension by deconstructing a for loop within a []\n", - "first_col = [row[0] for row in matrix]" + "first_col = [row for row in matrix]" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[1, 4, 7]" + "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]" ] }, - "execution_count": 34, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -750,7 +595,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/05-Dictionaries.ipynb b/00-Python Object and Data Structure Basics/05-Dictionaries.ipynb index 3207f0172..0b1de1181 100644 --- a/00-Python Object and Data Structure Basics/05-Dictionaries.ipynb +++ b/00-Python Object and Data Structure Basics/05-Dictionaries.ipynb @@ -27,7 +27,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Make a dictionary with {} and : to signify a key and a value\n", @@ -65,7 +67,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}" @@ -164,7 +168,9 @@ { "cell_type": "code", "execution_count": 8, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Subtract 123 from the value\n", @@ -231,7 +237,9 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a new dictionary\n", @@ -241,7 +249,9 @@ { "cell_type": "code", "execution_count": 12, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a new key through assignment\n", @@ -251,7 +261,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Can do this with any object\n", @@ -291,7 +303,9 @@ { "cell_type": "code", "execution_count": 15, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Dictionary nested inside a dictionary nested inside a dictionary\n", @@ -338,7 +352,9 @@ { "cell_type": "code", "execution_count": 17, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a typical dictionary\n", @@ -432,7 +448,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/06-Tuples.ipynb b/00-Python Object and Data Structure Basics/06-Tuples.ipynb index 3f93a3369..5c3be3e4c 100644 --- a/00-Python Object and Data Structure Basics/06-Tuples.ipynb +++ b/00-Python Object and Data Structure Basics/06-Tuples.ipynb @@ -25,7 +25,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a tuple\n", @@ -258,7 +260,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/07-Sets and Booleans.ipynb b/00-Python Object and Data Structure Basics/07-Sets and Booleans.ipynb index f0749fde0..3c78895fb 100644 --- a/00-Python Object and Data Structure Basics/07-Sets and Booleans.ipynb +++ b/00-Python Object and Data Structure Basics/07-Sets and Booleans.ipynb @@ -303,7 +303,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/08-Files.ipynb b/00-Python Object and Data Structure Basics/08-Files.ipynb index e5aa6cbb8..eeff857df 100644 --- a/00-Python Object and Data Structure Basics/08-Files.ipynb +++ b/00-Python Object and Data Structure Basics/08-Files.ipynb @@ -548,7 +548,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/09-Objects and Data Structures Assessment Test.ipynb b/00-Python Object and Data Structure Basics/09-Objects and Data Structures Assessment Test.ipynb index 149e61af2..3a42f1c9e 100644 --- a/00-Python Object and Data Structure Basics/09-Objects and Data Structures Assessment Test.ipynb +++ b/00-Python Object and Data Structure Basics/09-Objects and Data Structures Assessment Test.ipynb @@ -55,10 +55,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2**3" + ] }, { "cell_type": "markdown", @@ -66,25 +79,38 @@ "source": [ "Answer these 3 questions without typing code. Then type code to check your answer.\n", "\n", - " What is the value of the expression 4 * (6 + 5)\n", + " What is the value of the expression 4 * (6 + 5) = 44\n", " \n", - " What is the value of the expression 4 * 6 + 5 \n", + " What is the value of the expression 4 * 6 + 5 = 29\n", " \n", - " What is the value of the expression 4 + 6 * 5 " + " What is the value of the expression 4 + 6 * 5 = 34" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "34" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "4 + 6 * 5" + ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "What is the *type* of the result of the expression 3 + 1.5 + 4?

" + "What is the *type* of the result of the expression 3 + 1.5 + 4?

float" ] }, { @@ -97,19 +123,23 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ - "# Square root:\n" + "# Square root: x**0.5\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ - "# Square:\n" + "# Square: x**2\n" ] }, { @@ -128,13 +158,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "e\n" + ] + } + ], "source": [ "s = 'hello'\n", "# Print out 'e' using indexing\n", - "\n" + "\n", + "print(s[1])" ] }, { @@ -146,13 +185,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "olleh\n" + ] + } + ], "source": [ "s ='hello'\n", "# Reverse the string using slicing\n", - "\n" + "print(s[::-1])\n" ] }, { @@ -164,25 +211,42 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "o\n" + ] + } + ], "source": [ "s ='hello'\n", "# Print out the 'o'\n", "\n", "# Method 1:\n", + "print(s[-1:])\n", "\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "o\n" + ] + } + ], "source": [ "# Method 2:\n", - "\n" + "print(s[4:])" ] }, { @@ -201,20 +265,49 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 0, 0]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Method 1:\n" + "# Method 1:\n", + "li = [0,0,0]\n", + "li" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 0, 0]" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Method 2:\n" + "# Method 2:\n", + "li = []\n", + "li.append(0)\n", + "li.append(0)\n", + "li.append(0)\n", + "li" ] }, { @@ -226,12 +319,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, [3, 4, 'goodbye']]" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list3 = [1,2,[3,4,'hello']]\n", - "\n" + "\n", + "list3[2][2] = 'goodbye'\n", + "list3" ] }, { @@ -243,12 +349,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 3, 4, 5, 6]" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list4 = [5,3,4,6,1]\n", - "\n" + "list4.sort()\n", + "list4\n" ] }, { @@ -267,40 +385,74 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello\n" + ] + } + ], "source": [ "d = {'simple_key':'hello'}\n", - "# Grab 'hello'\n" + "# Grab 'hello'\n", + "print(d['simple_key'])" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'k2': 'hello'}\n", + "hello\n" + ] + } + ], "source": [ "d = {'k1':{'k2':'hello'}}\n", - "# Grab 'hello'\n" + "# Grab 'hello'\n", + "print(d['k1'])\n", + "print(d['k1']['k2'])" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello'" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Getting a little tricker\n", "d = {'k1':[{'nest_key':['this is deep',['hello']]}]}\n", "\n", - "#Grab hello\n" + "#Grab hello\n", + "d['k1'][0]['nest_key'][1][0]" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# This will be hard and annoying!\n", @@ -311,7 +463,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Can you sort a dictionary? Why or why not?

" + "Can you sort a dictionary? Why or why not?

can't sort dic since it's unordered list" ] }, { @@ -325,14 +477,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "What is the major difference between tuples and lists?

" + "What is the major difference between tuples and lists?

tuples are immutable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "How do you create a tuple?

" + "How do you create a tuple?

tu = (1,2,3)" ] }, { @@ -358,13 +510,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4, 11, 22, 33}" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list5 = [1,2,2,33,4,4,11,22,3,3,2]\n", "\n", - "\n" + "set(list5)\n" ] }, { @@ -427,7 +590,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -437,7 +602,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -447,7 +614,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -456,18 +625,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Answer before running cell\n", - "3.0 == 3" + "3.0 == 3\n", + "type(3.0)" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -483,16 +666,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# two nested lists\n", "l_one = [1,2,[3,4]]\n", "l_two = [1,2,{'k1':4}]\n", "\n", "# True or False?\n", - "l_one[2][0] >= l_two[2]['k1']" + "l_one[2][0] >= l_two[2]['k1'] #false" ] }, { @@ -520,7 +714,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/playground.ipynb b/00-Python Object and Data Structure Basics/playground.ipynb new file mode 100644 index 000000000..6832f3da1 --- /dev/null +++ b/00-Python Object and Data Structure Basics/playground.ipynb @@ -0,0 +1,65 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "d = {'key1':'he he', 'key2':['a', 'b', 'c']}" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "d['key2'][2] = d['key2'][2].upper()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'key1': 'he he', 'key2': ['a', 'b', 'C']}" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d" + ] + } + ], + "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 +} diff --git a/01-Python Comparison Operators/.ipynb_checkpoints/01-Comparison Operators-checkpoint.ipynb b/01-Python Comparison Operators/.ipynb_checkpoints/01-Comparison Operators-checkpoint.ipynb index d14ff8c99..15f053dd2 100644 --- a/01-Python Comparison Operators/.ipynb_checkpoints/01-Comparison Operators-checkpoint.ipynb +++ b/01-Python Comparison Operators/.ipynb_checkpoints/01-Comparison Operators-checkpoint.ipynb @@ -116,16 +116,16 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "True" + "False" ] }, - "execution_count": 3, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } @@ -368,7 +368,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/01-Python Comparison Operators/01-Comparison Operators.ipynb b/01-Python Comparison Operators/01-Comparison Operators.ipynb index d14ff8c99..15f053dd2 100644 --- a/01-Python Comparison Operators/01-Comparison Operators.ipynb +++ b/01-Python Comparison Operators/01-Comparison Operators.ipynb @@ -116,16 +116,16 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "True" + "False" ] }, - "execution_count": 3, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } @@ -368,7 +368,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/01-Python Comparison Operators/02-Chained Comparison Operators.ipynb b/01-Python Comparison Operators/02-Chained Comparison Operators.ipynb index 7a567d9bc..b3dc51f87 100644 --- a/01-Python Comparison Operators/02-Chained Comparison Operators.ipynb +++ b/01-Python Comparison Operators/02-Chained Comparison Operators.ipynb @@ -194,7 +194,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/02-Python Statements/.ipynb_checkpoints/06-List Comprehensions-checkpoint.ipynb b/02-Python Statements/.ipynb_checkpoints/06-List Comprehensions-checkpoint.ipynb index 256271bad..cc53da5d6 100644 --- a/02-Python Statements/.ipynb_checkpoints/06-List Comprehensions-checkpoint.ipynb +++ b/02-Python Statements/.ipynb_checkpoints/06-List Comprehensions-checkpoint.ipynb @@ -209,7 +209,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/02-Python Statements/01-Introduction to Python Statements.ipynb b/02-Python Statements/01-Introduction to Python Statements.ipynb index 4de998b19..251ec4925 100644 --- a/02-Python Statements/01-Introduction to Python Statements.ipynb +++ b/02-Python Statements/01-Introduction to Python Statements.ipynb @@ -117,7 +117,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/02-Python Statements/02-if, elif, and else Statements.ipynb b/02-Python Statements/02-if, elif, and else Statements.ipynb index 262bd6415..57fc517ba 100644 --- a/02-Python Statements/02-if, elif, and else Statements.ipynb +++ b/02-Python Statements/02-if, elif, and else Statements.ipynb @@ -200,7 +200,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/02-Python Statements/03-for Loops.ipynb b/02-Python Statements/03-for Loops.ipynb index 61f54fec6..ea659a985 100644 --- a/02-Python Statements/03-for Loops.ipynb +++ b/02-Python Statements/03-for Loops.ipynb @@ -619,7 +619,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/02-Python Statements/04-while Loops.ipynb b/02-Python Statements/04-while Loops.ipynb index f8ff55903..3f8183fe6 100644 --- a/02-Python Statements/04-while Loops.ipynb +++ b/02-Python Statements/04-while Loops.ipynb @@ -206,7 +206,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -288,7 +288,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/02-Python Statements/05-Useful-Operators.ipynb b/02-Python Statements/05-Useful-Operators.ipynb index 632406a02..4d7c0d9f2 100644 --- a/02-Python Statements/05-Useful-Operators.ipynb +++ b/02-Python Statements/05-Useful-Operators.ipynb @@ -20,16 +20,16 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "range(0, 11)" + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]" ] }, - "execution_count": 1, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -47,16 +47,16 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]" ] }, - "execution_count": 3, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -68,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -77,7 +77,7 @@ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]" ] }, - "execution_count": 4, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -579,7 +579,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/02-Python Statements/06-List Comprehensions.ipynb b/02-Python Statements/06-List Comprehensions.ipynb index 256271bad..cc53da5d6 100644 --- a/02-Python Statements/06-List Comprehensions.ipynb +++ b/02-Python Statements/06-List Comprehensions.ipynb @@ -209,7 +209,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/02-Python Statements/07-Statements Assessment Test.ipynb b/02-Python Statements/07-Statements Assessment Test.ipynb index b9e545460..5378028cf 100644 --- a/02-Python Statements/07-Statements Assessment Test.ipynb +++ b/02-Python Statements/07-Statements Assessment Test.ipynb @@ -33,7 +33,11 @@ "metadata": {}, "outputs": [], "source": [ - "#Code here" + "#Code here\n", + "ls = st.split()\n", + "for word in ls:\n", + " if word[0] == 's':\n", + " print(word)" ] }, { @@ -50,7 +54,10 @@ "metadata": {}, "outputs": [], "source": [ - "#Code Here" + "#Code Here\n", + "for num in range(0,11):\n", + " if(num%2 == 0):\n", + " print(num)" ] }, { @@ -68,7 +75,8 @@ "outputs": [], "source": [ "#Code in this cell\n", - "[]" + "list = [x for x in range(1,51) if x%3 == 0]\n", + "list" ] }, { @@ -94,7 +102,11 @@ "metadata": {}, "outputs": [], "source": [ - "#Code in this cell" + "#Code in this cell\n", + "splitStr = st.split()\n", + "for word in splitStr:\n", + " if len(word)%2 == 0:\n", + " print(word, \": even!\")" ] }, { @@ -111,7 +123,17 @@ "metadata": {}, "outputs": [], "source": [ - "#Code in this cell" + "#Code in this cell\n", + "for num in range(1, 101):\n", + " if num%3 == 0:\n", + " if num%5 == 0:\n", + " print(num, \": FizzBuzz\")\n", + " else:\n", + " print(num, \": Fizz\")\n", + " elif num%5 == 0:\n", + " print(num, \": Buzz\")\n", + " else:\n", + " print(num)" ] }, { @@ -124,7 +146,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -133,11 +155,26 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['C', 'a', 'l', 'o', 't', 'f', 'l', 'o', 'e', 'w', 'i', 't', 's']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#Code in this cell" + "#Code in this cell\n", + "#subli = st.split()\n", + "\n", + "li = [word[0] for word in st.split()]\n", + "li" ] }, { @@ -164,7 +201,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/02-Python Statements/09-Guessing Game Challenge.ipynb b/02-Python Statements/09-Guessing Game Challenge.ipynb index 97f6fb7eb..d1c1d563e 100644 --- a/02-Python Statements/09-Guessing Game Challenge.ipynb +++ b/02-Python Statements/09-Guessing Game Challenge.ipynb @@ -146,7 +146,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/02-Python Statements/10-Guessing Game Challenge - Solution.ipynb b/02-Python Statements/10-Guessing Game Challenge - Solution.ipynb index 3df229e00..9b83788d8 100644 --- a/02-Python Statements/10-Guessing Game Challenge - Solution.ipynb +++ b/02-Python Statements/10-Guessing Game Challenge - Solution.ipynb @@ -250,7 +250,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb index cb906dbe6..bc4611175 100644 --- a/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb +++ b/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb @@ -31,23 +31,39 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "def lesser_of_two_evens(a,b):\n", - " pass" + " if (a%2 == 0 and b%2 == 0):\n", + " if (a>b):\n", + " return b\n", + " else:\n", + " return a\n", + " else:\n", + " if (a>b):\n", + " return a\n", + " else:\n", + " return b" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "lesser_of_two_evens(2,4)" @@ -55,11 +71,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "lesser_of_two_evens(2,5)" @@ -76,23 +101,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 7, + "metadata": {}, "outputs": [], "source": [ "def animal_crackers(text):\n", - " pass" + " li = text.split()\n", + " if (li[0][0] == li[1][0]):\n", + " return True\n", + " else:\n", + " return False" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "animal_crackers('Levelheaded Llama')" @@ -100,11 +136,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "animal_crackers('Crazy Kangaroo')" @@ -177,23 +222,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 26, + "metadata": {}, "outputs": [], "source": [ "def old_macdonald(name):\n", - " pass" + " #print(enumerate(name))\n", + " li = [c.upper() if i==0 or i==3 else c for i, c in enumerate(name)]\n", + " return ''.join(li)" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'MacDonald'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "old_macdonald('macdonald')" @@ -221,23 +275,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 39, + "metadata": {}, "outputs": [], "source": [ "def master_yoda(text):\n", - " pass" + " li = list(reversed(text.split()))\n", + " return \" \".join(li)" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'home am I'" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "master_yoda('I am home')" @@ -245,11 +307,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ready are We'" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "master_yoda('We are ready')" @@ -271,23 +342,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 47, + "metadata": {}, "outputs": [], "source": [ "def almost_there(n):\n", - " pass" + " if abs(n - 100)<=10 or abs(n-200)<=10:\n", + " return True\n", + " else:\n", + " return False" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "almost_there(104)" @@ -295,11 +376,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "almost_there(150)" @@ -307,11 +397,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "almost_there(209)" @@ -339,23 +438,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 64, + "metadata": {}, "outputs": [], "source": [ "def has_33(nums):\n", - " pass" + " for i in range(0, len(nums)-1): #nếu check hết, lấy đến len(nums) thì chú ý nums[i+1] có thể không tồn tại\n", + " if nums[i]==3 and nums[i+1]==3:\n", + " return True\n", + " return False" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "has_33([1, 3, 3])" @@ -363,9 +472,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "has_33([1, 3, 1, 3])" @@ -373,9 +493,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "has_33([3, 1, 3])" @@ -392,23 +523,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 74, + "metadata": {}, "outputs": [], "source": [ "def paper_doll(text):\n", - " pass" + " li = [c + c + c for c in text]\n", + " return ''.join(li)" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'HHHeeellllllooo'" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "paper_doll('Hello')" @@ -416,11 +555,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'MMMiiissssssiiissssssiiippppppiii'" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "paper_doll('Mississippi')" @@ -438,23 +586,39 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 77, + "metadata": {}, "outputs": [], "source": [ "def blackjack(a,b,c):\n", - " pass" + " sum = a+b+c\n", + " if a+b+c<=21:\n", + " return sum\n", + " else:\n", + " if a==11 or b==11 or c==11:\n", + " sum = sum-10\n", + " if sum>21:\n", + " return \"BURST\"\n", + " else:\n", + " return sum" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "blackjack(5,6,7)" @@ -462,11 +626,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'BURST'" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "blackjack(9,9,9)" @@ -474,11 +647,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "19" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "blackjack(9,9,11)" @@ -497,23 +679,60 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 96, + "metadata": {}, "outputs": [], "source": [ "def summer_69(arr):\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + " flag = True\n", + " sum = 0\n", + " if len(arr)==0:\n", + " return sum\n", + " else: \n", + " for i in range(0, len(arr)):\n", + " if flag==False:\n", + " if arr[i]==9:\n", + " flag = True\n", + " continue\n", + " else:\n", + " continue\n", + " else:\n", + " if arr[i]==6:\n", + " flag = False\n", + " continue\n", + " else:\n", + " sum = sum + arr[i]\n", + " return sum" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "i = 0\n", + "sum = 1\n", + "i = 1\n", + "sum = 4\n", + "i = 2\n", + "sum = 9\n" + ] + }, + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "summer_69([1, 3, 5])" @@ -521,11 +740,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "summer_69([4, 5, 6, 7, 8, 9])" @@ -533,11 +761,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "i = 0\n", + "sum = 2\n", + "i = 1\n", + "sum = 3\n", + "i = 2\n", + "i = 3\n", + "i = 4\n" + ] + }, + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "summer_69([2, 1, 6, 9, 11])" @@ -707,7 +957,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/.ipynb_checkpoints/06-Nested Statements and Scope-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/06-Nested Statements and Scope-checkpoint.ipynb new file mode 100644 index 000000000..24555230e --- /dev/null +++ b/03-Methods and Functions/.ipynb_checkpoints/06-Nested Statements and Scope-checkpoint.ipynb @@ -0,0 +1,361 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Nested Statements and Scope \n", + "\n", + "Now that we have gone over writing our own functions, it's important to understand how Python deals with the variable names you assign. When you create a variable name in Python the name is stored in a *name-space*. Variable names also have a *scope*, the scope determines the visibility of that variable name to other parts of your code.\n", + "\n", + "Let's start with a quick thought experiment; imagine the following code:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "x = 25\n", + "\n", + "def printer():\n", + " x = 50\n", + " return x\n", + "\n", + "# print(x)\n", + "# print(printer())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What do you imagine the output of printer() is? 25 or 50? What is the output of print x? 25 or 50?" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "25\n" + ] + } + ], + "source": [ + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "50\n" + ] + } + ], + "source": [ + "print(printer())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Interesting! But how does Python know which **x** you're referring to in your code? This is where the idea of scope comes in. Python has a set of rules it follows to decide what variables (such as **x** in this case) you are referencing in your code. Lets break down the rules:" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "This idea of scope in your code is very important to understand in order to properly assign and call variable names. \n", + "\n", + "In simple terms, the idea of scope can be described by 3 general rules:\n", + "\n", + "1. Name assignments will create or change local names by default.\n", + "2. Name references search (at most) four scopes, these are:\n", + " * local\n", + " * enclosing functions\n", + " * global\n", + " * built-in\n", + "3. Names declared in global and nonlocal statements map assigned names to enclosing module and function scopes.\n", + "\n", + "\n", + "The statement in #2 above can be defined by the LEGB rule.\n", + "\n", + "**LEGB Rule:**\n", + "\n", + "L: Local — Names assigned in any way within a function (def or lambda), and not declared global in that function.\n", + "\n", + "E: Enclosing function locals — Names in the local scope of any and all enclosing functions (def or lambda), from inner to outer.\n", + "\n", + "G: Global (module) — Names assigned at the top-level of a module file, or declared global in a def within the file.\n", + "\n", + "B: Built-in (Python) — Names preassigned in the built-in names module : open, range, SyntaxError,..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Quick examples of LEGB\n", + "\n", + "### Local" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# x is local here:\n", + "f = lambda x:x**2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Enclosing function locals\n", + "This occurs when we have a function inside a function (nested functions)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello Sammy\n" + ] + } + ], + "source": [ + "name = 'This is a global name'\n", + "\n", + "def greet():\n", + " # Enclosing function\n", + " name = 'Sammy'\n", + " \n", + " def hello():\n", + " print('Hello '+name)\n", + " \n", + " hello()\n", + "\n", + "greet()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note how Sammy was used, because the hello() function was enclosed inside of the greet function!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Global\n", + "Luckily in Jupyter a quick way to test for global variables is to see if another cell recognizes the variable!" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a global name\n" + ] + } + ], + "source": [ + "print(name)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Built-in\n", + "These are the built-in function names in Python (don't overwrite these!)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Local Variables\n", + "When you declare variables inside a function definition, they are not related in any way to other variables with the same names used outside the function - i.e. variable names are local to the function. This is called the scope of the variable. All variables have the scope of the block they are declared in starting from the point of definition of the name.\n", + "\n", + "Example:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x is 50\n", + "Changed local x to 2\n", + "x is still 50\n" + ] + } + ], + "source": [ + "x = 50\n", + "\n", + "def func(x):\n", + " print('x is', x)\n", + " x = 2\n", + " print('Changed local x to', x)\n", + "\n", + "func(x)\n", + "print('x is still', x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The first time that we print the value of the name **x** with the first line in the function’s body, Python uses the value of the parameter declared in the main block, above the function definition.\n", + "\n", + "Next, we assign the value 2 to **x**. The name **x** is local to our function. So, when we change the value of **x** in the function, the **x** defined in the main block remains unaffected.\n", + "\n", + "With the last print statement, we display the value of **x** as defined in the main block, thereby confirming that it is actually unaffected by the local assignment within the previously called function.\n", + "\n", + "## The global statement\n", + "If you want to assign a value to a name defined at the top level of the program (i.e. not inside any kind of scope such as functions or classes), then you have to tell Python that the name is not local, but it is global. We do this using the global statement. It is impossible to assign a value to a variable defined outside a function without the global statement.\n", + "\n", + "You can use the values of such variables defined outside the function (assuming there is no variable with the same name within the function). However, this is not encouraged and should be avoided since it becomes unclear to the reader of the program as to where that variable’s definition is. Using the global statement makes it amply clear that the variable is defined in an outermost block.\n", + "\n", + "Example:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Before calling func(), x is: 50\n", + "This function is now using the global x!\n", + "Because of global x is: 50\n", + "Ran func(), changed global x to 2\n", + "Value of x (outside of func()) is: 2\n" + ] + } + ], + "source": [ + "x = 50\n", + "\n", + "def func():\n", + " global x\n", + " print('This function is now using the global x!')\n", + " print('Because of global x is: ', x)\n", + " x = 2\n", + " print('Ran func(), changed global x to', x)\n", + "\n", + "print('Before calling func(), x is: ', x)\n", + "func()\n", + "print('Value of x (outside of func()) is: ', x)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "The global statement is used to declare that **x** is a global variable - hence, when we assign a value to **x** inside the function, that change is reflected when we use the value of **x** in the main block.\n", + "\n", + "You can specify more than one global variable using the same global statement e.g. global x, y, z." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "## Conclusion\n", + "You should now have a good understanding of Scope (you may have already intuitively felt right about Scope which is great!) One last mention is that you can use the **globals()** and **locals()** functions to check what are your current local and global variables.\n", + "\n", + "Another thing to keep in mind is that everything in Python is an object! I can assign variables to functions just like I can with numbers! We will go over this again in the decorator section of the course!" + ] + } + ], + "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": 1 +} diff --git a/03-Methods and Functions/.ipynb_checkpoints/08-Functions and Methods Homework-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/08-Functions and Methods Homework-checkpoint.ipynb new file mode 100644 index 000000000..8e88344bd --- /dev/null +++ b/03-Methods and Functions/.ipynb_checkpoints/08-Functions and Methods Homework-checkpoint.ipynb @@ -0,0 +1,386 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Functions and Methods Homework \n", + "\n", + "Complete the following questions:\n", + "____\n", + "**Write a function that computes the volume of a sphere given its radius.**\n", + "

The volume of a sphere is given as $$\\frac{4}{3} πr^3$$

" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def vol(rad):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "33.49333333333333" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Check\n", + "vol(2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "**Write a function that checks whether a number is in a given range (inclusive of high and low)**" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def ran_check(num,low,high):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 is in the range between 2 and 7\n" + ] + } + ], + "source": [ + "# Check\n", + "ran_check(5,2,7)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you only wanted to return a boolean:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def ran_bool(num,low,high):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ran_bool(3,1,10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "____\n", + "**Write a Python function that accepts a string and calculates the number of upper case letters and lower case letters.**\n", + "\n", + " Sample String : 'Hello Mr. Rogers, how are you this fine Tuesday?'\n", + " Expected Output : \n", + " No. of Upper case characters : 4\n", + " No. of Lower case Characters : 33\n", + "\n", + "HINT: Two string methods that might prove useful: **.isupper()** and **.islower()**\n", + "\n", + "If you feel ambitious, explore the Collections module to solve this problem!" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "def up_low(s):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original String : Hello Mr. Rogers, how are you this fine Tuesday?\n", + "No. of Upper case characters : 4\n", + "No. of Lower case Characters : 33\n" + ] + } + ], + "source": [ + "s = 'Hello Mr. Rogers, how are you this fine Tuesday?'\n", + "up_low(s)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "____\n", + "**Write a Python function that takes a list and returns a new list with unique elements of the first list.**\n", + "\n", + " Sample List : [1,1,1,1,2,2,3,3,3,3,4,5]\n", + " Unique List : [1, 2, 3, 4, 5]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def unique_list(lst):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_list([1,1,1,1,2,2,3,3,3,3,4,5])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "____\n", + "**Write a Python function to multiply all the numbers in a list.**\n", + "\n", + " Sample List : [1, 2, 3, -4]\n", + " Expected Output : -24" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def multiply(numbers): \n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-24" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "multiply([1,2,3,-4])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "____\n", + "**Write a Python function that checks whether a passed in string is palindrome or not.**\n", + "\n", + "Note: A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "def palindrome(s):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "palindrome('helleh')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "____\n", + "#### Hard:\n", + "\n", + "**Write a Python function to check whether a string is pangram or not.**\n", + "\n", + " Note : Pangrams are words or sentences containing every letter of the alphabet at least once.\n", + " For example : \"The quick brown fox jumps over the lazy dog\"\n", + "\n", + "Hint: Look at the string module" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "import string\n", + "\n", + "def ispangram(str1, alphabet=string.ascii_lowercase):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ispangram(\"The quick brown fox jumps over the lazy dog\")" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'abcdefghijklmnopqrstuvwxyz'" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "string.ascii_lowercase" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "#### Great Job!" + ] + } + ], + "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": 1 +} diff --git a/03-Methods and Functions/01-Methods.ipynb b/03-Methods and Functions/01-Methods.ipynb index fde90b46d..5fbbc5edf 100644 --- a/03-Methods and Functions/01-Methods.ipynb +++ b/03-Methods and Functions/01-Methods.ipynb @@ -170,7 +170,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/02-Functions.ipynb b/03-Methods and Functions/02-Functions.ipynb index 1e9243d60..8fb172055 100644 --- a/03-Methods and Functions/02-Functions.ipynb +++ b/03-Methods and Functions/02-Functions.ipynb @@ -383,7 +383,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/03-Function Practice Exercises.ipynb b/03-Methods and Functions/03-Function Practice Exercises.ipynb index cb906dbe6..1a962d916 100644 --- a/03-Methods and Functions/03-Function Practice Exercises.ipynb +++ b/03-Methods and Functions/03-Function Practice Exercises.ipynb @@ -31,23 +31,39 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "def lesser_of_two_evens(a,b):\n", - " pass" + " if (a%2 == 0 and b%2 == 0):\n", + " if (a>b):\n", + " return b\n", + " else:\n", + " return a\n", + " else:\n", + " if (a>b):\n", + " return a\n", + " else:\n", + " return b" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "lesser_of_two_evens(2,4)" @@ -55,11 +71,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "lesser_of_two_evens(2,5)" @@ -76,23 +101,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 7, + "metadata": {}, "outputs": [], "source": [ "def animal_crackers(text):\n", - " pass" + " li = text.split()\n", + " if (li[0][0] == li[1][0]):\n", + " return True\n", + " else:\n", + " return False" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "animal_crackers('Levelheaded Llama')" @@ -100,11 +136,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "animal_crackers('Crazy Kangaroo')" @@ -177,23 +222,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 26, + "metadata": {}, "outputs": [], "source": [ "def old_macdonald(name):\n", - " pass" + " #print(enumerate(name))\n", + " li = [c.upper() if i==0 or i==3 else c for i, c in enumerate(name)]\n", + " return ''.join(li)" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'MacDonald'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "old_macdonald('macdonald')" @@ -221,23 +275,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 39, + "metadata": {}, "outputs": [], "source": [ "def master_yoda(text):\n", - " pass" + " li = list(reversed(text.split()))\n", + " return \" \".join(li)" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'home am I'" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "master_yoda('I am home')" @@ -245,11 +307,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ready are We'" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "master_yoda('We are ready')" @@ -271,23 +342,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 47, + "metadata": {}, "outputs": [], "source": [ "def almost_there(n):\n", - " pass" + " if abs(n - 100)<=10 or abs(n-200)<=10:\n", + " return True\n", + " else:\n", + " return False" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "almost_there(104)" @@ -295,11 +376,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "almost_there(150)" @@ -307,11 +397,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "almost_there(209)" @@ -339,23 +438,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 64, + "metadata": {}, "outputs": [], "source": [ "def has_33(nums):\n", - " pass" + " for i in range(0, len(nums)-1): #nếu check hết, lấy đến len(nums) thì chú ý nums[i+1] có thể không tồn tại\n", + " if nums[i]==3 and nums[i+1]==3:\n", + " return True\n", + " return False" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "has_33([1, 3, 3])" @@ -363,9 +472,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "has_33([1, 3, 1, 3])" @@ -373,9 +493,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "has_33([3, 1, 3])" @@ -392,23 +523,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 74, + "metadata": {}, "outputs": [], "source": [ "def paper_doll(text):\n", - " pass" + " li = [c + c + c for c in text]\n", + " return ''.join(li)" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'HHHeeellllllooo'" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "paper_doll('Hello')" @@ -416,11 +555,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'MMMiiissssssiiissssssiiippppppiii'" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "paper_doll('Mississippi')" @@ -438,23 +586,39 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 77, + "metadata": {}, "outputs": [], "source": [ "def blackjack(a,b,c):\n", - " pass" + " sum = a+b+c\n", + " if a+b+c<=21:\n", + " return sum\n", + " else:\n", + " if a==11 or b==11 or c==11:\n", + " sum = sum-10\n", + " if sum>21:\n", + " return \"BURST\"\n", + " else:\n", + " return sum" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "blackjack(5,6,7)" @@ -462,11 +626,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'BURST'" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "blackjack(9,9,9)" @@ -474,11 +647,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "19" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "blackjack(9,9,11)" @@ -497,23 +679,48 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 98, + "metadata": {}, "outputs": [], "source": [ "def summer_69(arr):\n", - " pass" + " flag = True\n", + " sum = 0\n", + " if len(arr)==0:\n", + " return sum\n", + " else: \n", + " for i in range(0, len(arr)):\n", + " if flag==False:\n", + " if arr[i]==9:\n", + " flag = True\n", + " continue\n", + " else:\n", + " continue\n", + " else:\n", + " if arr[i]==6:\n", + " flag = False\n", + " continue\n", + " else:\n", + " sum = sum + arr[i]\n", + " return sum" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "summer_69([1, 3, 5])" @@ -521,11 +728,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 100, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "summer_69([4, 5, 6, 7, 8, 9])" @@ -533,11 +749,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 101, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "summer_69([2, 1, 6, 9, 11])" @@ -563,23 +788,39 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 119, + "metadata": {}, "outputs": [], "source": [ "def spy_game(nums):\n", - " pass" + " li = []\n", + " for i in range(0, len(nums)):\n", + " if nums[i]==0 or nums[i]==7:\n", + " li.append(nums[i])\n", + " \n", + " for i in range(0, len(li)-2):\n", + " if li[i]==0 and li[i+1]==0 and li[i+2]==7:\n", + " return True\n", + " else:\n", + " return False" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 120, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 120, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "spy_game([1,2,4,0,0,7,5])" @@ -587,11 +828,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 121, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 121, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "spy_game([1,0,2,4,0,5,7])" @@ -599,11 +849,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 122, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "spy_game([1,7,2,0,4,5,0])" @@ -621,24 +880,74 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 6, + "metadata": {}, "outputs": [], "source": [ "def count_primes(num):\n", - " pass\n", + " count = 0\n", + " \n", + " for checked_num in range(2, num+1):\n", + " uoc_count = 0\n", + " for i in range(1, checked_num+1):\n", + " if checked_num%i==0:\n", + " uoc_count = uoc_count + 1\n", + " \n", + " if uoc_count==2:\n", + " count = count + 1\n", + " print(checked_num)\n", + " \n", + " return count\n", " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "3\n", + "5\n", + "7\n", + "11\n", + "13\n", + "17\n", + "19\n", + "23\n", + "29\n", + "31\n", + "37\n", + "41\n", + "43\n", + "47\n", + "53\n", + "59\n", + "61\n", + "67\n", + "71\n", + "73\n", + "79\n", + "83\n", + "89\n", + "97\n" + ] + }, + { + "data": { + "text/plain": [ + "25" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check\n", "count_primes(100)" @@ -707,7 +1016,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/04-Function Practice Exercises - Solutions.ipynb b/03-Methods and Functions/04-Function Practice Exercises - Solutions.ipynb index 33ea1eea2..25d832e8f 100644 --- a/03-Methods and Functions/04-Function Practice Exercises - Solutions.ipynb +++ b/03-Methods and Functions/04-Function Practice Exercises - Solutions.ipynb @@ -1098,7 +1098,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/05-Lambda-Expressions-Map-and-Filter.ipynb b/03-Methods and Functions/05-Lambda-Expressions-Map-and-Filter.ipynb index 4243e6898..94cab1bf0 100644 --- a/03-Methods and Functions/05-Lambda-Expressions-Map-and-Filter.ipynb +++ b/03-Methods and Functions/05-Lambda-Expressions-Map-and-Filter.ipynb @@ -561,7 +561,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/06-Nested Statements and Scope.ipynb b/03-Methods and Functions/06-Nested Statements and Scope.ipynb index c3871db28..24555230e 100644 --- a/03-Methods and Functions/06-Nested Statements and Scope.ipynb +++ b/03-Methods and Functions/06-Nested Statements and Scope.ipynb @@ -14,7 +14,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "x = 25\n", @@ -119,7 +121,9 @@ { "cell_type": "code", "execution_count": 4, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# x is local here:\n", @@ -349,7 +353,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/08-Functions and Methods Homework.ipynb b/03-Methods and Functions/08-Functions and Methods Homework.ipynb index 95d610956..8e88344bd 100644 --- a/03-Methods and Functions/08-Functions and Methods Homework.ipynb +++ b/03-Methods and Functions/08-Functions and Methods Homework.ipynb @@ -378,7 +378,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/.ipynb_checkpoints/02-Object Oriented Programming Homework-checkpoint.ipynb b/05-Object Oriented Programming/.ipynb_checkpoints/02-Object Oriented Programming Homework-checkpoint.ipynb index 3824be7be..0a2d98391 100644 --- a/05-Object Oriented Programming/.ipynb_checkpoints/02-Object Oriented Programming Homework-checkpoint.ipynb +++ b/05-Object Oriented Programming/.ipynb_checkpoints/02-Object Oriented Programming Homework-checkpoint.ipynb @@ -13,17 +13,20 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, + "execution_count": 5, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Line:\n", " \n", " def __init__(self,coor1,coor2):\n", - " pass\n", + " self.coor1 = coor1\n", + " self.coor2 = coor2\n", " \n", " def distance(self):\n", - " pass\n", + " return ((self.coor1[0]-self.coor2[0])**2 + (self.coor1[1]-self.coor2[1])**2)**0.5\n", " \n", " def slope(self):\n", " pass" @@ -31,7 +34,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -45,7 +48,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -54,7 +57,7 @@ "9.433981132056603" ] }, - "execution_count": 3, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -101,7 +104,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Cylinder:\n", @@ -119,7 +124,9 @@ { "cell_type": "code", "execution_count": 6, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# EXAMPLE OUTPUT\n", @@ -183,7 +190,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/01-Object Oriented Programming.ipynb b/05-Object Oriented Programming/01-Object Oriented Programming.ipynb index 4d9e12e27..d97afcdcd 100644 --- a/05-Object Oriented Programming/01-Object Oriented Programming.ipynb +++ b/05-Object Oriented Programming/01-Object Oriented Programming.ipynb @@ -28,7 +28,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "lst = [1,2,3]" @@ -159,7 +161,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Dog:\n", @@ -244,7 +248,9 @@ { "cell_type": "code", "execution_count": 8, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Dog:\n", @@ -260,7 +266,9 @@ { "cell_type": "code", "execution_count": 9, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "sam = Dog('Lab','Sam')" @@ -415,7 +423,9 @@ { "cell_type": "code", "execution_count": 14, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Animal:\n", @@ -701,8 +711,10 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, + "execution_count": 10, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Book:\n", @@ -724,7 +736,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -732,6 +744,7 @@ "output_type": "stream", "text": [ "A book is created\n", + "A book is destroyed\n", "Title: Python Rocks!, author: Jose Portilla, pages: 159\n", "159\n", "A book is destroyed\n" @@ -784,7 +797,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/02-Object Oriented Programming Homework.ipynb b/05-Object Oriented Programming/02-Object Oriented Programming Homework.ipynb index 3824be7be..0a2d98391 100644 --- a/05-Object Oriented Programming/02-Object Oriented Programming Homework.ipynb +++ b/05-Object Oriented Programming/02-Object Oriented Programming Homework.ipynb @@ -13,17 +13,20 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, + "execution_count": 5, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Line:\n", " \n", " def __init__(self,coor1,coor2):\n", - " pass\n", + " self.coor1 = coor1\n", + " self.coor2 = coor2\n", " \n", " def distance(self):\n", - " pass\n", + " return ((self.coor1[0]-self.coor2[0])**2 + (self.coor1[1]-self.coor2[1])**2)**0.5\n", " \n", " def slope(self):\n", " pass" @@ -31,7 +34,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -45,7 +48,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -54,7 +57,7 @@ "9.433981132056603" ] }, - "execution_count": 3, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -101,7 +104,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Cylinder:\n", @@ -119,7 +124,9 @@ { "cell_type": "code", "execution_count": 6, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# EXAMPLE OUTPUT\n", @@ -183,7 +190,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/04-OOP Challenge.ipynb b/05-Object Oriented Programming/04-OOP Challenge.ipynb index fb60ffb74..7f173e632 100644 --- a/05-Object Oriented Programming/04-OOP Challenge.ipynb +++ b/05-Object Oriented Programming/04-OOP Challenge.ipynb @@ -23,17 +23,31 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, + "execution_count": 24, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Account:\n", - " pass" + " def __init__(self, owner, balance):\n", + " self.owner = owner\n", + " self.balance = balance\n", + " \n", + " def deposit(self, amount):\n", + " self.balance += amount\n", + " return \"Deposit Accepted\"\n", + " \n", + " def withdraw(self, amount):\n", + " if self.balance>=amount:\n", + " self.balance -= amount\n", + " return \"Withdrawal accepted\"\n", + " return \"Funds Unavailable\"" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ @@ -43,26 +57,29 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 26, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Account owner: Jose\n", - "Account balance: $100\n" - ] + "data": { + "text/plain": [ + "100" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "# 2. Print the object\n", - "print(acct1)" + "acct1.owner\n", + "acct1.balance" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -71,7 +88,7 @@ "'Jose'" ] }, - "execution_count": 4, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -83,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 28, "metadata": {}, "outputs": [ { @@ -92,7 +109,7 @@ "100" ] }, - "execution_count": 5, + "execution_count": 28, "metadata": {}, "output_type": "execute_result" } @@ -104,33 +121,40 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 29, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Deposit Accepted\n" - ] + "data": { + "text/plain": [ + "150" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "# 5. Make a series of deposits and withdrawals\n", - "acct1.deposit(50)" + "acct1.deposit(50)\n", + "acct1.balance" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 30, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Withdrawal Accepted\n" - ] + "data": { + "text/plain": [ + "'Withdrawal accepted'" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -139,15 +163,18 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 31, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Funds Unavailable!\n" - ] + "data": { + "text/plain": [ + "'Funds Unavailable'" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -179,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/06-Modules and Packages/.ipynb_checkpoints/Useful_Info_Notebook-checkpoint.ipynb b/06-Modules and Packages/.ipynb_checkpoints/Useful_Info_Notebook-checkpoint.ipynb new file mode 100644 index 000000000..f64fe1f3a --- /dev/null +++ b/06-Modules and Packages/.ipynb_checkpoints/Useful_Info_Notebook-checkpoint.ipynb @@ -0,0 +1,209 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Modules and Packages\n", + "\n", + "There's no code here because it didn't really make sense for the section. Check out the video lectures for more info and the resources for this.\n", + "\n", + "Here is the best source the official docs!\n", + "https://docs.python.org/3/tutorial/modules.html#packages\n", + "\n", + "But I really like the info here: https://python4astronomers.github.io/installation/packages.html\n", + "\n", + "Here's some extra info to help:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Modules in Python are simply Python files with the .py extension, which implement a set of functions. Modules are imported from other modules using the import command.\n", + "\n", + "To import a module, we use the import command. Check out the full list of built-in modules in the Python standard library [here](https://docs.python.org/3/py-modindex.html).\n", + "\n", + "The first time a module is loaded into a running Python script, it is initialized by executing the code in the module once. If another module in your code imports the same module again, it will not be loaded twice but once only - so local variables inside the module act as a \"singleton\" - they are initialized only once.\n", + "\n", + "If we want to import the math module, we simply import the name of the module:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# import the library\n", + "import math" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# use it (ceiling rounding)\n", + "math.ceil(2.4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exploring built-in modules\n", + "Two very important functions come in handy when exploring modules in Python - the dir and help functions.\n", + "\n", + "We can look for which functions are implemented in each module by using the dir function:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']\n" + ] + } + ], + "source": [ + "print(dir(math))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "When we find the function in the module we want to use, we can read about it more using the help function, inside the Python interpreter:\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function ceil in module math:\n", + "\n", + "ceil(...)\n", + " ceil(x)\n", + " \n", + " Return the ceiling of x as an Integral.\n", + " This is the smallest integer >= x.\n", + "\n" + ] + } + ], + "source": [ + "help(math.ceil)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Writing modules\n", + "Writing Python modules is very simple. To create a module of your own, simply create a new .py file with the module name, and then import it using the Python file name (without the .py extension) using the import command.\n", + "\n", + "## Writing packages\n", + "Packages are name-spaces which contain multiple packages and modules themselves. They are simply directories, but with a twist.\n", + "\n", + "Each package in Python is a directory which MUST contain a special file called **\\__init\\__.py**. This file can be empty, and it indicates that the directory it contains is a Python package, so it can be imported the same way a module can be imported.\n", + "\n", + "If we create a directory called foo, which marks the package name, we can then create a module inside that package called bar. We also must not forget to add the **\\__init\\__.py** file inside the foo directory.\n", + "\n", + "To use the module bar, we can import it in two ways:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Just an example, this won't work\n", + "import foo.bar" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# OR could do it this way\n", + "from foo import bar" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the first method, we must use the foo prefix whenever we access the module bar. In the second method, we don't, because we import the module to our module's name-space.\n", + "\n", + "The **\\__init\\__.py** file can also decide which modules the package exports as the API, while keeping other modules internal, by overriding the **\\__all\\__** variable, like so:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "__init__.py:\n", + "\n", + "__all__ = [\"bar\"]" + ] + } + ], + "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": 1 +} diff --git a/06-Modules and Packages/Useful_Info_Notebook.ipynb b/06-Modules and Packages/Useful_Info_Notebook.ipynb index 786b0485a..f64fe1f3a 100644 --- a/06-Modules and Packages/Useful_Info_Notebook.ipynb +++ b/06-Modules and Packages/Useful_Info_Notebook.ipynb @@ -32,7 +32,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# import the library\n", @@ -151,7 +153,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# OR could do it this way\n", @@ -197,7 +201,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.3" } }, "nbformat": 4,