Chapter 4- Data types, Operators and Expressions

Example-show1.cpp, Page no-111

In [1]:
a=int
b=int
c=100
distance=float
a=c
b=c+100
distance=55.9
print "a =", a
print "b =", b
print "c =", c
print "distance =", distance
a = 100
b = 200
c = 100
distance = 55.9

Example-ascii.cpp, Page no-112

In [2]:
code=int(raw_input("Enter an ASCII code(0-127): "))
symbol=code
print "The symbol corresponding to %d is %c" %(code, symbol)
Enter an ASCII code(0-127): 65
The symbol corresponding to 65 is A

Example-temper.cpp, Page no-115

In [3]:
c=float(raw_input("Enter temperature in Celsius: "))
f=1.8*c+32
print "Equivalent fahrenheit = ", f
f=float(raw_input("Enter temperature in fahrenheit: "))
c=(f-32)/1.8
print "Equivalent Celsius = ", c
Enter temperature in Celsius: 5
Equivalent fahrenheit =  41.0
Enter temperature in fahrenheit: 40
Equivalent Celsius =  4.44444444444

Example-size.cpp, Page no-116

In [1]:
from ctypes import c_char, c_short, c_int, c_long, c_float, c_double, c_longdouble, sizeof
print "sizeof( char ) =", sizeof(c_char)
print "sizeof( short ) =", sizeof(c_short)
print "sizeof( short int ) =", sizeof(c_short)
print "sizeof( int ) =", sizeof(c_int)
print "sizeof( long ) =", sizeof(c_long)
print "sizeof( long int ) =", sizeof(c_long)
print "sizeof( float ) =", sizeof(c_float)
print "sizeof( double ) =", sizeof(c_double)
print "sizeof( long double ) =", sizeof(c_longdouble)
sizeof( char ) = 1
sizeof( short ) = 2
sizeof( short int ) = 2
sizeof( int ) = 4
sizeof( long ) = 4
sizeof( long int ) = 4
sizeof( float ) = 4
sizeof( double ) = 8
sizeof( long double ) = 8

Example-modules.cpp, Page no-120

In [5]:
numerator=int(raw_input("Enter numerator: "))
denominator=int(raw_input("Enter denominator: "))
result=numerator/denominator
remainder=numerator%denominator
print numerator, "/", denominator, "=", result
print numerator, "%", denominator, "=", remainder
Enter numerator: 12
Enter denominator: 5
12 / 5 = 2
12 % 5 = 2

Example-notemp.cpp, Page no-121

In [2]:
a, b=[int(x) for x in raw_input("Enter two integers <a, b>: ").split()] #taking input in single line sperated by white space
a=a+b
b=a-b
a=a-b
print "Value of a and b on swapping in main():", a, b
Enter two integers <a, b>: 10 20
Value of a and b on swapping in main(): 20 10

Example-relation.cpp, Page no-122

In [3]:
my_age=int(raw_input("Enter my age: "))
your_age=int(raw_input("Enter your age: "))
if(my_age==your_age):
    print "We are born in the same year."
else:
    print "We are born in different years"
Enter my age: 25
Enter your age: 25
We are born in the same year.

Example-char1.cpp, Page no-123

In [1]:
from ctypes import c_byte
c=c_byte(255) #signed char
d=c_byte(-1) #signed char
if c.value<0:
    print 'c is less than 0'
else:
    print 'c is not less than 0'
if d.value<0:
    print 'd is less than 0'
else:
    print 'd is not less than 0'
if c.value==d.value:
    print 'c and d are equal'
else:
    print 'c and d are not equal'
c is less than 0
d is less than 0
c and d are equal

Example-char2.cpp, Page no-124

In [1]:
from ctypes import c_ubyte, c_byte
c=c_ubyte(255) #unsigned char
d=c_byte(-1) #signed char
if c.value<0:
    print 'c is less than 0'
else:
    print 'c is not less than 0'
if d.value<0:
    print 'd is less than 0'
else:
    print 'd is not less than 0'
if c.value==d.value:
    print 'c and d are equal'
else:
    print 'c and d are not equal'
c is not less than 0
d is less than 0
c and d are not equal

Example-leap.cpp, Page no-126

In [2]:
year=int(raw_input("Enter any year: "))
if( (year%4==0 and year%100!=0) or (year%400==0)):
    print year, "is a leap year"
