Created
September 3, 2014 17:09
-
-
Save kanungo/3a369fc0431052c5c9c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 3\n", | |
"for i in range(5):\n", | |
" print i\n", | |
" print i**2\n", | |
" print i*i*i" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 4\n", | |
"n = input(\"How many numbers do you have? \")\n", | |
"summ = 0.0\n", | |
"for i in range(n):\n", | |
" x = input(\"Enter a number >> \")\n", | |
" summ = summ + x\n", | |
"print(\"\\nThe average of the numbers is\", summ / n)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 5\n", | |
"for x in range(0, 4, 2):\n", | |
" print \"This is index %d\" % (x)\n", | |
"for x in range(7, 0, -3):\n", | |
" print \"This is index %d\" % (x)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 6\n", | |
"for x in xrange(0, 5):\n", | |
" for y in xrange(0, 5):\n", | |
" print '%d * %d = %d' % (x, y, x*y)\n", | |
"\n", | |
"for x in xrange(300000):\n", | |
" print x\n", | |
" if x == 11:\n", | |
" break" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 7\n", | |
"# ****** Do this in spyder ******\n", | |
"# Create a text file iand save it in the same folder where your prgrams are\n", | |
"# The text file containss three lines and is called myfile.txt\n", | |
"# Hello\n", | |
"# Hi\n", | |
"# OK\n", | |
"\n", | |
"f = open(\u201cmyfile.txt\")\n", | |
"for line in f: \n", | |
" print \">\", line\n", | |
"\n", | |
"mystring = \"Hello there\"\n", | |
"for x in mystring:\n", | |
" print x\n", | |
"\n", | |
"# iterating through a list\n", | |
"mycollection = ['Hello', True, 5, 5.0, (2,3)]\n", | |
"for x in mycollection:\n", | |
" print x\n", | |
" \n", | |
"# iterating through a dictionary\n", | |
"d = { 'a': 1, 'b': 2, 'c': 3, }\n", | |
"for v in d:\n", | |
" print v\n", | |
"# Note the strange order!" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 8\n", | |
"i = 0\n", | |
"while i <= 10:\n", | |
" print(i)\n", | |
" i = i + 1\n", | |
"\n", | |
"for i in\n", | |
"range(11):\n", | |
" print(i)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 9\n", | |
"summ = 0.0\n", | |
"count = 0\n", | |
"x = int(raw_input(\"Enter an integer (negative to quit) >> \"))\n", | |
"while x >= 0:\n", | |
" summ = summ + x\n", | |
" count = count + 1\n", | |
" x = int(raw_input(\"Enter an integer (negative to quit) >> \"))\n", | |
"print(\"\\nThe average of the numbers is\", summ / count)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 10\n", | |
"# ****** Do this in spyder ******\n", | |
"# Create file with one number on each line. \n", | |
"fileName = raw_input(\"What file are the numbers in? \")\n", | |
"infile = open(fileName,'r')\n", | |
"sum = 0.0\n", | |
"count = 0\n", | |
"for line in infile.readlines():\n", | |
" sum = sum + float(line)\n", | |
" count = count + 1\n", | |
"print(\"\\nThe average of the numbers is\", sum / count)\n", | |
"close(infile)\n", | |
"\n", | |
"# ****** Do this in spyder ******\n", | |
"# Create file with one number on each line. The last line should be blank.\n", | |
"# That is the sentinak character. The while statement checks for that\n", | |
"fileName = raw_input(\"What file are the numbers in? \")\n", | |
"infile = open(fileName,'r')\n", | |
"sum = 0.0\n", | |
"count = 0\n", | |
"line = infile.readlines()\n", | |
"while line != \"\": # This is the check for the sentinel character\n", | |
" sum = sum + float(line)\n", | |
" count = count + 1\n", | |
" line = infile.readlines()\n", | |
"print(\"\\nThe average of the numbers is\", sum / count)\n", | |
"close(infile)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 11\n", | |
"# Create file with one number on each line. The last line should be blank.\n", | |
"# That is the sentinak character. The if statement checks for that\n", | |
"fileName = raw_input(\"What file are the numbers in? \")\n", | |
"infile = open(fileName,'r')\n", | |
"sum = 0.0\n", | |
"count = 0\n", | |
"line = infile.readline()\n", | |
"while line != \"\":\n", | |
" for xStr in line.split(\",\"):\n", | |
" sum = sum + float(xStr)\n", | |
" count = count + 1\n", | |
" line = infile.readline()\n", | |
"print(\"\\nThe average of the numbers is\", sum / count)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 12\n", | |
"# First Example\n", | |
"for letter in 'Python': \n", | |
"if letter == 'h':\n", | |
" break\n", | |
" print 'Current Letter :', letter\n", | |
"\n", | |
"# Second Example\n", | |
"var = 10 \n", | |
"while var > 0: \n", | |
" print 'Current variable value :', var\n", | |
" var = var -1\n", | |
" if var == 5:\n", | |
" break\n", | |
"print \"Good bye!\"\n", | |
"mycollection = ['Hello', True, 5, 5.0, (2,3)]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 13\n", | |
"# First Example\n", | |
"for letter in 'Python':\n", | |
" if letter == 'h':\n", | |
" continue\n", | |
" print 'Current Letter :', letter\n", | |
"\n", | |
"# Second Example\n", | |
"var = 10\n", | |
"while var > 0: \n", | |
" var = var -1\n", | |
" if var == 5:\n", | |
" continue\n", | |
" print 'Current variable value :', var\n", | |
"print \"Good bye!\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 15\n", | |
"mylist = [1,2,4,3,5,6]\n", | |
"for anelement in mylist:\n", | |
" if anelement % 2 == 0:\n", | |
" print \"list contains an even number\"\n", | |
" break\n", | |
"else:\n", | |
" print \"list does not contain an even number\" " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment