New course! Every coder should learn Generative AI!
Try a free lesson+ 2
what is the return type of the function?
I am a java developer learning python, in java we are specifying return type. but in python we are not specifying any return type, I think the default return type will be string. (or) we need to do by Type casting
3 Answers
+ 3
def add (x, y):
z = x + y
if type(x, y) == int:
return int(z)
elif type(x, y) == str:
return str(z)
elif type(x, y) == float:
return float(z)
elif type(x, y) == list:
return list(z)
elif type(x, y) == dict:
return dict(z)
add (2, 3)
z = int(5)
add('2', '3')
z = str('23')
etc...
this is just an example code, but it should represent how return specifies the type it returns (as you can't operate on two different types).
+ 2
python is a dynamically typed language, so you wont find the typical int x = 6, for example. you dont need to specify return types.
0
There is no default return type in python like we have in Java / php.
Check this out
def checkDefaultReturnType():
print("hello")
print(type(checkDefaultReturnType()))
It prints "<class 'NoneType'> " against print(type(checkDefaultReturnType()))