Type Casting in Python Programming

Ø  We can convert one type value to another type. This conversion is called Typecasting or Type coercion.

The following are various inbuilt functions for type casting.

1. int()                        2. float()         3. complex()              4. bool()         5. str()

1. int();

Ø  We can use this function to convert values from other types to int

Example:

>>> int(123.987)à123

>>> int(10+5) à TypeError: can't convert complex to int

>>> int(True)à 1

>>> int(False)à 0

>>> int("10")à10

>>> int("10.5")à ValueError: invalid literal for int() with base 10: "10.5"

>>> int("ten")à ValueError: invalid literal for int() with base 10: "ten'

>>> int("OB1111")àValueError: invalid literal for int() with base 10: '081111'

Note:

1. We can convert from any type to int except complex type.

2. If we want to convert str type to int type, compulsary str should contain only integral value and should be specified in base-10

2. float():

Ø  We can use float() function to convert other type values to float type.

 >>> float(10)à 10.0

>>> float(10+5)) àTypeError: can't convert complex to float

>>> float(True)à 1.0

>>> float(False)à 0.0

>>> float("10")à10.0

>>> float("10.5")à10.5

>>> float("ten")à ValueError: could not convert string to float: "ten'

>>> float("081111")àValueError: could not convert string to float: '081111'

Note:

1. We can convert any type value to float type except complex type.

2. Whenever we are trying to convert str type to float type compulsary str should be either integral or floating point literal and should be specified only in base-10.

3.complex();

Ø  We can use complex() function to convert other types to complex type.

Form-1: complex(x)

Ø  We can use this function to convert x into complex number with real part x and imaginary part 0.

Example:

complex(10) è10+0j

complex(10.5) è10.5+0)

complex(True) è1+0j

complex(False) è0j

complex("10")è10+0j

complex("10.5")è10.5+0]

complex("ten")è ValueError: complex() arg is a malformed string

Form-2: complex(x,y)

Ø  We can use this method to convert x and y into complex number such that x will be real part and y will be imaginary part.

Example: complex(10,-2)è10-2j

                   complex(True,False)è1+0j

4. bool():

Ø  We can use this function to convert other type values to bool type.

Example:

>>> bool(0)èFalse

>>> bool(1) èTrue

>>> bool(10) èTrue

>>> bool(10.5) èTrue

>>> bool(0.178) èTrue

>>> bool(0.0) èFalse

>>> bool(10-2)) èTrue

>>> bool(0+1.5j) èTrue

>>> bool(0+0)) èFalse

>>> bool("True")èTrue

>>> bool("False")èTrue

>>> bool("")èFalse

 

5. str();

Ø  We can use this method to convert other type values to str type

Example:

>>> str(10) è'10'

>>> str(10.5) è10.5"

>>> str(10+5) è(10+5))"

>>> str(True) è 'True'

No comments:

Post a Comment

Thank you Very Much.For Given Comment

Downloading Talliki Vandhanam List

--> Citizens can know the status of Talliki Vandhanam Click Here -->Knowing the Status of Talliki Vandhanam   --> In GSWS NBM Login...