type()

type() returns the data type of the object .

list of Python built in Data types

  1. None
  2. Numeric ( integer, float , complex )
  3. Sequence ( list, tuple, set , range )
  4. Bool
  5. string ( or str )
  6. dictionary.

Getting type of object ( Output is with comment # part )
print(type(5))          #<class 'int'>
print(type(3.4))        #<class 'float'>
print(type(5+9j))       #<class 'complex'>
Using List , set , dictionary and tuple.
print(type(['One','Tow','Three']))          # <class 'list'>
print(type(('One','Tow','Three')))          # <class 'tuple'>
print(type({'One','Tow','Three'}))          # <class 'set'>
print(type({1:'Alex',2:'Ronald',3:'John'})) # <class 'dict'>

None

If the object is not assigned any value, we call it NoneType.
a=None
print(type(a)) # <class 'NoneType'>

Range

sequence of numbers ( Immutable ) can be returned by using range(). These are range data type.
x=range(5)      # 5 is stop value
print(type(x))  # <class 'range'>

bool

Bool data type stors values True or False. Read more on bool()
a=True
print(type(a)) # <class 'bool'>

str

String data types. Built in function str() update to string data type.
print(type("Welcome"))  #<class 'str'>
Apart from these built in data types, user can create its own data types by using class.
class Animal():
    #instance attributes
    def __init__(self1,name1,age):
        self1.name= name1
        self1.age=age

#instantiate the Animal  class
tiger=Animal("Ronald",5)  # tiger is an object of Animal class

print(type(tiger)) # <class '__main__.Animal'>

How to covert one data type to other ?

We can use int() , float(), str(), complex() , bool() to convert data types.

Integer to String by using str()
my_int=5
my_int=str(5)
print(type(my_int)) # <class 'str'>
Integer to float()
my_int=5
my_int=float(5)
print(type(my_int)) # <class 'float'>
Integer to complex, by using complex()
my_int1=5
my_int2=3
my_int=complex(my_int1,my_int2)
print(type(my_int)) # <class 'complex'>
Float to Integer by using float()
my_float=5.4
my_int=int(my_float)
print(type(my_int)) # <class 'int'>
All types of conversion are not possible. This will generate error.
my_str='plus2net'
my_int=int(my_str)
print(type(my_int))
Output
 ValueError: invalid literal for int() with base 10: 'plus2net'

Variables and Data Types

When we create one variable, actually we are creating one object or instance of a data type ( class ).
i=5
In above code , i is one object of integer class or data type.
Variables in Python Keywords in Python
All Built in Functions in Python

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer