Unlike other languages Java, C++, etc. Python is a strongly, dynamically-typed language in which we don’t have to specify the data type of the function’s return value and its arguments. It relates types with values instead of names. The only way to specify data of specific types is by providing explicit datatypes while calling the functions.
Example 1: We have a function to add 2 elements.
Python3
def add(num1, num2):
print ( "Datatype of num1 is " , type (num1))
print ( "Datatype of num2 is " , type (num2))
return num1 + num2
print (add( 2 , 3 ))
print (add( float ( 2 ), float ( 3 )))
|
Output:
Datatype of num1 is <class 'int'>
Datatype of num2 is <class 'int'>
5
Datatype of num1 is <class 'float'>
Datatype of num2 is <class 'float'>
5.0
Example 2: We have a function for string concatenation
Python3
def concatenate(num1, num2):
print ( "Datatype of num1 is " , type (num1))
print ( "Datatype of num2 is " , type (num2))
return num1 + num2
print (concatenate( 111 , 100 ))
print (concatenate( str ( 111 ), str ( 100 )))
|
Output:
Datatype of num1 is <class 'int'>
Datatype of num2 is <class 'int'>
211
Datatype of num1 is <class 'str'>
Datatype of num2 is <class 'str'>
111100
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Feb, 2023
Like Article
Save Article