DEV Community

Guru prasanna
Guru prasanna

Posted on

Python Day 3- Datatype and Return Datatype

Datatype:
Type of data given as input to computer.If an input is given in alphabet or number or any other types.
To find this datatype type() function is used.
For example:

x = 5 #input
print(type(x))#To find datatype 
Enter fullscreen mode Exit fullscreen mode

In above example x is reference variable or identifier and 5 is value.
= is assignment operator which denotes value is assigned to variable.

Criteria for identifiers:
1.Meaningful names
2.No space is allowed.You can use _ instead of space.
3.Numbers are allowed but not in beginning.
4.No special characters are allowed except _ , $.

Refer:https://www.w3schools.com/python/python_datatypes.asp

Types:
1.String(str):Refers a word
Ex: name="Guru"

2.Integer(int):Refers to numbers
Ex: Age=23

3.Float(float): Decimal numbers
Ex: x=12.2

4.Boolean(bool):True or False
Ex: a=True
b=False

There are many other datatypes like list, set, complex, tuple, range, dict, set, frozenset, bytes, bytearray, memoryview, nonetype.

why do we need datatypes:
computer will get confused whether the input should be taken as it is or not.Like if a value is entered inside quotes(' '," ")then python will take it as string and won't change the value.

def add(no1, no2):
    print(no1+no2)

def subtract(no1,no2):
    print(no1-no2)

def multiply(no1,no2):
    print(no1*no2)

def divide(no1,no2):
    print(no1/no2)

add(10,12)
subtract(12,8)
Enter fullscreen mode Exit fullscreen mode

We can define multiple functions but it will be executed only if it is called.In above program add and subtract is only called so in output those 2 functions alone will be executed.
So output will be:

22
4
Enter fullscreen mode Exit fullscreen mode

Return datatype:
By calling a function using type() we can identify return datatype of a function.
For ex-type(subtract(100,50).

def add(no1,no2):
    print(no1+no2)
    return no1+no2

def subtract(no1,no2):
    print(no1-no2)
    return no1-no2

def multiply(no1,no2):
    print(no1*no2)
    return no1*no2

def divide(no1,no2):
    print(no1/no2)

def modulus(no1,no2):
    pass

result1=add(10,12)
result2=subtract(12,8)
result3=multiply(result1,result2)
divide(result3,10)
modulus(10,20)

print(type(subtract(100,50)))
print(type(multiply(10,20)))
print(type(modulus(10,100)))
Enter fullscreen mode Exit fullscreen mode

For the above syntax output will be

22
4
88
8.8
50
<class 'int'>
200
<class 'int'>
<class 'nonetype'>

Enter fullscreen mode Exit fullscreen mode

In above for modulus,pass is given which means it will be defined in future.

1.If the result is returned then only type will be shown in output as int or str or any other datatype.
2.For modulus since pass is given,nothing will be returned while calling and datatype will be shown as "nonetype".

Top comments (0)