Skip to content

Instantly share code, notes, and snippets.

@kanungo
Created September 3, 2014 17:14
Show Gist options
  • Save kanungo/247db31ea24003e24b90 to your computer and use it in GitHub Desktop.
Save kanungo/247db31ea24003e24b90 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 5\n",
"# Execute this cell first to place these two functions into the namespace\n",
"# You can now run the next three cells\n",
"def names():\n",
" name = raw_input(\"Please enter your name: \")\n",
" print \"Hello \", name\n",
" \n",
"def myadd(firstarg, secondarg):\n",
" try:\n",
" mysum = firstarg + secondarg\n",
" print 'The sum is ', mysum\n",
" except:\n",
" print 'Bad input'"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"names()"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"myadd(5,6)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"myadd(5, \"hi\")"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Slide 10\n",
"# ****** do this in spyder *********\n",
"# Create a NEW file and save the following in myfirstmods.py\n",
"def myadd(a,b):\n",
" try:\n",
" return a+b\n",
" except:\n",
" return 0\n",
" \n",
"def mysub(a,b):\n",
" try:\n",
" return a-b\n",
" except:\n",
" return 0\n",
" \n",
"# Now create ANOTHER file and save the following in mycaller.py in the same folder as the previous file\n",
"# When you run this file, you import the two functions that you created previously into your namespace\n",
"# and then call them\n",
"import myfirstmod\n",
"a = 4\n",
"b = 16\n",
"\n",
"c = myfirstmod.myadd(a,b)\n",
"print 'I called the add function --> ', c\n",
"\n",
"print 'I called the subtract function --> ', myfirstmod.mysub(a,b) "
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment