Skip to content

Instantly share code, notes, and snippets.

@kanungo
Created September 3, 2014 17:04
Show Gist options
  • Save kanungo/9d22f1196a517c5d6767 to your computer and use it in GitHub Desktop.
Save kanungo/9d22f1196a517c5d6767 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 3\n",
"# 2 == 2 evaluates to True\n",
"2 == 2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 4\n",
"import random\n",
"number = random.randint(0,20)\n",
"guess = int(input('Enter an integer : '))\n",
"if guess == number:\n",
" print('Congratulations, you guessed it.')\n",
"elif guess < number:\n",
" print('No, it is higher than that')\n",
"else:\n",
" print('No, it is lower than that')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 6\n",
"a = 1\n",
"b = 2\n",
"c = 3\n",
"print (a or b or c)\n",
"\n",
"print (a and b and c)\n",
"\n",
"print (b or a and c)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 9 \n",
"# Firstr try this and you get an error because of division by zero\n",
"# T\n",
"(x,y) = (5,0)\n",
"z = x/y"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 9 (i)\n",
"(x,y) = (5,0)\n",
"try:\n",
" z = x/y\n",
"except ZeroDivisionError:\n",
" print \"divide by zero\"\n",
"\n",
"# Slide 9 (ii)\n",
"(x,y) = (5,0)\n",
"try:\n",
" z = x/y\n",
"except ZeroDivisionError, e:\n",
" z = e \n",
" print z\n",
"\n",
"# Slide 9 (iii)\n",
"(x,y) = (5,0)\n",
"try:\n",
" z = x/y\n",
"except Exception, e:\n",
" print str(e)\n",
" print repr(e)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 10\n",
"# quadratic.py\n",
"# A program that computes the real roots of a quadratic equation.\n",
"# Illustrates use of the math library.\n",
"# Note: This program crashes if the equation has no real roots.\n",
"import math\n",
"print \"This program finds the real solutions to a quadratic\"\n",
"print\n",
"a, b, c = input(\"Please enter the coefficients (a, b, c): \")\n",
"discRoot = math.sqrt(b * b - 4 * a * c)\n",
"root1 = (-b + discRoot) / (2 * a)\n",
"root2 = (-b - discRoot) / (2 * a)\n",
"print\n",
"print \"The solutions are:\", root1, root2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 10\n",
"# quadratic2.py\n",
"# A program that computes the real roots of a quadratic equation.\n",
"# Bad version using a simple if to avoid program crash\n",
"import math \n",
"print \"This program finds the real solutions to a quadratic\\n\"\n",
"a, b, c = input(\"Please enter the coefficients (a, b, c): \")\n",
"discrim = b * b - 4 * a * c\n",
"if discrim >= 0:\n",
" discRoot = math.sqrt(discrim)\n",
" root1 = (-b + discRoot) / (2 * a)\n",
" root2 = (-b - discRoot) / (2 * a)\n",
" print \"\\nThe solutions are:\", root1, root2\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 10\n",
"# quadratic3.py\n",
"# A program that computes the real roots of a quadratic equation.\n",
"# Illustrates use of a two-way decision\n",
"import math\n",
"print \"This program finds the real solutions to a quadratic\\n\"\n",
"a, b, c = input(\"Please enter the coefficients (a, b, c): \")\n",
"discrim = b * b - 4 * a * c \n",
"if discrim < 0:\n",
" print \"\\nThe equation has no real roots!\"\n",
"else:\n",
" discRoot = math.sqrt(b * b - 4 * a * c)\n",
" root1 = (-b + discRoot) / (2 * a)\n",
" root2 = (-b - discRoot) / (2 * a)\n",
" print \"\\nThe solutions are:\", root1, root2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 10\n",
"# quadratic4.py\n",
"# A program that computes the real roots of a quadratic equation.\n",
"# Illustrates use of a multi-way decision\n",
"import math\n",
"print \"This program finds the real solutions to a quadratic\\n\"\n",
"a, b, c = input(\"Please enter the coefficients (a, b, c): \")\n",
"discrim = b * b - 4 * a * c\n",
"if discrim < 0:\n",
" print \"\\nThe equation has no real roots!\"\n",
"elif discrim == 0:\n",
" root = -b / (2 * a)\n",
" print \"\\nThere is a double root at\", root\n",
"else:\n",
" discRoot = math.sqrt(b * b - 4 * a * c)\n",
" root1 = (-b + discRoot) / (2 * a)\n",
" root2 = (-b - discRoot) / (2 * a)\n",
" print \"\\nThe solutions are:\", root1, root2\n",
" "
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 10\n",
"# quadratic5.py\n",
"# A program that computes the real roots of a quadratic equation.\n",
"# Illustrates exception handling to avoid crash on bad inputs\n",
"import math\n",
"print \"This program finds the real solutions to a quadratic\\n\"\n",
"try:\n",
" a, b, c = input(\"Please enter the coefficients (a, b, c): \")\n",
" discRoot = math.sqrt(b * b - 4 * a * c)\n",
" root1 = (-b + discRoot) / (2 * a)\n",
" root2 = (-b - discRoot) / (2 * a)\n",
" print \"\\nThe solutions are:\", root1, root2\n",
"except ValueError:\n",
" print \"\\nNo real roots\""
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 10\n",
"# quadratic6.py\n",
"# A program that computes the real roots of a quadratic equation.\n",
"# Illustrates robust exception handling to avoid crash on bad inputs\n",
"import math\n",
"print \"This program finds the real solutions to a quadratic\\n\"\n",
"try:\n",
" a, b, c = input(\"Please enter the coefficients (a, b, c): \")\n",
" discRoot = math.sqrt(b * b - 4 * a * c)\n",
" root1 = (-b + discRoot) / (2 * a)\n",
" root2 = (-b - discRoot) / (2 * a)\n",
" print \"\\nThe solutions are:\", root1, root2\n",
"except ValueError, excObj:\n",
" msg = str(excObj)\n",
" if msg == \"math domain error\":\n",
" print \"\\nNo Real Roots\"\n",
" elif msg == \"unpack tuple of wrong size\":\n",
" print \"\\nYou didn't give me the right number of coefficients.\"\n",
" else:\n",
" print \"\\nSomething went wrong, sorry!\"\n",
"except NameError:\n",
" print \"\\nYou didn't enter three numbers.\"\n",
"except TypeError:\n",
" print \"\\nYour inputs were not all numbers.\"\n",
"except SyntaxError:\n",
" print \"\\nYour input was not in the correct form. Missing comma(s), perhaps?\"\n",
"except:\n",
" print \"\\nSomething went wrong, sorry!\""
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 11\n",
"# ***** Do this in spyder ******\n",
"\n",
"# first.py\n",
"# Save this file in a separate folder\n",
"print \"Hi ... in FIRST\"\n",
"\n",
"if __name__ == \"__main__\":\n",
" print \"Program is running independently\"\n",
"else:\n",
" print \"Program running as an imported module\"\n",
" \n",
"\n",
"# second.py\n",
"# Save this file in the same folder as you saved first.py\n",
"import first\n",
"\n",
"print \"Hi ... in SECOND\"\n",
"\n",
"if __name__ == \"__main__\":\n",
" print \"Program is running independently\"\n",
"else:\n",
" print \"Program running as an imported module\"\n",
"\n",
" \n",
"# Now run first.py\n",
"# Then run second.py"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment