Explicitly define datatype in a Python function
Last Updated :
15 Jul, 2025
Unlike other programming languages such as Java and C++, Python is a strongly, dynamically-typed language. This means that we do not have to explicitly specify the data type of function arguments or return values. Python associates types with values rather than variable names. However, if we want to work with specific data types, we can explicitly define them either while calling functions or by using type hints.
Explicitly Defining Data Types While Calling Functions
Example 1: Function to add 2 elements.
Python
def add(a, b):
print("Datatype of a is ", type(a))
print("Datatype of b is ", type(b))
return a + b
# calling the function without explicitly declaring the datatypes
print(add(2, 3))
# calling the function by explicitly, defining the datatypes as float
print(add(float(2), float(3)))
OutputDatatype of a is <class 'int'>
Datatype of b is <class 'int'>
5
Datatype of a is <class 'float'>
Datatype of b is <class 'float'>
5.0
Explanation:
- add function takes two arguments and prints their data types before returning their sum.
- When add(2, 3) is called, Python treats 2 and 3 as integers and the function returns 5.
- When add(float(2), float(3)) is called, the numbers are explicitly converted to floats hence the result is 5.0.
Example 2: Function for string concatenation
Python
def concatenate(a, b):
print("Datatype of a is ", type(a))
print("Datatype of b is ", type(b))
return a + b
# calling the function without explicitly declaring the datatypes
print(concatenate(111, 100))
# calling the function by explicitly defining the datatypes as string
print(concatenate(str(111), str(100)))
OutputDatatype of a is <class 'int'>
Datatype of b is <class 'int'>
211
Datatype of a is <class 'str'>
Datatype of b is <class 'str'>
111100
Explanation:
- concatenate() takes two arguments and returns their sum or concatenation.
- When concatenate(111, 100) is called, Python treats 111 and 100 as integers and performs numeric addition.
- When concatenate(str(111), str(100)) is called, the numbers are explicitly converted to strings and Python concatenates them.
Using Type Hints for Explicit Data Types
Python allows type hints (introduced in PEP 484) to explicitly define expected data types in function arguments and return values.
Example 1: Using type hints for clarity
Python
def multiply(num1: int, num2: int) -> int:
return num1 * num2
# Calling the function
print(multiply(4, 5))
Explanation:
- function multiply takes two integers (num1 and num2) and returns an integer (-> int).
- The type hints indicate the expected types but do not enforce them.
Example 2: Using Type Hints with Lists and Dictionaries
Python
from typing import List, Dict
# Function to square elements in a list
def fun1(numbers: List[int]) -> List[int]:
return [num ** 2 for num in numbers]
# Function to return a dictionary of student marks
def fun2() -> Dict[str, int]:
return {"Alice": 85, "Bob": 90}
print(fun1([1, 2, 3]))
print(fun2())
Output[1, 4, 9]
{'Alice': 85, 'Bob': 90}
Explanation:
- fun1 takes a list of integers (List[int]) and returns a list of integers.
- fun2 returns a dictionary where the keys are strings (Dict[str, int]) and values are integers.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice