Skip to content

Instantly share code, notes, and snippets.

@kanungo
Last active August 29, 2015 14:05
Show Gist options
  • Save kanungo/49b6bb5549529ae0a912 to your computer and use it in GitHub Desktop.
Save kanungo/49b6bb5549529ae0a912 to your computer and use it in GitHub Desktop.
This is the code that we worked on in the first class
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"# ------------------------------------------------------\n",
"# Assignment exercise\n",
"myint = 3\n",
"print 'the value of myint is ', myint \n",
"myfloat = 4.3\n",
"print 'the value of myfloat is ', myfloat \n",
"myint = 3.3\n",
"print 'the value of myint is ', myint \n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# ------------------------------------------------------\n",
"# Operators exercise\n",
"# Try to compute the outcome before executing the statements\n",
"var1 = 6\n",
"print 'the value of var1 is ', var1 \n",
"\n",
"var2 = 3.5\n",
"print 'the value of var2 is ', var2 \n",
"\n",
"var3 = var1 / var2\n",
"print 'the value of var3 is ', var3 \n",
"\n",
"var4 = var1 // var2\n",
"print 'the value of var4 is ', var4 \n",
"\n",
"var5 = var1 % var2\n",
"print 'the value of var5 is ', var5 \n",
"\n",
"var6 = True\n",
"print 'the value of var6 is ', var6 \n",
"\n",
"var7 = (7 > 8)\n",
"print 'the value of var7 is ', var7 \n",
"\n",
"var8 = var1 + var6 + var7\n",
"print 'the value of var8 is ', var8 \n",
"\n",
"var1 = None\n",
"print 'the value of var1 is ', var1 \n",
"\n",
"del (var2)\n",
"print 'the value of var2 is ', var2 \n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# ------------------------------------------------------\n",
"# String exercise\n",
"# Make s tring\n",
"s1 = \"DNSC6119 Programming for Analytics\"\n",
"print 's1 is ', s1\n",
"# Access different parts of a string\n",
"print 's1[0] is --> ', s1[0]\n",
"print 's1[-1] is --> ', s1[-1]\n",
"print 's1[-2] is --> ', s1[-2]\n",
"print 's1[2:6] is --> ', s1[2:6]\n",
"print 's1[4:] is --> ', s1[4:]\n",
"print 's1[:10] is --> ', s1[:10]\n",
"\n",
"# Make sure you understand what happens here\n",
"print \"DN\" in s1\n",
"print \"For\" in s1\n",
"\n",
"# Functions associated with strings\n",
"print 's1.title() is --> ', s1.title() \n",
"print 's1.swapcase() --> ', s1.swapcase()\n",
"print 's1.upper() --> ', s1.upper()\n",
"print 's1.lower() --> ', s1.lower()\n",
"print 's1.capitalize() --> ', s1.capitalize()\n",
"\n",
"# More functions associated with strings (why the double quotes?) \n",
"print \"s1.count('a') is --> \", s1.count('a')\n",
"print \"s1.find('mm') is --> \", s1.find('mm')\n",
"print \"s1[s1.index('mm'):] is --> \", s1[s1.index('mm'):]\n",
"print \"s1.rfind('a') is --> \", s1.rfind('a')\n",
"print \"s1[s1.index('a'):s1.rindex('a')] is --> \", s1[s1.index('a'):s1.rindex('a')]\n",
"\n",
"s2 = s1.replace('a', 'Z')\n",
"print 's2 is --> ', s2\n",
"\n",
"mylist = s1.split(' ')\n",
"print 'mylist is --> ', mylist\n",
"\n",
"s3 = mylist[1] + ' ' + mylist[2] + ' ' + mylist[3]\n",
"print 's3 is --> ', s3\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# ------------------------------------------------------\n",
"# List exercise\n",
"# This is to show the difference between strings and lists (mutability)\n",
"# Define a string named s\n",
"s = 'a3a'\n",
"print 's[1] --> ', s[1]\n",
"s[1] = 'a' # should give you an error, why? Delete this line and proceed\n",
"\n",
"# Now to lists; define a list named ls\n",
"ls = []\n",
"print 'ls is --> ', ls\n",
"ls.append('a')\n",
"ls.append('3')\n",
"ls.append('a')\n",
"print 'ls is --> ', ls\n",
"\n",
"ls[1] = ('3')\n",
"print 'ls is --> ', ls\n",
"\n",
"# Do this on your own\n",
"# 1. Create a list of four of your friends\n",
"# 2. Add one more friend to the list\n",
"# 3. Search for a specific friend in your list\n",
"# 4. Delete the second friend in your list\n",
"# 5. Use the \u201c+\u201d operator on your list\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# ------------------------------------------------------\n",
"# Tuples exercise\n",
"# Syntactically, a tuple is a comma-separated list of values:\n",
"t = 'a', 'b', 'c', 'd', 'e'\n",
"# and \n",
"t = ('a', 'b', 'c', 'd', 'e') \n",
"# are identical\n",
"# To create a tuple with a single element, you have to include a final comma:\n",
"t1 = 'a',\n",
"type(t1)\n",
"# A value in parentheses is not a tuple:\n",
"t2 = ('a')\n",
"type(t2)\n",
"# If the argument is a sequence (string, list or tuple), the result is a tuple with the elements of the sequence:\n",
"t = tuple('lupins')\n",
"print t ('l', 'u', 'p', 'i', 'n', 's')\n",
"# Most list operators also work on tuples. The bracket operator indexes an element:\n",
"t = ('a', 'b', 'c', 'd', 'e')\n",
"print t[0]\n",
"# And the slice operator selects a range of elements.\n",
"print t[1:3] ('b', 'c')\n",
"# But if you try to modify one of the elements of the tuple, you get an error:\n",
"t[0] = 'A'# You can\u2019t modify the elements of a tuple, but you can replace one tuple with another:\n",
"t = ('A',) + t[1:]\n",
"print t ('A', 'b', 'c', 'd', 'e')\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# ------------------------------------------------------\n",
"# Set exercise\n",
"# 1. Create a new empty and name it set1\n",
"# 2. Add a single member to the set\n",
"# 3. Add two more members to the set\n",
"# 4. Copy set1 to set2\n",
"# 5. Add three more elements to set2\n",
"# 6. Create and print the following sets\n",
"# 7. set3 = intersection of set2 and set1\n",
"# 8. set4 = union of set2 and set1\n",
"# 9. set5 = set difference of set2 and set1\n",
"# 10. set6 = symmetric difference of set2 and set1\n",
"# 11. set7 = shallow copy of set1\n",
"# 12. Test if set2 is superset of set1\n",
"# 13. Test if set1 is subset of set2\n",
"set1 = set()\n",
"set1.add(4)\n",
"print set1\n",
"\n",
"set1.update([1,2])\n",
"print set1\n",
"\n",
"set2 = set1\n",
"print set2\n",
"\n",
"set2.update([3,5,6])\n",
"print set2\n",
"\n",
"set3 = set1 & set2\n",
"print set3\n",
"\n",
"set4 = set1 | set2\n",
"print set4\n",
"\n",
"set5 = set1 - set2\n",
"print set5\n",
"\n",
"set6 = set1 ^ set2\n",
"print set6\n",
"\n",
"set7 = set1.copy()\n",
"print set7\n",
"\n",
"result = set2 >= set1\n",
"print result\n",
"\n",
"result = set1 <= set2\n",
"print result "
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# ------------------------------------------------------\n",
"# Dictionary exercise\n",
"# 1. Create an empty dictionary and name it d1\n",
"# 2. Populate it with the key value pairs: potato: 25 and tomato:45# 3. Create a list (mylist) of 2 tuples: (\u2018apple\u2019, 15), (\u2018orange\u2019, 45)\n",
"# 4. Create a dictionary d2 from that tuple (mytuple)\n",
"# 5. Create two lists called produce and quantity\n",
"# produce = [\"banana \", \"lime\"]\n",
"# quantity = [23,46]\n",
"# 6. Zip produce and quantity into a list named pq\n",
"# 7. Create a dictionary called d3 from pq\n",
"# 8. Assign the value 333 to the key \u201cbanana\u201d in d3\n",
"# 9. Test the membership of a key in a dictionary; specifically, \n",
"# test whether \u201ctomato\u201d exists in d2 and then \n",
"# test whether \u201ctomato\u201d exists in d1\n",
"# 12. Delete (or remove) a key in a dictionary; specifically, remove \u201ctomato\u201d from d1 \n",
"# 13. Obtain the length (or size) of a dictionary; in this case, that of d3\n",
"# 14. Perform an emptiness test on d1\n",
"# 15. Increment the value associated with the key \u201clime\u201d by 210 in d3.\n",
"d1 = {}\n",
"d1[\"tomato\"] = 25\n",
"d1[\"potato\"] = 30\n",
"print d1\n",
"\n",
"mylist = ('apple', 15), ('orange', 45) # is this really a list, regardless of what I called it? \n",
"print mylist\n",
"\n",
"d2 = dict(mylist)\n",
"print d2\n",
"\n",
"produce = [\"banana\", \"lime\"]\n",
"quantity = [23, 46]\n",
"pq = zip(produce, quantity)\n",
"type(pq)\n",
"type(pq[0])\n",
"print pq\n",
"\n",
"d3 = dict(pq)\n",
"print d3\n",
"\n",
"d3[\"banana\"] = 333\n",
"print d3\n",
"\n",
"result = \"tomato\" in d2\n",
"print result\n",
"\n",
"result = \"tomato\" in d1\n",
"print result\n",
"\n",
"del d1[\"tomato\"]\n",
"print d1\n",
"\n",
"len(d3)\n",
"print any(d1) # what happened here\n",
"\n",
"d3[\"lime\"] = d3[\"lime\"] + 210\n",
"print d3"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# ------------------------------------------------------\n",
"# Generate 10 random integers into a list and sort them. \n",
"# Print the list. The numbers should be drawn from a uniform # distribution between 0 and 20.\n",
"import random\n",
"mylist = random.sample(range(0,20), 10)\n",
"mylist.sort()\n",
"print mylist\n",
"# Generate a list of 10 numbers starting with 0 in steps of 3.\n",
"# Print the list. Print the sum of those numbers.\n",
"mylist = range(0,3*10,3)\n",
"print mylist\n",
"sum(mylist)\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# ------------------------------------------------------\n",
"# Built-in user input exercise\n",
"a = input(\"Type something ... (input a number, say 4) \")\n",
"print \"You typed ... \", a\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Built-in user input exercise\n",
"a = input(\"Type something ... (input an expression, say 7+5)\")\n",
"print \"You typed ... \", a"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Built-in user input exercise\n",
"a = input(\"Type something ... (input your name in quotes, like 'Shivraj')\")\n",
"print \"You typed ... \", a"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Built-in user input exercise\n",
"a = input(\"Type something ... (input a text expression like your full name, 'Shivraj' + ' ' + 'Kanungo') \")\n",
"print \"You typed ... \", a"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment