0% found this document useful (0 votes)
13 views3 pages

Day 20 Type Conversion

Type casting is the conversion of one data type to another. The main type casting functions in Python are int(), float(), complex(), bool(), and str(). int() converts values to integers, float() converts to floating point numbers, complex() converts to complex numbers, bool() converts to Boolean True or False, and str() converts to string representations of values. These functions allow values to be used for different purposes but can result in errors if the conversion is not possible for the given value.

Uploaded by

Kumar Sujit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Day 20 Type Conversion

Type casting is the conversion of one data type to another. The main type casting functions in Python are int(), float(), complex(), bool(), and str(). int() converts values to integers, float() converts to floating point numbers, complex() converts to complex numbers, bool() converts to Boolean True or False, and str() converts to string representations of values. These functions allow values to be used for different purposes but can result in errors if the conversion is not possible for the given value.

Uploaded by

Kumar Sujit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Type Casting

=========================
We can convert one type / class value to another type/ class. This
conversion is called Typecasting or Type coersion.
The following are various inbuilt functions for type casting.
1. int()
2. float()
3. complex()
4. bool()
5. str()

int():
------
We can use this function to convert values from other types to int
Ex:

>>> int(123.987)
123
>>> int(10+5j)
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("0B1111")
ValueError: invalid literal for int() with base 10: '0B1111'
>>>int ("101", base =2)
5

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

float():
--------
We can use float() function to convert other type values to float type.
>>> float(10)
10.0
>>> float(10+5j)
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("0B1111")
ValueError: could not convert string to float: '0B1111'
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.

complex():
--------------
We can use complex() function to convert other types to complex type.
Format 1: complex(x)
We can use this function to convert x into complex number with real
part x and imaginary part 0.
Ex:
>>>complex(10) #==>10+0j
>>>complex(10.5) #===>10.5+0j
>>>complex(True) #==>1+0j
>>>complex(False) #==>0j
>>>complex("10") #==>10+0j
>>>complex("10.5") #==>10.5+0j
>>>complex("ten") #==>ValueError: complex() arg is a malformed string

Format-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.
Ex:
>>>complex(10,-2) #==>10-2j
>>>complex(True,False) #==>1+0j

bool():
---------
We can use this function to convert other type values to bool type.
Ex:
>>>bool(0) #==>False
>>>bool(1) #==>True
>>>bool(10) #===>True
>>>bool(10.5) #===>True
>>>bool(0.178) #==>True
>>>bool(0.0) #==>False
>>>bool(10-2j) #==>True
>>>bool(0+1.5j) #==>True
>>>bool(0+0j) #==>False
>>>bool("True") #==>True
>>>bool("False") #==>True
>>>bool("") #==>False

str():
------
We can use this method to convert other type values to str type
Ex:
>>> str(10) #==>'10'
>>> str(10.5) #==>'10.5'
>>> str(10+5j) #==>'(10+5j)'
>>> str(True) #==>'True'

You might also like