{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Objects and Data Structures Assessment Test" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Test your knowledge. \n", "\n", "** Answer the following questions **" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Write a brief description of all the following Object Types and Data Structures we've learned about: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**For the full answers, review the Jupyter notebook introductions of each topic!**\n", "\n", "[Numbers](http://nbviewer.ipython.org/github/jmportilla/Complete-Python-Bootcamp/blob/master/Numbers.ipynb)\n", "\n", "[Strings](http://nbviewer.ipython.org/github/jmportilla/Complete-Python-Bootcamp/blob/master/Strings.ipynb)\n", "\n", "[Lists](http://nbviewer.ipython.org/github/jmportilla/Complete-Python-Bootcamp/blob/master/Lists.ipynb)\n", "\n", "[Tuples](http://nbviewer.ipython.org/github/jmportilla/Complete-Python-Bootcamp/blob/master/Tuples.ipynb)\n", "\n", "[Dictionaries](http://nbviewer.ipython.org/github/jmportilla/Complete-Python-Bootcamp/blob/master/Dictionaries.ipynb)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numbers\n", "\n", "Write an equation that uses multiplication, division, an exponent, addition, and subtraction that is equal to 100.25.\n", "\n", "Hint: This is just to test your memory of the basic arithmetic commands, work backwards from 100.25" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "100.25" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Your answer is probably different\n", "(20000 - (10 ** 2) / 12 * 34) - 19627.75" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Explain what the cell below will produce and why. Can you change it so the answer is correct?" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2/3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer: Because Python 2 performs classic division for integers. Use floats to perform true division. For example:\n", "2.0/3**" ] }, { "cell_type": "markdown", "metadata": {}, "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", " \n", " What is the value of the expression 4 * 6 + 5 \n", " \n", " What is the value of the expression 4 + 6 * 5 " ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "44" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 * (6 + 5)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "29" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 * 6 + 5 " ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "34" ] }, "execution_count": 18, "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?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer: Floating Point Number**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What would you use to find a number’s square root, as well as its square? " ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "10.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "100 ** 0.5" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 ** 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given the string 'hello' give an index command that returns 'e'. Use the code below:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'e'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'hello'\n", "# Print out 'e' using indexing\n", "s[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reverse the string 'hello' using indexing:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'olleh'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s ='hello'\n", "\n", "# Reverse the string using indexing\n", "\n", "s[::-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given the string hello, give two methods of producing the letter 'o' using indexing." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'o'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s ='hello'\n", "\n", "# Print out the\n", "\n", "s[-1]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'o'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Build this list [0,0,0] two separate ways." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 0, 0]" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Method 1\n", "[0]*3" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 0, 0]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Method 2\n", "l = [0,0,0]\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reassign 'hello' in this nested list to say 'goodbye' item in this list:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "l = [1,2,[3,4,'hello']]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [], "source": [ "l[2][2] = 'goodbye'" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, [3, 4, 'goodbye']]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sort the list below:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "l = [5,3,4,6,1]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 3, 4, 5, 6]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Method 1\n", "sorted(l)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 3, 4, 5, 6]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Method 2\n", "l.sort()\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using keys and indexing, grab the 'hello' from the following dictionaries:" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": true }, "outputs": [], "source": [ "d = {'simple_key':'hello'}\n", "# Grab 'hello'" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['simple_key']" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": true }, "outputs": [], "source": [ "d = {'k1':{'k2':'hello'}}\n", "# Grab 'hello'" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['k1']['k2']" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Getting a little tricker\n", "d = {'k1':[{'nest_key':['this is deep',['hello']]}]}" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This was harder than I expected...\n", "d['k1'][0]['nest_key'][1][0]" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# This will be hard and annoying!\n", "d = {'k1':[1,2,{'k2':['this is tricky',{'tough':[1,2,['hello']]}]}]}" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Phew\n", "d['k1'][2]['k2'][1]['tough'][2][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you sort a dictionary? Why or why not?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer: No! Because normal dictionaries are *mappings* not a sequence. **" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is the major difference between tuples and lists?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tuples are immutable!**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How do you create a tuple?" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t = (1,2,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sets " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is unique about a set?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer: They don't allow for duplicate items!**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use a set to find the unique values of the list below:" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": true }, "outputs": [], "source": [ "l = [1,2,2,33,4,4,11,22,3,3,2]" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 11, 22, 33}" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Booleans" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the following quiz questions, we will get a preview of comparison operators:\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
OperatorDescriptionExample
==If the values of two operands are equal, then the condition becomes true. (a == b) is not true.
!=If values of two operands are not equal, then condition becomes true.
<>If values of two operands are not equal, then condition becomes true. (a <> b) is true. This is similar to != operator.
>If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true.
<If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true.
>=If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true.
<=If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true.
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What will be the resulting Boolean of the following pieces of code (answer fist then check by typing it in!)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Answer before running cell\n", "2 > 3" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Answer before running cell\n", "3 <= 2" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Answer before running cell\n", "3 == 2.0" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Answer before running cell\n", "3.0 == 3" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Answer before running cell\n", "4**0.5 != 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Final Question: What is the boolean output of the cell block below?" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 71, "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']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Great Job on your first assessment! " ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "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.5.3" } }, "nbformat": 4, "nbformat_minor": 0 }