Skip to content

Instantly share code, notes, and snippets.

@BurcakAsal
Created November 15, 2015 06:25
Show Gist options
  • Save BurcakAsal/60b38235f97975d64c56 to your computer and use it in GitHub Desktop.
Save BurcakAsal/60b38235f97975d64c56 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#Basic example for using python debugger, you may also use \"pythontutor.com\" for online debugging\n",
"#Do not use just (c)ontinue, use also (s)tep, (n)ext, (l)ist or (p)rint etc... to benefit from all features of the debugger\n",
"import pdb\n",
"\n",
"def exam_func(number):\n",
" \n",
" number = number +1\n",
" \n",
" number = 2*number+3\n",
" \n",
" return (number)\n",
"\n",
" \n",
"\n",
"a=5+6\n",
"pdb.set_trace() # breakpoint\n",
"b=7+8\n",
" \n",
"exam_func(5) \n",
"\n",
"print(\"Hello!\") \n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#A basic exception usage, try to remove 'try' and 'except' statements and run again, try to see what happens different\n",
"try:\n",
" a=5\n",
" b=0\n",
" c=a/b\n",
"except:\n",
" print(\"Division error\") \n",
"\n",
"print ('Hello')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Another basic exception with different kind of error, \n",
"#Try to remove 'try' and 'except' statements and run again, try to see what happens different\n",
"try:\n",
" new_list = [1,2,3]\n",
" c = new_list[5]\n",
"except:\n",
" print('Out of index error')\n",
"number = 3+4\n",
"print(number)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Mutiple subclass exceptions, run and think about the results, then swap 'Exception' and 'ZeroDivisionError' and see new result\n",
"try:\n",
" a=5\n",
" b=0\n",
" c=a/b\n",
"except Exception:\n",
" print(\"An error\") \n",
"except ZeroDivisionError: \n",
" print(\"Division error\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#----------------------------------------\n",
"#Declaring your own exception\n",
"class Interval_Exception(Exception):\n",
" pass\n",
"#-----------------------------------------\n",
"# Giving your own exception using \"raise\" within a function\n",
"def example_func(number):\n",
" if(number>5 and number<10):\n",
" raise Interval_Exception\n",
" print(number) \n",
"\n",
"\n",
"try:\n",
" example_func(8) # try also other values such as out of (5,10) interval\n",
"except Interval_Exception:\n",
" print(\"Error about interval!\")\n",
"#-----------------------------------\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#Example for \"assert\" statement\n",
"def exam_func(number):\n",
" return (2*number+3)\n",
" \n",
"def test_func():\n",
" assert (exam_func(3)==7), \"Equation Error!\" #try different values for testing: /3->7,3->9/5->8,5->13/ etc...\n",
" print(\"Test is succesful\") \n",
"\n",
"try: \n",
" test_func() \n",
"except:\n",
" print(\"There is an equation error!\") \n",
" \n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment