KnowledgeBoat Logo
|

Computer Applications

Anant has written following expressions in Python 2.7 :

a = - 4 % 1.5
b = - 4 // 1.5
c = 4.0 / 5
d = 4 / 5

(a) Find out the values stored by a, b, c and d.

(b) What are the datatypes of a, b, c and d ?

(c) Why are the values of c and d not the same.

Python Funda

1 Like

Answer

(a) The values stored in a, b, c, d are as follows:

VariableValueReason
a-0.5Remainder of -4 divided by 1.5 is -0.5
b-3.0Floor division of -4 by 1.5 is -3.0 as floor division divides and truncates the fractional part from the result.
c0.8Division of 4.0 by 5 is 0.8, which is a floating-point value
d0Division of 4 by 5 is 0, and the fractional part is truncated

(b) The data types of a, b, c and d are as follows:

VariablesDatatypes
afloat
bfloat
cfloat
dint

(c) The values of 'c' and 'd' are not the same because of the difference in the division operation performed in each case:

'c' is the result of dividing two floating-point numbers, which preserves the decimal point and gives a floating-point result. 'd' is the result of dividing two integers, which truncates the fractional part and gives an integer result.

Answered By

3 Likes


Related Questions