Hour 8: Using Conditional Operators

Example 8.1, Page No.122

In [10]:
import sys
ch=' '
int_num=0
flt_num=0.0
dbl_num=0.0
print "The size of char is:",sys.getsizeof(chr),".byte"
print "The size of ch is:",sys.getsizeof(ch),".byte"
print "The size of int is:",sys.getsizeof(int),".byte"
print "The size of int_num is:",sys.getsizeof(int_num),".byte"
print "The size of float is:",sys.getsizeof(float),".byte"
print "The size of flt_num is:",sys.getsizeof(flt_num),".byte"
#As python has no double data type, I have used only float data type here.
The size of char is: 36 .byte
The size of ch is: 22 .byte
The size of int is: 436 .byte
The size of int_num is: 12 .byte
The size of float is: 436 .byte
The size of flt_num is: 16 .byte

Example 8.2, Page No.125

In [17]:
num=0
print "The AND operator yields:",(num%2==0 and num%3==0)
num=2
print "The AND operator yields:",(num%2==0 and num%3==0)
num=3
print "The AND operator yields:",(num%2==0 and num%3==0)
num=6
print "The AND operator yields:",(num%2==0 and num%3==0)
The AND operator yields: True
The AND operator yields: False
The AND operator yields: False
The AND operator yields: True

Example 8.3, Page No.127

In [8]:
#It is not possible to use for-loop in Python the way it is given in textbook. Hence, the use of while loop
num=1
print "Enter a single digit that can be divided \nby both 2 and 3:"
for num in range(2,7):
    if(num%2!=0 or num%3!=0):
        print num
print num
print "You got such a number:",num
Enter a single digit that can be divided 
by both 2 and 3:
2
3
4
5
6
You got such a number: 6

Example 8.4, Page No.128

In [6]:
num=7
print "!(n<7) yields:",(not(num<7))
print "!(n>7) yields:",(not(num>7))
print "!(n==7) yields:",(not(num==7))
!(n<7) yields: True
!(n>7) yields: True
!(n==7) yields: False

Example 8.5, Page No.132

In [23]:
x=4321
y=5678
print "Given x=",x,",i.e.,0X{:04X}".format(x)
print "      y=",y,",i.e.,0X{:04X}".format(y)
z=x&y
print "X & y returns:{:6d}".format(z),",i.e.,0X{:04X}".format(z)
z=x|y
print "X | y returns:{:6d}".format(z),",i.e.,0X{:04X}".format(z)
z=x^y
print "X ^ y returns:{:6d}".format(z),",i.e.,0X{:04X}".format(z)
print " ~X  returns:{:6d}".format(~x),",i.e.,0X{:04X}".format(~x)
Given x= 4321 ,i.e.,0X10E1
      y= 5678 ,i.e.,0X162E
X & y returns:  4128 ,i.e.,0X1020
X | y returns:  5871 ,i.e.,0X16EF
X ^ y returns:  1743 ,i.e.,0X06CF
 ~X  returns: -4322 ,i.e.,0X-10E2

Example 8.6, Page No.134

In [29]:
x=255
y=5
print "Given x={:4d}".format(x),",i.e.,0X{:04X}".format(x)
print "      y={:4d}".format(y),",i.e.,0X{:04X}".format(y)
z=x>>y
print "x >> y yields:{:6d}".format(z),",i.e.,0X{:04X}".format(z)
z=x<<y
print "x << y yields:{:6d}".format(z),",i.e.,0X{:04X}".format(z)
Given x= 255 ,i.e.,0X00FF
      y=   5 ,i.e.,0X0005
x >> y yields:     7 ,i.e.,0X0007
x << y yields:  8160 ,i.e.,0X1FE0

Example 8.7, Page No.135

In [1]:
import sys
x=sys.getsizeof(int)
if x==2:
    print "The int data type has 2 bytes."
else:
    print "int doesn’t have 2 bytes."
print "The maximum value of int is: "
if x!=2:
    print ~(1 << x * 8 - 1)
else:
    print ~(1 << 15)
int doesn’t have 2 bytes.
The maximum value of int is: 
-491580764103542290389344003363488311062969810712349922950599774733041575563632045350838283083871987455171335931407342682680717201183371703685174955816593532050173778613198750299409984983114585069503870574316115011832273610552315180593120587012451297348595879648367038580742589916924929458075098309119941596150264781422412791167044744644512096092893664088575496131199113239879791394700259830388709521310004694953161827594685985046611263824408277271440340445151941401762175596309439311534804991519681288440882037066695104175933148121875339324910503310089608103880047567773082849318342395952108111196228471393174721112896352782400967939746829014435634293038974558987792428521448420075385030683955244112860897403710788795703260001867255101895634186646659237618203276865199656402546057935845979230559646064581353308805843310002383509210463719836898912777400961330646465730432059223747226730680060410613651809943713333186362580641534580104154278272333152706344741470433854227018577102016581210085064981107523450403116588706337588967284774946664250005782529