Featured

    Featured Posts

Python Tutorial 5:Type Conversion In Python


Type Conversion:



 §  The process of converting the value of one data type (integer, string, float, etc.) to  
       another data type is called type conversion. Python has two types of type conversion.
   1.     Implicit Type Conversion
   2.     Explicit Type Conversion 
Explicit Type Conversion:
 §  In Explicit Type Conversion, users convert the data type of an object to required data type. We use the predefined functions like int()float()str(), etc to perform explicit type conversion.

·        int()
·        float()
·        complex()
·        bool()
·        str()
int()
 §  By using this function, we can convert values from other types to int

print(int(123.456)) #output will be 123
print(int(True))    #output will be 1
print(int(False))   #output will be 0
print(int(“10”))    #output will be 10
print(int(10+20j))
print(int(10+20j))
TypeError: can't convert complex to int

Note:-
§  when we are trying to convert string value into int type compulsory string value  
   should be int type with base 10.
print(int("10.2"))
print(int("10.2"))
ValueError: invalid literal for int() with base 10: '10.2'

print(int("0B111"))
print(int("0B111"))
ValueError: invalid literal for int () with base 10: '0B111'

Note:
§  We can convert from any type to int except complex type.
float(): 
 §  By using this function, we can convert values from other types to float.

print(float(10))  #output will be 10.0
print(float(True)) #output will be 1.0
print(float(False)) #output will be 0.0

§  Whenever we are trying to convert str type to float type compulsory str should 
  be either integral or floating-point literal and should be specified only in base-10.
print(float("10")) #output will be 10.0 
print(float("10.1")) #output will be 10.1
print(float("ten"))
print(float("ten"))
ValueError: could not convert string to float: 'ten'

print( float(0B111))  #output will be 7.0
print( float("0B111"))
print( float("0B111"))
ValueError: could not convert string to float: '0B111'
Note:-
   |- We can convert any type to float except to complex type.

complex():
   |- Other Types to complx type.

Form-1: complex(x)==>x+0j
print(complex(10))   #output will be (10+0j)
print(complex(10.5)) #output will be (10.5+0j)
print(complex(True)) #output will be (1+0j)
print(complex(False)) #output will be (0j)
print(complex("10"))  #output will be (10+0j)
print(complex("10.5")) #output will be (10.5+0j)
print(complex("ten"))
print(complex("ten"))
ValueError: complex() arg is a malformed string

Form-2: complex(x,y)==>x+yj
print(complex(10,20))   #output will be (10+20j)
print(complex(True,False))  #output will be (1+0j)
print(complex(10,20.5))  #output will be (10+20.5j)
print(complex("10","12"))
print(complex("10","12"))
TypeError: complex() can't take second arg if first is a string

bool():

 §  By using this function, we can convert values from other types to bool.
|- For int arguments
             |- For non-zero value it returns True.
             |- For zero value it returns False.

print(bool(0))  #output will be False
print(bool(1))  #output will be True
print(bool("10"))  #output will be True

|- For float arguments
              |- If before decimal and after decimal is zero then it returns False.
              |- If before decimal or after decimal is non-zero value then it returns True.

print(bool(0.0))  #output will be False 
print( bool(0.1)) #output will be True
print(bool(0.01)) #output will be True
print(bool(2.3)) #output will be True
|- For complex arguments
                |- If both real and img part is zero then it will return False.
                |- If at least one part is non-zero then it will return True.

print(bool(0+0j)) #output will be False
print( bool(0+1j)) #output will be True
print(bool(0+2.5j)) #output will be True
print( bool(2.5+0j)) #output will be True

|- For str arguments
            |- arg is empty string==>"==>False
            |- arg is non-empty==> True

print(bool(''))  #output will be False
print(bool('python')) #output will be True
print( bool(' '))  #output will be True

str():-
|- is Used To convert any types to str types.
print(str(100))    #output will be 100
print(str(2.6))    #output will be 2.6
print(str(True))   #output will be True
print(str(False))   #output will be False
print(str(10+20j))  #output will be 10+20j

ord(character):-
§  By using ord() method we can converts any character to a numeric value. This method takes only one length string.
# converting character  to integer 
c = ord('a'
print ("After converting character to integer : ",end=""
print (c)  
output:

After converting character to integer : 97

# converting character  to integer 
c = ord('abc'
print ("After converting character to integer : ",end=""
print (c) 
    c = ord('abc') 
TypeError: ord() expected a character, but string of length 3 found

chr(number):-
§  By using chr(number) method we can convert number to its corresponding ASCII  
  character. 
# converting integer to character
c = chr(65)
print ("After converting integer to character : ",end="")
print (c)
# converting integer to character
c = chr(100)
print ("After converting integer to character : ",end="")
print (c)
# converting integer to character
c = chr(55)
print ("After converting integer to character : ",end="")
print (c)
output:
After converting integer to character : A
After converting integer to character : d
After converting integer to character : 7

author

Author Name

Author Description!

Get Free Email Updates to your Inbox!

Post a Comment

www.CodeNirvana.in

Powered by Blogger.

About

Site Links

Popular Posts

Translate

Total Pageviews