"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Program to demonstrate the use of Conditional operator with two values\n",
"\n",
"#Result\n",
"#in python if..else statement can be written similar to conditional operator\n",
"#prints Result = 4 if 2 == 3, otherwise Result = 5\n",
"\n",
"print \"Result = \",4 if 2==3 else 5"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Result = 5\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.3, Page number: 23
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Program to use Conditional operator with two statements\n",
"\n",
"#Result\n",
"#in python if..else statement can be written similar to conditional operator\n",
"#prints True if 3>2, otherwise prints False\n",
"\n",
"print \"True\" if 3>2 else \"False\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.4, Page number: 25
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Program to use increment operator as prefix\n",
"\n",
"#Variable declaration\n",
"x = 10\n",
"y = 20\n",
"\n",
"#Calculation\n",
"#there is no ++/-- operator in python\n",
"\n",
"z = x * y\n",
"y += 1\n",
"a = x * y\n",
"\n",
"#Result\n",
"print z,\" \",a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"200 210\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.5, Page number: 25
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Program to use increment operator as prefix\n",
"\n",
"#Variable declaration\n",
"x = 10\n",
"y = 20\n",
"\n",
"#Calculation\n",
"#there is no ++/-- operator in python\n",
"\n",
"y += 1\n",
"z = x * y\n",
"a = x * y\n",
"\n",
"#Result\n",
"print z,\" \",a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"210 210\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.6, Page number: 26
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Program to find the size and address of integer and float variables\n",
"\n",
"#import python module\n",
"#sys python module is required for getsizeof() function.\n",
"\n",
"import sys\n",
"\n",
"#Variable declaration\n",
"#No specific float type variable declaration is available in python. If we\n",
"#assign a floating point value to a variable,\n",
"#it will be treated as floating point variable\n",
"x = 2\n",
"y = 2.0\n",
"\n",
"#Result\n",
"#In python, integer variable uses 12 bytes and float variable uses 16 bytes of\n",
"#memory to store the variables\n",
"#sys.getsizeof() function returns the size of variable in bytes which is similar\n",
"#to sizeof() operator\n",
"#id() function returns the address of variables which is similar to '&' operator\n",
"\n",
"print \"Size (x) = \",sys.getsizeof(x)\n",
"print \"Size (y) = \",sys.getsizeof(y)\n",
"print \"Address of x = \",id(x),\"and y = \",id(y)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Size (x) = 12\n",
"Size (y) = 16\n",
"Address of x = 5626076 and y = 54761072\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Program uses conditional operator to determine the value of 'b'depending the inputted value of 'a'\n",
"\n",
"#Reads Input value for a\n",
"\n",
"a = raw_input(\"Enter Any Integer either above 5 or below 5 :-\")\n",
"a = int(a)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#Calculation\n",
"#Assigns b = 3 if a > 5, otherwise b = 4\n",
"b = 3 if a > 5 else 4\n",
"\n",
"#Result\n",
"print \"Calculated Value of b is :- \",b"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Any Integer either above 5 or below 5 :-6\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Calculated Value of b is :- 3\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.9, Page number: 28
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Program to determine the value of 'b' using conditional operator\n",
"\n",
"#Input\n",
"\n",
"a = raw_input(\"Enter Any Integer either above 5 or below 5 :-\")\n",
"a = int(a)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#Calculation\n",
"#Assigns b = 3 if a == 5, otherwise b = 4\n",
"b = 3 if a == 5 else 4\n",
"\n",
"#Result\n",
"print \"Calculated Value of b is :- \",b"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Any Integer either above 5 or below 5 :-3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Calculated Value of b is :- 4\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.10, Page number: 29
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read three variables x, y and z and Using conditional statements, evaluate\n",
"#values of variables a, b and c.\n",
"#Perform the sum with two sets of variables. Check whether the sums are equal or not\n",
"#and print appropriate messages.\n",
"\n",
"\n",
"#Reads three Input values for x,y and z\n",
"\n",
"x = raw_input(\"Enter Value of x \")\n",
"x = int(x)\n",
"\n",
"y = raw_input(\"Enter Value of y \")\n",
"y = int(y)\n",
"\n",
"z = raw_input(\"Enter Value of z \")\n",
"z = int(z)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#Calculation\n",
"#Assigns a = 3 if x >= 5, otherwise a = 4\n",
"a = 3 if x >= 5 else 4\n",
"\n",
"#Result\n",
"print \"Calculated Value of a is :- \",a\n",
"\n",
"#Calculation\n",
"#Assigns b = 10 if y <= 8, otherwise b = 9\n",
"b = 10 if y <= 8 else 9\n",
"\n",
"#Result\n",
"print \"Calculated Value of b is :- \",b\n",
"\n",
"#Calculation\n",
"#Assigns c = 20 if z == 10, otherwise c = 30\n",
"c = 20 if z == 10 else 30\n",
"\n",
"#Result\n",
"print \"Calculated Value of c is :- \",c\n",
"\n",
"#Calculation\n",
"m = x + y + z\n",
"n = a + b + c\n",
"\n",
"#Result\n",
"print \"Addition of x,y,z is \",m,\"(m)\"\n",
"print \"Addition of a,b,c is \",n,\"(n)\"\n",
"print \"m & n NOT EQUAL\" if m != n else \"m & n ARE EQUAL\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Value of x 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Value of y 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Value of z 7\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Calculated Value of a is :- 3\n",
"Calculated Value of b is :- 10\n",
"Calculated Value of c is :- 30\n",
"Addition of x,y,z is 14 (m)\n",
"Addition of a,b,c is 43 (n)\n",
"m & n NOT EQUAL\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Print logic 1 if input character is capital otherwise 0\n",
"\n",
"#Import\n",
"import sys\n",
"\n",
"#Variable initialization\n",
"#raw_input() function is used to read a character from the keyboard in Python 2.7\n",
"#ord() function converts the character into its corresponding ASCII integer\n",
"\n",
"\n",
"#Reads Input value for x\n",
"\n",
"x = raw_input(\"Enter a Character : \")\n",
"x = ord(x)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#Condition\n",
"y = 1 if (x >= 65) and (x <= 90) else 0\n",
"\n",
"#Result\n",
"print \"Y : \",y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Character : A\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Y : 1\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.13, Page number: 31
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display logic 0 if one reads a character through keyboard otherwise logic 1.\n",
"#ASCII values for 0 to 9 are 48 to 57 respectively\n",
"\n",
"#Variable initialization\n",
"#raw_input() function is used to read a character from the keyboard in Python 2.7\n",
"#ord() function converts the character into its corresponding ASCII integer\n",
"\n",
"#Reads Input value for x\n",
"\n",
"x = raw_input(\"Enter The Character or Integer : \")\n",
"x = ord(x)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#Condition\n",
"y = 1 if (x >= 48) and (x <= 57) else 0\n",
"\n",
"#Result\n",
"print \"Value of Y = \",y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter The Character or Integer : A\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Value of Y = 0\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.14, Page number: 32
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display 1 if inputted number is between 1-100 otherwise 0.\n",
"#Use the logical AND operator\n",
"\n",
"#Variable initialization\n",
"#Reads Input value for x\n",
"\n",
"x = raw_input(\"Enter numbers : \")\n",
"x = int(x)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#Condition\n",
"z = 1 if (x >= 1) and (x <= 100) else 0\n",
"\n",
"#Result\n",
"print \"Z = \",z"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter numbers : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Z = 1\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.15, Page number: 32
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display 1 if inputted number is either 1 or 100 otherwise 0.\n",
"#Use the logical OR operator\n",
"\n",
"#Variable initialization\n",
"#Reads Input value for x\n",
"\n",
"x = raw_input(\"Enter numbers : \")\n",
"x = int(x)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#Condition\n",
"z = 1 if (x == 1) or (x == 100) else 0\n",
"\n",
"#Result\n",
"print \"Z = \",z"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter numbers : 1\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Z = 1\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.16, Page number: 33
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display 1 if inputted number is except 100 otherwise 0.\n",
"#Use the logical NOT operator\n",
"\n",
"#Variable initialization\n",
"#Reads Input value for x\n",
"\n",
"x = raw_input(\"Enter number : \")\n",
"x = int(x)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#Condition\n",
"z = 1 if (x != 100) else 0\n",
"\n",
"#Result\n",
"print \"Z : \",z"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter number : 100\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Z : 0\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.17, Page number: 33
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Shift inputted data by two bits right\n",
"\n",
"#Variable initialization\n",
"#Reads Input value for x\n",
"\n",
"x = raw_input(\"Read The Integer from keyboard (x) :- \")\n",
"x = int(x)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#bitwise shifting\n",
"x >>= 2\n",
"y = x\n",
"\n",
"#Result\n",
"print \"The Right shifted data is = \",y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Read The Integer from keyboard (x) :- 8\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Right shifted data is = 2\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.18, Page number: 34
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Shift inputted data by two bits to the left\n",
"\n",
"#Variable initialization\n",
"#Reads Input value for x\n",
"\n",
"x = raw_input(\"Read The Integer from keyboard (x) :- \")\n",
"x = int(x)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#bitwise shifting\n",
"x <<= 3\n",
"y = x\n",
"\n",
"#Result\n",
"print \"The Left shifted data is = \",y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Read The Integer from keyboard (x) :- 2\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Left shifted data is = 16\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.19, Page number: 35
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Use bitwise AND operator between the two integers and display the results\n",
"\n",
"#Variable initialization\n",
"#Reads Input value for x\n",
"\n",
"a = raw_input(\"Read The Integers from keyboard (a & b) :- \")\n",
"a = int(a)\n",
"\n",
"b = raw_input(\"Read The Integers from keyboard (a & b) :- \")\n",
"b = int(b)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#bitwise operation\n",
"c = a & b\n",
"\n",
"#Result\n",
"print \"The Answer after ANDing is (C) = \",c"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Read The Integers from keyboard (a & b) :- 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Read The Integers from keyboard (a & b) :- 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Answer after ANDing is (C) = 0\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.20, Page number: 37
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#operate OR operation on two integers and display the result\n",
"\n",
"#Variable initialization\n",
"#Reads Input value for x\n",
"\n",
"a = raw_input(\"Read The Integers from keyboard (a & b) :- \")\n",
"a = int(a)\n",
"\n",
"b = raw_input(\"Read The Integers from keyboard (a & b) :- \")\n",
"b = int(b)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#bitwise operation\n",
"c = a | b\n",
"\n",
"#Result\n",
"print \"The Oring operation betwen a & b in c = \",c"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Read The Integers from keyboard (a & b) :- 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Read The Integers from keyboard (a & b) :- 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Oring operation betwen a & b in c = 12\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 3.21, Page number: 38
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Exclusive OR operation between the two integers and display the result\n",
"\n",
"#Variable initialization\n",
"#Reads Input value for x\n",
"\n",
"a = raw_input(\"Read The Integers from keyboard (a & b) :- \")\n",
"a = int(a)\n",
"\n",
"b = raw_input(\"Read The Integers from keyboard (a & b) :- \")\n",
"b = int(b)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#bitwise operation\n",
"c = a ^ b\n",
"\n",
"#Result\n",
"print \"The data after Exclusive OR operation is in c = \",c"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Read The Integers from keyboard (a & b) :- 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Read The Integers from keyboard (a & b) :- 2\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The data after Exclusive OR operation is in c = 10\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}