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:
| Variable | Value | Reason | 
|---|---|---|
| a | -0.5 | Remainder of -4 divided by 1.5 is -0.5 | 
| b | -3.0 | Floor division of -4 by 1.5 is -3.0 as floor division divides and truncates the fractional part from the result. | 
| c | 0.8 | Division of 4.0 by 5 is 0.8, which is a floating-point value | 
| d | 0 | Division 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:
| Variables | Datatypes | 
|---|---|
| a | float | 
| b | float | 
| c | float | 
| d | int | 
(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
- What is meant by a floating-point literal in Python ? How many ways can a floating literal be represented into ? 
- What are the two Boolean literals in Python ? 
- What kind of program elements are these: 'a', 4.38925, "a", main( ) ? 
- Aashna has written following code : - a = 5 b = '5' c = "5" d = 5.0- (a) What are the data types of a, b, c, d ? - (b) Are a, b, c, d storing exactly the same values ? Why/why not ? - (c) What will be the output of following code based on values of a, b, c and d ? - print a == b - print b == c - print a == d - (d) Discuss the reasons behind the output of part (c).