function - Difference between returns and printing in python? -
this question has answer here:
in python don't seem understanding return function. why use when print it?
def maximum(x, y): if x > y: print(x) elif x == y: print('the numbers equal') else: print(y) maximum(2, 3)
this code gives me 3
. using return same exact thing.
def maximum(x, y): if x > y: return x elif x == y: return 'the numbers equal' else: return y print(maximum(2, 3))
so what's difference between two? sorry mega noob question!
the point
return not function. control flow construct (like if
else
constructs). lets "take data between function calls".
break down
print
: gives value user output string.print(3)
give string'3'
screen user view. program lose value.return
: gives value program. callers of function have actual data , data type (bool, int, etc...)return 3
have value 3 put in place of function called.
example time
def ret(): return 3 def pri(): print(3) 4 + ret() # ret() replaced number 3 when function ret returns >>> 7 4 + pri() # pri() prints 3 , implicitly returns none can't added >>> 3 >>> typeerror cannot add int , nonetype
Comments
Post a Comment