"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Enumerated data type for 12 months\n",
"\n",
"import sys\n",
"\n",
"#There is no enumerated data type in python and an alternate is to use class\n",
"#Class declaration\n",
"class month:\n",
" Jan = 0\n",
" Feb = 1\n",
" Mar = 2\n",
" Apr = 3\n",
" May = 4\n",
" June = 5\n",
" July = 6\n",
" Aug = 7\n",
" Sep = 8\n",
" Oct = 9\n",
" Nov = 10\n",
" Dec = 11\n",
" \n",
"#Class variable declaration\n",
"c = month()\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nJan = %d\"%(c.Jan))\n",
"sys.stdout.write(\"\\nFeb = %d\"%(c.Feb))\n",
"sys.stdout.write(\"\\nJune = %d\"%(c.June))\n",
"sys.stdout.write(\"\\nDec = %d\"%(c.Dec))\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Jan = 0\n",
"Feb = 1\n",
"June = 5\n",
"Dec = 11"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 13.24, Page number: 434
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Enumerated data type \n",
"\n",
"import sys\n",
"\n",
"#There is no enumerated data type in python and an alternate is to use class\n",
"#Class declaration\n",
"class month:\n",
" Jan = 1\n",
" Feb = 2\n",
" Mar = 3\n",
" Apr = 4\n",
" May = 5\n",
" June = 6\n",
" July = 7\n",
" Aug = 8\n",
" Sep = 9\n",
" Oct = 10\n",
" Nov = 11\n",
" Dec = 12\n",
" \n",
"#Class variable declaration\n",
"c = month()\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nJan = %d\"%(c.Jan))\n",
"sys.stdout.write(\"\\nFeb = %d\"%(c.Feb))\n",
"sys.stdout.write(\"\\nJune = %d\"%(c.June))\n",
"sys.stdout.write(\"\\nDec = %d\"%(c.Dec))\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Jan = 1\n",
"Feb = 2\n",
"June = 6\n",
"Dec = 12"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 13.25, Page number: 434
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display name of month using enumerated data type\n",
"\n",
"import sys\n",
"\n",
"#There is no enumerated data type in python and an alternate is to use class\n",
"#Class declaration\n",
"class month:\n",
" Jan = 1\n",
" Feb = 2\n",
" Mar = 3\n",
" Apr = 4\n",
" May = 5\n",
" June = 6\n",
" July = 7\n",
" Aug = 8\n",
" Sep = 9\n",
" Oct = 10\n",
" Nov = 11\n",
" Dec = 12\n",
" \n",
"#Class variable declaration\n",
"c = month()\n",
"\n",
"#Result\n",
"for f in range(c.Jan,c.Dec+1):\n",
" #There is no switch case statement in python\n",
" if f == c.Jan:\n",
" sys.stdout.write(\"\\nJanuary\")\n",
" else:\n",
" if f == c.Feb:\n",
" sys.stdout.write(\"\\nFebruary\")\n",
" else:\n",
" if f == c.Mar:\n",
" sys.stdout.write(\"\\nMarch\")\n",
" else:\n",
" if f == c.Apr:\n",
" sys.stdout.write(\"\\nApril\")\n",
" else:\n",
" if f == c.May:\n",
" sys.stdout.write(\"\\nMay\")\n",
" else:\n",
" if f == c.June:\n",
" sys.stdout.write(\"\\nJune\")\n",
" else:\n",
" if f == c.July:\n",
" sys.stdout.write(\"\\nJuly\")\n",
" else:\n",
" if f == c.Aug:\n",
" sys.stdout.write(\"\\nAugust\")\n",
" else:\n",
" if f == c.Sep:\n",
" sys.stdout.write(\"\\nSeptember\")\n",
" else:\n",
" if f == c.Oct:\n",
" sys.stdout.write(\"\\nOctober\")\n",
" else:\n",
" if f == c.Nov:\n",
" sys.stdout.write(\"\\nNovember\")\n",
" else:\n",
" sys.stdout.write(\"\\nDecember\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"January\n",
"February\n",
"March\n",
"April\n",
"May\n",
"June\n",
"July\n",
"August\n",
"September\n",
"October\n",
"November\n",
"December"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 13.26, Page number: 436
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Enumerated data type\n",
"\n",
"import sys\n",
"\n",
"#There is no enumerated data type in python. class is used instead\n",
"class capital:\n",
" Mumbai = 0\n",
" Hyderabad = 1\n",
" Bangalore = 2\n",
"\n",
"class state:\n",
" name = ''\n",
" c = capital()\n",
" \n",
"#Class variable declaration\n",
"s = state()\n",
"c1 = capital()\n",
"\n",
"#Class variable initialization\n",
"s.name = \"Andhra Pradesh\"\n",
"s.c = s.c.Hyderabad\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nState : %s\"%(s.name))\n",
"sys.stdout.write(\"\\nCapital : %d\"%(s.c))\n",
"\n",
"if s.c == c1.Hyderabad:\n",
" sys.stdout.write(\"\\nHyderabad is Capital of %s\"%(s.name))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"State : Andhra Pradesh\n",
"Capital : 1\n",
"Hyderabad is Capital of Andhra Pradesh"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 13.27, Page number: 437
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Identify the type of entered character using enumerated data type.\n",
"\n",
"import sys\n",
"\n",
"#Class declaration instead of enumerated data type\n",
"class ctype:\n",
" Letter = 0\n",
" Digit = 1\n",
" Other = 2\n",
" \n",
"#Variable Initialization\n",
"ch = raw_input(\"Enter any character\")\n",
"c = ctype()\n",
"f = ch.isalpha()\n",
"\n",
"#Result\n",
"if f != 0:\n",
" sys.stdout.write(\"\\n%c is %d type of symbol\"%(ch,c.Letter))\n",
"else:\n",
" f = ch.isdigit()\n",
" if f != 0:\n",
" sys.stdout.write(\"\\n%c is %d type of symbol\"%(ch,c.Digit))\n",
" else:\n",
" sys.stdout.write(\"\\n%c is %d type of symbol\"%(ch,c.Other))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter any character=\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"= is 2 type of symbol"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 13.28, Page number: 438
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Size of union and number of bytes reserved for it\n",
"\n",
"import sys\n",
"\n",
"#There is no union/structure in python. class is used instead\n",
"#Class declaration\n",
"class result:\n",
" marks = 0\n",
" grade = ''\n",
" \n",
"class res:\n",
" name = ''\n",
" age = 0\n",
" perf = result()\n",
" \n",
"#Class variable declaration\n",
"data = res()\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Size of Union : %d\\n\"%(sys.getsizeof(data.perf)))\n",
"sys.stdout.write(\"Size of Structure : %d\\n\"%(sys.getsizeof(data)))\n",
"\n",
"#in python, value tagged method is used for data storage and can represent large numbers"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Size of Union : 36\n",
"Size of Structure : 36\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 13.29, Page number: 440
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Memory size of the computer\n",
"\n",
"import psutil\n",
"\n",
"psutil.phymem_usage()\n",
"\n",
"#Displays current status of the memory\n",
"#different systems will have different memory status"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 4,
"text": [
"usage(total=1600512000L, used=1496383488L, free=104128512L, percent=93.5)"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"