Python File handling

Python Type Casting

Introduction To Python Type Casting

Type Casting means convert variable or object from one data type to another data type.For example convert string in to integer.

On some data type some operation does not allow so that type casting requires.For example, we want to perform division on a number, but that number stores in string format in variable so on that variable Python interpreter not allow to perform the division operation.

Python has a some inbuilt functions that help in type casting

  • int()
  • float()
  • complex()
  • bool()
  • str()

int()

int() function is used to convert variable into int type from string ,float or bool. int function not allow to convert complex type into int type.

Syntax

int(<variable-name>,<base>)

int function takes two arguments.

  • The first argument is variable that want to convert into int type.Default value is 0 if not pass any variable.
  • The Second argument is base of number in which want to convert.Default value is 10.

The float type variable is converted into it by removing the fraction part from a number.The str type variable must require a numeric value without the fraction part otherwise generate error.bool type value False converts into 0 and True value convert into 1.

Example

x = 10.10
type(x)
y = int(x)
print(y)
<class 'float'>
10
print(int(True))
1
print(int("125"))
125
print(int("125.36"))
print(int("125.36"))
ValueError: invalid literal for int() with base 10: '125.36'
print(int('0B101',2))
5
print(int( 125+10j ))
print(int(125+10j))
TypeError: can't convert complex to int
print(int())
0

float()

float() function is used to convert a variable into float type from string ,int or bool. float function not allow to convert complex type into float type.

Syntax

float(<variable-name>)

The int type variable is converted into without any data loss.The str type variable must require a numeric value.bool type value False converts into 0.0 and True value convert into 1.0

Example

print(float(125))
125.0
print(float(0b101))
5.0
print(float("125.10"))
125.1
print( float(True) )
1.0
print( float('0b11') )
print(float('0b11'))
ValueError: could not convert string to float: '0b11'
print( float('one') )
print(float('one'))
ValueError: could not convert string to float: 'one'
print( float( 10 + 5j ) )
print( float( 10 + 5j ) )
TypeError: can't convert complex to float

complex()

complex() function is used to convert a variable into complex type from string ,int or bool,float.

complex function has a two overloaded function.

Syntax

complex(<real-part>)
complex(<real-part>,<imaginary-part>
  • In first version of complex method imaginary part is not provided so that imaginary value by default consider as 0.
  • In second version of complex method both the arguments are provided.
  • If both the argument is not provided then both default value is 0 consider.

If pass str type variable as a argument then the string must be in valid format otherwise generate error.Valid format is <real> + <imaginary>J or <real> + <imaginary>j

Example

print(complex(5))
(5+0j)
print(complex(5 , 7 ))
(5+7j)
print(complex(5,-7))
(5-7j)
print(complex("5+7j"))
(5+7j)
print(complex(True))
(1+0j)
print(complex(False))
0j
print(complex(125.02))
(125.02+0j)
print(complex("5+7"))
print(complex("5+7"))
ValueError: complex() arg is a malformed string

bool()

bool() function is used to convert a variable into bool type from string ,int or complex,float.

Syntax

bool(<variable-name>)
  • If any argument is not passes in bool function then default value 0 is consider and that return False.
  • If argument variable has a int datatype then 0 value return False and all value other then 0 return True.
  • If argument variable has a float data type then 0.0 value return False and all value other then 0 return True.
  • If argument variable has a complex data type then if real and imaginary part both the value is 0 ( 0 + 0 j ) then return False otherwise return True.
  • If argument variable has a str type then only empty string return False otherwise return True.

Example

print(bool(0))
print(bool(5))
False
True
print(bool(0.0))
print(bool(10.5))
False
True
print(bool(0 + 0j ))
print(bool( 10 + 5j ))
False
True
print(bool(""))
print(bool("any"))
print(bool("False"))
print(bool())
False
True
True
False

str()

str() function is used to convert a variable into str type from other data types.

Syntax

str(<variable-name>,<encoding>,<errors>)
  • First argument is variable name that want to convert in str type.If not provide first argument then return empty string.
  • Second argument is about in which encoding format string want ti convert default UTF-8.
  • Third argument is about error mode at a time of conversion.6 types of errors are available default is strict.

Example

str(7)
str(5.7)
str(True)
str(5+7j)
str()
'7'
'5.7'
'True'
'(5+7j)'

Refrence Python.org