Q:

Python | Declare different types of variables, print their values, types and Ids

belongs to collection: Python basic programs

0

Declare different types of variables; print their types, ids and variables in Python.

There are two inbuilt functions are using in the program:

  1. type() - its returns the data type of the variable/object.
  2. id() - it returns the unique identification number (id) of created object/variable.
  3.  

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

print("Numbers")
print("---------------------------------")
a=10
print(type(a),id(a),a)
a=23.7
print(type(a),id(a),a)
a=2+6j
print(type(a),id(a),a)


print("\nText")
print("---------------------------------")
a='h'
print(type(a),id(a),a)
a="h"
print(type(a),id(a),a)
a='hello'
print(type(a),id(a),a)
a="hello"
print(type(a),id(a),a)

print("\nBoolean")
print("---------------------------------")
a=True
print(type(a),id(a),a)

print("\nFunction")
print("---------------------------------")
def fun1():
    return "I am Function"
a=fun1
print(type(a),id(a),a())

print("\nObjects")
print("---------------------------------")
class Demo:
    def hi(self):
        return "Hi"
a=Demo()
print(type(a),id(a),a.hi())

print("\nCollections")
print("---------------------------------")
a=[1,2,3]
print(type(a),id(a),a)
a=[]
print(type(a),id(a),a)
a=(1,2,3)
print(type(a),id(a),a)
a=()
print(type(a),id(a),a)
a=1,2,3
print(type(a),id(a),a)
a={1,2,3}
print(type(a),id(a),a)
a={}
print(type(a),id(a),a)
a={"id":1,"name":"pooja"}
print(type(a),id(a),a)

Output

Numbers
---------------------------------
<class 'int'> 10455328 10
<class 'float'> 139852163465696 23.7
<class 'complex'> 139852162869456 (2+6j)

Text
---------------------------------
<class 'str'> 139852162670976 h   
<class 'str'> 139852162670976 h   
<class 'str'> 139852162911512 hello    
<class 'str'> 139852162911512 hello    
  
Boolean
--------------------------------- 
<class 'bool'> 10348608 True 
  
Function 
--------------------------------- 
<class 'function'> 139852163226616 I am Function 
  
Objects
--------------------------------- 
<class '__main__.Demo'> 139852162234016 Hi  
  
Collections   
--------------------------------- 
<class 'list'> 139852162259208 [1, 2, 3]    
<class 'list'> 139852162261256 [] 
<class 'tuple'> 139852162244752 (1, 2, 3)   
<class 'tuple'> 139852182028360 ()
<class 'tuple'> 139852162244968 (1, 2, 3)   
<class 'set'> 139852163034472 {1, 2, 3}
<class 'dict'> 139852163024776 {} 
<class 'dict'> 139852163024584 {'id': 1, 'name': 'pooja'}   

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to demonstrate variables scope... >>
<< Python | Printing different values (integer, float...