else:
    print year, "is not a leap year"
Enter any year: 1996
1996 is a leap year

Example-large.cpp, Page no-127

In [1]:
from ctypes import c_uint
u=c_uint(0) #unsigned integer
print 'Value before conversion:', u.value
u.value=~int(u.value) # 1's complement
print 'Value after conversion:', u.value
Value before conversion: 0
Value after conversion: 4294967295

Example-extract.cpp, Page no-130

In [1]:
a=int(raw_input("Enter an integer: "))
n=int(raw_input("Enter bit position to extract: "))
bit=(a>>(n-1))&1  #shift operator
print "The bit is ", bit
Enter an integer: 10
Enter bit position to extract: 2
The bit is  1

Example-max.cpp, Page no-133

In [3]:
a, b=[int(x) for x in raw_input("Enter two integers: ").split()]
larger=a if a>b else b #?: operator
print "The larger of the two is", larger
Enter two integers: 10 20
The larger of the two is 20

Example-oddeven.cpp, Page no-133

In [4]:
num=int(raw_input("Enter the number: "))
print "The number", num,"is",
print "Even" if num%2==0 else "Odd" #?: operator
num=int(raw_input("Enter the number: "))
print "The number", num,"is",
print "Even" if num%2==0 else "Odd"
Enter the number: 10
The number 10 is Even
Enter the number: 25
The number 25 is Odd

Example-coerce.cpp, Page no-136

In [1]:
f=float
i=12
j=5
print "when i = ", i, "j = ", j
f=i/j
print "i/j = ", f
f=float(i)/float(j)
print "(float)i/j = ", f
f=float(i)/j
print "float(i)/j = ", f
f=i/float(j)
print "i/float(j) = ", f
when i =  12 j =  5
i/j =  2
(float)i/j =  2.4
float(i)/j =  2.4
i/float(j) =  2.4

Example-city.cpp, Page no-141

In [1]:
CITY='Bidar'
def which_city():
    print 'City in Function:',
    print CITY
print 'Earlier City:',
print CITY
CITY='Bangalore'
print 'New City:',
print CITY
which_city()
Earlier City: Bidar
New City: Bangalore
City in Function: Bangalore

Example-color1.cpp, Page no-143

In [1]:
def PrintColor(c):
    (red, blue, green)=(0, 1, 2) #enum
    type =['red', 'green', 'blue']
    if c==red:
        color='red'
    elif c==blue:
        color='blue'
    else:
        color ='green'
    print 'Your color choice as per color2.cpp module:', color
(red, green, blue)=(0, 1, 2) #enum
type =['red', 'green', 'blue']
print 'Your color choice in color1.cpp module: green'
PrintColor(green)
Your color choice in color1.cpp module: green
Your color choice as per color2.cpp module: blue

Example-maxmacro.cpp, Page no-146

In [1]:
def Max(a, b):
    return a if a>b else b
print 'max(2, 3) =', Max(2, 3)
print 'max(10.2, 4.5) =', Max(10.2, 4.5)
i=5
j=10
print 'i =',i
print 'j =', j
print 'On execution of k=max(++i, ++j);...'
i+=1
j+=1
k=Max(i+1, j+1)
print 'i =', i
print 'j =', j #the operand is j+1 and not ++j and thus the change is not reflected back in j. 
print 'k =', k #operand of type j+=1 is not allowed
max(2, 3) = 3
max(10.2, 4.5) = 10.2
i = 5
j = 10
On execution of k=max(++i, ++j);...
i = 6
j = 11
k = 12

Example-exp.cpp, Page no-148

In [4]:
a=30
b=20
c=11
result=a+b/(c-1)+a%b #--c is replaced by (c-1)
print 'a+b/--c+a%b =', result
a+b/--c+a%b = 42

Example Page no-150

In [1]:
num1=int(raw_input("Enter the first number: "))
num2=int(raw_input("Enter the second number: "))
print num1, '+', num2, '=',num1+num2
print num1, '-', num2, '=',num1-num2
print num1, '*', num2, '=',num1*num2
print num1, '/', num2, '=',num1/num2
Enter the first number: 20
Enter the second number: 10
20 + 10 = 30
20 - 10 = 10
20 * 10 = 200
20 / 10 = 2