Home » Python

Find Output of Python programs | Set 2 (Basics)

Here, we have some of the Python basic programs (calculation based), and we have to find their outputs. Each program has correct output along with their explanation.
Submitted by IncludeHelp, on August 14, 2018

Program 1:

a = 10
b = 3

res = a/b 
print "a/b: ", res 

res = float(a/b)
print "float (a/b) : ", res 

res = float (a) /float (b)
print "float (a/b) : ", res 

res = a/b 
print "a/b: ", res

Output

    a/b:  3
    float (a/b) :  3.0
    float (a/b) :  3.33333333333
    a/b:  3

Explanation:

  • The values of a and b are integer type.
  • Result of a/b will be an integer, thus the output of a/b will be 3.
  • The statement float(a/b) will convert the result in float, a/b will be 3. Thus, float(a/b) = float(3) = 3.0. Output will be 3.0
  • The statement float(a)/float(b)) will cast type the value of a and b and values will be 10.0 and 3.0. Thus the result of float(a)/float(b) will be 3.33333333333
  • The statement a/b returns the output without remainder i.e. the integer part of the result will be printed. Hence, he output will be 3.

Program 2:

a = 36.24
b = 24 

res = a/b 
print "a/b : ", res 

res = (a//b)
print "(a//b) : ", res

Output

    a/b :  1.51
    (a//b) :  1.0

Explanation:

  • Value of a is float, so there is no need to cast type a or b, the output will be in float.
  • Statement a/b will return the divide result. Thus, the output will be 1.51.
  • Statement a//b will return only integer part of the result. Thus the output will be 1.0.


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.