Type Conversion Practice Question ( DS150423 )

Try to convert (float,str,bool,complex) into different datatypes and identify what all conversions are possible.

3 Likes

1.complex cant be converted to int and float
2.any datatype above given cant be converted to None
3.string cant be converted to int,float,complex.

3 Likes
  1. Float:
  • float to int: possible, may result in a loss of decimal places
  • float to str: possible
  • float to bool: possible, False if the float is 0.0, otherwise True
  • float to complex: possible, the imaginary part will be 0.0
  1. String:
  • str to int: possible if the string represents an integer value, otherwise raises ValueError
  • str to float: possible if the string represents a float value, otherwise raises ValueError
  • str to bool: possible, evaluates to False for empty string (‘’), otherwise True
  • str to complex: possible if the string represents a complex value, otherwise raises ValueError
  1. Boolean:
  • bool to int: possible, True converts to 1 and False converts to 0
  • bool to float: possible, True converts to 1.0 and False converts to 0.0
  • bool to str: possible, True converts to ‘True’ and False converts to ‘False’
  • bool to complex: possible, True converts to (1+0j) and False converts to 0j
  1. Complex:
  • complex to int: not possible
  • complex to float: possible, may result in a loss of the imaginary part
  • complex to str: possible
  • complex to bool: possible, False if both real and imaginary parts are 0.0, otherwise True.

Python considers 0j as False and non-zero complex numbers as True.

4 Likes

int , float, and bool can be typecasted into any othe datatype except Nonetype, when we try to typecast any variable to nonetype it shows an error that ‘‘Object not callable’’(whatever that means).

if str is a number value as such like ‘‘67’’ then it can be typecasted into any other datatype but if the string is some word or sencentence then it cannot be typecasted into int and float, otherrwise fine.

complex no under no value gets converted into an integer but it does get converted to a float no, a str and bool.

2 Likes

1.Complex cant be converted to int, but it cannot convert in float possible, may result in a loss of the imaginary part
2.Python considers 0j as False and non-zero complex numbers as True
3.String is a number value as such like ‘‘1,5,6,9’’ then it can be converted into any other datatype until it’s number it is not able to convert words sentence

2 Likes

kindly give an example for this

sorry maam it was an mistake as i was not using laptop that time and it mistakenly type it can to cannot. Apologies

No problem :+1: @tusharsuparets

1 Like

str can’t converted into anyone
float conveted into anyone
complex cant converted into float, int,

1 Like

1.Conversion of float is possible to every data type except in the case of float 0.0 where in Boolean it shows false .
2.Conversion of string is possible to Boolean and string itself only ,where other it shows like cannot be converted as well malformed string.
3.Conversion of Boolean is possible to every data type.
4.Conversion of Complex is not possible in int and float but it is possible in string, bool, complex

1 Like

SInce the answer related to type conversion is already given, here is a try on data type conversion to different collection (list to tuple,set,dict and vice versa)

data conversion to collection list

str1 = (“Mary”) #string to list
lst0 = list(str1)
print(“New List0”,lst0)

tupl = (4,5,8,7,4) #tuple to list
lst = list(tupl)
print(“New List”, lst)

set1 = {5,8,“bag”,8,4,6,7} #set to list
lst1 = list(set1)
print(“New List1”,lst1)

dct1 = {10:“Professor”,20:“Student”,30:“Staff”} # dict to list(only Keys)
lst2 = list(dct1.keys())
print(“New List2”,lst2)

dct1 = {10:“Professor”,20:“Student”,30:“Staff”} # dict to list(only Values)
lst3 = list(dct1.values())
print(“New List3”,lst3)

dct1 = {10:“Professor”,20:“Student”,30:“Staff”} # dict to list(only Items)
lst4 = list(dct1.items())
print(“New List4”,lst4)

#data conversion to collection tuple

str0 = (“Mary”) #str to tuple
tupl0 = tuple(str1)
print(“New Tuple0”,tupl0)

lst1 = [‘M’,‘A’,‘R’,‘Y’] #lst to tuple
tupl1 = tuple(lst1)
print(“New Tuple1”,tupl1)

set1 = {5,8,“bag”,8,4,6,7} #set to tuple
tupl2 = tuple(set1)
print(“New Tuple2”,tupl2)

dct1 = {10:“Professor”,20:“Student”,30:“Staff”} # dict to tuple(only Keys)
tupl3 = tuple(dct1.keys())
print(“New Tuple3”,tupl3)

dct1 = {10:“Professor”,20:“Student”,30:“Staff”} # dict to tuple(only Values)
tupl4 = tuple(dct1.values())
print(“New Tuple4”,tupl4)

dct1 = {10:“Professor”,20:“Student”,30:“Staff”} # dict to tuple(only Items)
tupl5 = tuple(dct1.items())
print(“New Tuple5”,tupl5)

#data conversion to collection set

str0 = (“Mary”) #str to set
set0 = set(str0)
print(“New Set0”,set0)

lst1 = [‘M’,‘A’,‘R’,‘Y’] #lst to set
set1 = set(lst1)
print(“New Set1”,set1)

tup1 = (5,8,“bag”,8,4,6,7) #tuple to set
set2 = set(tup1)
print(“New Set2”,set2)

dct1 = {10:“Professor”,20:“Student”,30:“Staff”} # dict to set(only Keys)
set3 = set(dct1.keys())
print(“New Set3”,set3)

dct1 = {10:“Professor”,20:“Student”,30:“Staff”} # dict to set(only Values)
set4 = set(dct1.values())
print(“New Set4”,set4)

dct1 = {10:“Professor”,20:“Student”,30:“Staff”} # dict to set(only Items)
set5 = set(dct1.items())
print(“New Set5”,set5)

#data conversion to collection dict

#set ti dict
#1. using json.loads()
#2. using est.literal.eval()
#3. using general expression

which can be learned later

lst1 = [(‘M’,4),(‘A’,5),(‘R’,6),(‘Y’,7)] #lst to dict
dct1 = dict(lst1)
print(“New Dict1”,dct1)

tup1 = ((5,8),(“bag”,8),(“hot”,4),(6,7)) #tuple to dict
dct2 = dict(tup1)
print(“New Dict2”,dct2)

set1 = {(5,8),(“bag”,8),(“hot”,4),(6,7)} #set to dict
dct3 = dict(set1)
print(“New Dict3”,dct3)

OUTPUT:
data type