diff options
Diffstat (limited to 'Teach_Yourself_C_in_24_Hours/hour8.ipynb')
-rwxr-xr-x | Teach_Yourself_C_in_24_Hours/hour8.ipynb | 294 |
1 files changed, 294 insertions, 0 deletions
diff --git a/Teach_Yourself_C_in_24_Hours/hour8.ipynb b/Teach_Yourself_C_in_24_Hours/hour8.ipynb new file mode 100755 index 00000000..9918b3d0 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour8.ipynb @@ -0,0 +1,294 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:77914c92baf9b6155be2979d7adfda60e6d3dc335c9ab543c21e730dcfce4fad"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 8: Using Conditional Operators"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.1, Page No.122"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "ch=' '\n",
+ "int_num=0\n",
+ "flt_num=0.0\n",
+ "dbl_num=0.0\n",
+ "print \"The size of char is:\",sys.getsizeof(chr),\".byte\"\n",
+ "print \"The size of ch is:\",sys.getsizeof(ch),\".byte\"\n",
+ "print \"The size of int is:\",sys.getsizeof(int),\".byte\"\n",
+ "print \"The size of int_num is:\",sys.getsizeof(int_num),\".byte\"\n",
+ "print \"The size of float is:\",sys.getsizeof(float),\".byte\"\n",
+ "print \"The size of flt_num is:\",sys.getsizeof(flt_num),\".byte\"\n",
+ "#As python has no double data type, I have used only float data type here."
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The size of char is: 36 .byte\n",
+ "The size of ch is: 22 .byte\n",
+ "The size of int is: 436 .byte\n",
+ "The size of int_num is: 12 .byte\n",
+ "The size of float is: 436 .byte\n",
+ "The size of flt_num is: 16 .byte\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.2, Page No.125"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "num=0\n",
+ "print \"The AND operator yields:\",(num%2==0 and num%3==0)\n",
+ "num=2\n",
+ "print \"The AND operator yields:\",(num%2==0 and num%3==0)\n",
+ "num=3\n",
+ "print \"The AND operator yields:\",(num%2==0 and num%3==0)\n",
+ "num=6\n",
+ "print \"The AND operator yields:\",(num%2==0 and num%3==0)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The AND operator yields: True\n",
+ "The AND operator yields: False\n",
+ "The AND operator yields: False\n",
+ "The AND operator yields: True\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.3, Page No.127"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#It is not possible to use for-loop in Python the way it is given in textbook. Hence, the use of while loop\n",
+ "num=1\n",
+ "print \"Enter a single digit that can be divided \\nby both 2 and 3:\"\n",
+ "for num in range(2,7):\n",
+ " if(num%2!=0 or num%3!=0):\n",
+ " print num\n",
+ "print num\n",
+ "print \"You got such a number:\",num"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a single digit that can be divided \n",
+ "by both 2 and 3:\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "You got such a number: 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.4, Page No.128"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "num=7\n",
+ "print \"!(n<7) yields:\",(not(num<7))\n",
+ "print \"!(n>7) yields:\",(not(num>7))\n",
+ "print \"!(n==7) yields:\",(not(num==7))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "!(n<7) yields: True\n",
+ "!(n>7) yields: True\n",
+ "!(n==7) yields: False\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.5, Page No.132"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x=4321\n",
+ "y=5678\n",
+ "print \"Given x=\",x,\",i.e.,0X{:04X}\".format(x)\n",
+ "print \" y=\",y,\",i.e.,0X{:04X}\".format(y)\n",
+ "z=x&y\n",
+ "print \"X & y returns:{:6d}\".format(z),\",i.e.,0X{:04X}\".format(z)\n",
+ "z=x|y\n",
+ "print \"X | y returns:{:6d}\".format(z),\",i.e.,0X{:04X}\".format(z)\n",
+ "z=x^y\n",
+ "print \"X ^ y returns:{:6d}\".format(z),\",i.e.,0X{:04X}\".format(z)\n",
+ "print \" ~X returns:{:6d}\".format(~x),\",i.e.,0X{:04X}\".format(~x)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Given x= 4321 ,i.e.,0X10E1\n",
+ " y= 5678 ,i.e.,0X162E\n",
+ "X & y returns: 4128 ,i.e.,0X1020\n",
+ "X | y returns: 5871 ,i.e.,0X16EF\n",
+ "X ^ y returns: 1743 ,i.e.,0X06CF\n",
+ " ~X returns: -4322 ,i.e.,0X-10E2\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.6, Page No.134"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x=255\n",
+ "y=5\n",
+ "print \"Given x={:4d}\".format(x),\",i.e.,0X{:04X}\".format(x)\n",
+ "print \" y={:4d}\".format(y),\",i.e.,0X{:04X}\".format(y)\n",
+ "z=x>>y\n",
+ "print \"x >> y yields:{:6d}\".format(z),\",i.e.,0X{:04X}\".format(z)\n",
+ "z=x<<y\n",
+ "print \"x << y yields:{:6d}\".format(z),\",i.e.,0X{:04X}\".format(z)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Given x= 255 ,i.e.,0X00FF\n",
+ " y= 5 ,i.e.,0X0005\n",
+ "x >> y yields: 7 ,i.e.,0X0007\n",
+ "x << y yields: 8160 ,i.e.,0X1FE0\n"
+ ]
+ }
+ ],
+ "prompt_number": 29
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.7, Page No.135"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "x=sys.getsizeof(int)\n",
+ "if x==2:\n",
+ " print \"The int data type has 2 bytes.\"\n",
+ "else:\n",
+ " print \"int doesn\u2019t have 2 bytes.\"\n",
+ "print \"The maximum value of int is: \"\n",
+ "if x!=2:\n",
+ " print ~(1 << x * 8 - 1)\n",
+ "else:\n",
+ " print ~(1 << 15)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "int doesn\u2019t have 2 bytes.\n",
+ "The maximum value of int is: \n",
+ "-491580764103542290389344003363488311062969810712349922950599774733041575563632045350838283083871987455171335931407342682680717201183371703685174955816593532050173778613198750299409984983114585069503870574316115011832273610552315180593120587012451297348595879648367038580742589916924929458075098309119941596150264781422412791167044744644512096092893664088575496131199113239879791394700259830388709521310004694953161827594685985046611263824408277271440340445151941401762175596309439311534804991519681288440882037066695104175933148121875339324910503310089608103880047567773082849318342395952108111196228471393174721112896352782400967939746829014435634293038974558987792428521448420075385030683955244112860897403710788795703260001867255101895634186646659237618203276865199656402546057935845979230559646064581353308805843310002383509210463719836898912777400961330646465730432059223747226730680060410613651809943713333186362580641534580104154278272333152706344741470433854227018577102016581210085064981107523450403116588706337588967284774946664250005782529\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |