Open In App

Explicitly define datatype in a Python function

Last Updated : 01 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# function definition
def add(num1, num2):
    print("Datatype of num1 is ", type(num1))
    print("Datatype of num2 is ", type(num2))
    return num1 + num2
 
# 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)))


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




# function definition
def concatenate(num1, num2):
    print("Datatype of num1 is ", type(num1))
    print("Datatype of num2 is ", type(num2))
    return num1 + num2
 
# 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)))


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


Previous Article
Next Article

Similar Reads

How to Explicitly Free Memory in Python?
Python uses a technique called garbage collection to automatically manage memory. The garbage collector identifies objects that are no longer in use and reclaims their memory. The primary mechanism for this is reference counting, augmented by a cyclic garbage collector to handle reference cycles. When to Explicitly Free MemoryWhile explicit memory
3 min read
Python - Change Datatype of Tuple Values
Sometimes, while working with set of records, we can have a problem in which we need to perform a data type change of values of tuples, which are in its 2nd position, i.e value position. This kind of problem can occur in all domains that include data manipulations. Let's discuss certain ways in which this task can be performed. Input : test_list =
3 min read
Python - Find frequency of given Datatype in tuple
Sometimes, while working with Python records, we can have a problem in which we need to extract count of any data type occurred in tuple. This can have application in various domains such as day-day programming and web development. Let's discuss certain ways in which this task can be performed. Input : test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is'), da
6 min read
Convert Python String to Float datatype
Let us see how to convert a string object into a float object. We can do this by using these functions : float() decimal() Method 1: Using float() # declaring a string str1 = &quot;9.02&quot; print(&quot;The initial string : &quot; + str1) print(type(str1)) # converting into float str2 = float(str1) print(&quot;\nThe conversion of string to float i
2 min read
How to Retrieve Blob Datatype from Postgres with Python
In this article, We will learn How to retrieve BLOB from a PostgreSQL database. BLOB is a Binary large object (BLOB) is a data type that can store any binary data.To Retrieve Blob Datatype from Postgres with Python we will use psycopg2.Stepwise Implementation:Connect to the PostgreSQL server.Create a cursor with the help of cursor() method in Pytho
3 min read
Pandas DataFrame dtypes Property | Find DataType of Columns
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns).  Pandas DataFrame.dtypes attribute returns a series with the data type of each column. Example: C/C++ Code import pandas as pd df = pd.DataFrame({'Weight': [45, 88, 56, 15, 71], 'Name': ['Sam', 'Andrea', 'Alex',
2 min read
How to define a mathematical function in SymPy?
SymPy is a Python Library that makes 'Symbolic Computation' possible in Python. Mathematical Functions using SymPy We can define mathematical functions by using SymPy in Python. There are two types of functions that we can define with the help of SymPy: 'Undefined Functions' and 'Custom Functions'. Undefined Functions: A programmer can create 'Unde
4 min read
How to Define an Auto Increment Primary Key in PostgreSQL using Python?
Prerequisite: PostgreSQL Python has various database drivers for PostgreSQL. Currently, most used version is psycopg2 because it fully implements the Python DB-API 2.0 specification. The psycopg2 provides many useful features such as client-side and server-side cursors, asynchronous notification and communication, COPY command support, etc. Install
3 min read
Define a Nested Dictionary in Python
In Python, dictionaries are powerful data structures that allow you to store and organize data using key-value pairs. When you need to represent complex and hierarchical data, nested dictionaries come in handy. A nested dictionary is a dictionary that contains one or more dictionaries as values. In this article, we will explore some simple methods
3 min read
Define and Call Methods in a Python Class
In object-oriented programming, a class is a blueprint for creating objects, and methods are functions associated with those objects. Methods in a class allow you to define behavior and functionality for the objects created from that class. Python, being an object-oriented programming language, provides a straightforward syntax for defining and cal
3 min read
Practice Tags :