0% found this document useful (0 votes)
15 views7 pages

Python TypeErrors with Data Types

Uploaded by

Koushik Banala
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)
15 views7 pages

Python TypeErrors with Data Types

Uploaded by

Koushik Banala
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

#######float######

a=17.0
#premitive
int(a)
17
complex(a)
(17+0j)
bool(a)
True
#non premitive
str(a)
'17.0'
list(a)
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
list(a)
TypeError: 'float' object is not iterable
tuple(a)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
tuple(a)
TypeError: 'float' object is not iterable
set(a)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
set(a)
TypeError: 'float' object is not iterable
dict(a)
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
dict(a)
TypeError: 'float' object is not iterable

######complex#######
b=10+7j
#premitive
int(b)
Traceback (most recent call last):
File "<pyshell#16>", line 2, in <module>
int(b)
TypeError: int() argument must be a string, a bytes-like object or a real number,
not 'complex'
float(b)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
float(b)
TypeError: float() argument must be a string or a real number, not 'complex'
bool(b)
True
#non premitive
str(b)
'(10+7j)'
list(b)
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
list(b)
TypeError: 'complex' object is not iterable
tuple(b)
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
tuple(b)
TypeError: 'complex' object is not iterable
set(b)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
set(b)
TypeError: 'complex' object is not iterable
dict(b)
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
dict(b)
TypeError: 'complex' object is not iterable

########bool##########
c=0
d=1
#premitive
int(c)
0
int(d)
1
float(c)
0.0
float(d)
1.0
complex(c)
0j
complex(d)
(1+0j)
#non premitive
str(c)
'0'
str(d)
'1'
list(c)
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
list(c)
TypeError: 'int' object is not iterable
list(d)
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
list(d)
TypeError: 'int' object is not iterable
tuple(c)
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
tuple(c)
TypeError: 'int' object is not iterable
tuple(d)
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
tuple(d)
TypeError: 'int' object is not iterable
set(c)
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
set(c)
TypeError: 'int' object is not iterable
set(d)
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
set(d)
TypeError: 'int' object is not iterable
dict(c)
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
dict(c)
TypeError: 'int' object is not iterable
dict(d)
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
dict(d)
TypeError: 'int' object is not iterable

e=True
f=False
#premitive
int(e)
1
int(f)
0
float(e)
1.0
float(f)
0.0
complex(e)
(1+0j)
complex(f)
0j
#non premitive
str(e)
'True'
str(f)
'False'
list(e)
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
list(e)
TypeError: 'bool' object is not iterable
list(f)
Traceback (most recent call last):
File "<pyshell#62>", line 1, in <module>
list(f)
TypeError: 'bool' object is not iterable
tuple(e)
Traceback (most recent call last):
File "<pyshell#63>", line 1, in <module>
tuple(e)
TypeError: 'bool' object is not iterable
tuple(f)
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
tuple(f)
TypeError: 'bool' object is not iterable
set(e)
Traceback (most recent call last):
File "<pyshell#65>", line 1, in <module>
set(e)
TypeError: 'bool' object is not iterable
set(f)
Traceback (most recent call last):
File "<pyshell#66>", line 1, in <module>
set(f)
TypeError: 'bool' object is not iterable
dict(e)
Traceback (most recent call last):
File "<pyshell#67>", line 1, in <module>
dict(e)
TypeError: 'bool' object is not iterable
dict(f)
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
dict(f)
TypeError: 'bool' object is not iterable

########string###########
s="hello"
#premitive
int(s)
Traceback (most recent call last):
File "<pyshell#82>", line 1, in <module>
int(s)
ValueError: invalid literal for int() with base 10: 'hello'
float(s)
Traceback (most recent call last):
File "<pyshell#83>", line 1, in <module>
float(s)
ValueError: could not convert string to float: 'hello'
complex(s)
Traceback (most recent call last):
File "<pyshell#84>", line 1, in <module>
complex(s)
ValueError: complex() arg is a malformed string
bool(s)
True
#non premitive
list(s)
['h', 'e', 'l', 'l', 'o']
tuple(s)
('h', 'e', 'l', 'l', 'o')
set(s)
{'h', 'l', 'e', 'o'}
dict(s)
Traceback (most recent call last):
File "<pyshell#90>", line 1, in <module>
dict(s)
ValueError: dictionary update sequence element #0 has length 1; 2 is required
s= "he","hi"
dict(s)
{'h': 'i'}

###########list##########
l=[1,"hey",3,"party"]
#premitive
int(l)
Traceback (most recent call last):
File "<pyshell#112>", line 1, in <module>
int(l)
TypeError: int() argument must be a string, a bytes-like object or a real number,
not 'list'
float(l)
Traceback (most recent call last):
File "<pyshell#113>", line 1, in <module>
float(l)
TypeError: float() argument must be a string or a real number, not 'list'
complex(l)
Traceback (most recent call last):
File "<pyshell#114>", line 1, in <module>
complex(l)
TypeError: complex() first argument must be a string or a number, not 'list'
bool(l)
True

#non premitive

str(l)
"[1, 'hey', 3, 'party']"
tuple(l)
(1, 'hey', 3, 'party')
set(l)
{1, 3, 'party', 'hey'}
dict(l)
Traceback (most recent call last):
File "<pyshell#122>", line 1, in <module>
dict(l)
TypeError: cannot convert dictionary update sequence element #0 to a sequence

##################tuple############
t=(1,"hey",3,"party")
#premitive
int(t)
Traceback (most recent call last):
File "<pyshell#130>", line 1, in <module>
int(t)
TypeError: int() argument must be a string, a bytes-like object or a real number,
not 'tuple'
float(t)
Traceback (most recent call last):
File "<pyshell#131>", line 1, in <module>
float(t)
TypeError: float() argument must be a string or a real number, not 'tuple'
complex(t)
Traceback (most recent call last):
File "<pyshell#132>", line 1, in <module>
complex(t)
TypeError: complex() first argument must be a string or a number, not 'tuple'
bool(t)
True
#non premitive
str(t)
"(1, 'hey', 3, 'party')"
list(t)
[1, 'hey', 3, 'party']
set(t)
{1, 3, 'party', 'hey'}
dict(t)
Traceback (most recent call last):
File "<pyshell#138>", line 1, in <module>
dict(t)
TypeError: cannot convert dictionary update sequence element #0 to a sequence

###############set################
se={"go",11,"hai",22,"go"}
#premitive
>>> int(se)
Traceback (most recent call last):
File "<pyshell#149>", line 1, in <module>
int(se)
TypeError: int() argument must be a string, a bytes-like object or a real number,
not 'set'
>>> float(se)
Traceback (most recent call last):
File "<pyshell#150>", line 1, in <module>
float(se)
TypeError: float() argument must be a string or a real number, not 'set'
>>> complex(se)
Traceback (most recent call last):
File "<pyshell#151>", line 1, in <module>
complex(se)
TypeError: complex() first argument must be a string or a number, not 'set'
>>> bool(se)
True
>>> #non premitive
>>> str(se)
"{'hai', 11, 'go', 22}"
>>> list(se)
['hai', 11, 'go', 22]
>>> tuple(se)
('hai', 11, 'go', 22)
>>> dict(se)
Traceback (most recent call last):
File "<pyshell#157>", line 1, in <module>
dict(se)
ValueError: dictionary update sequence element #0 has length 3; 2 is required
>>>
>>>
>>>

>>>
>>> ###################dict###################
>>> d={1:"come","say":2,3:"no","way":4}
>>> #premitive
>>> int(d)
Traceback (most recent call last):
File "<pyshell#165>", line 1, in <module>
int(d)
TypeError: int() argument must be a string, a bytes-like object or a real number,
not 'dict'
>>> float(d)
Traceback (most recent call last):
File "<pyshell#166>", line 1, in <module>
float(d)
TypeError: float() argument must be a string or a real number, not 'dict'
>>> complex(d)
Traceback (most recent call last):
File "<pyshell#167>", line 1, in <module>
complex(d)
TypeError: complex() first argument must be a string or a number, not 'dict'
>>> bool(d)
True
>>> #non premitive
>>> str(d)
"{1: 'come', 'say': 2, 3: 'no', 'way': 4}"
>>> list(d)
[1, 'say', 3, 'way']
>>> tuple(d)
(1, 'say', 3, 'way')
>>> set(d)
{1, 3, 'way', 'say'}

Common questions

Powered by AI

Attempting to convert a float (e.g., 17.0) to a non-iterable type such as list, tuple, set, or dict results in a TypeError with the message 'float object is not iterable' . Similarly, trying to convert a complex number (e.g., 10+7j) to a non-iterable type also results in a TypeError, but the message specifies 'complex object is not iterable' . The underlying reason is that neither float nor complex numbers are iterable by nature, unlike strings or lists.

Attempting to convert a single character string to a dictionary raises a ValueError, indicating a 'malformed string' . Dictionaries require key-value pairs to be initialized and cannot be directly mapped from a single element. This reinforces the structured nature of dictionaries which mandates a consistent mapping format, preventing ambiguous or unintended data structures even from potentially iterable inputs.

In Python, converting booleans True and False to primitive data types like int or float results in integer representations 1 and 0, respectively, and float representations 1.0 and 0.0, respectively. However, converting booleans to non-primitive types such as list, tuple, set, or dict raises a 'bool object is not iterable' TypeError . This is because booleans are not iterable, unlike sequences or mappings.

Complex numbers consist of a real and an imaginary part. Direct conversion to primary numeric types such as int or float is not possible because this action would require disregarding the imaginary component entirely, leading to a loss of information . Consequently, Python raises a TypeError to prevent implicit data loss, enforcing only conversions that preserve the complete numerical value.

Python converts boolean True to 1 when interpreted as an integer or float, and as (1+0j) when interpreted as a complex number. Similarly, False becomes 0 or 0.0 and 0j . This automatic interpretation implies that when using booleans in numeric contexts, Python treats them as basic numeric types, seamlessly collaborating with complex numbers since booleans essentially represent simple numerical values (1, 0).

Python allows conversion of lists and tuples to sets because these structures are inherently iterable, and elements can be directly inserted into a set, which is inherently unordered. However, dictionaries are mappings with key-value pairs. Converting a dictionary to a set using 'dict(d)' results in a TypeError because the attempt to retrieve the key-value pairs as sequences of length 2 fails; dictionaries are expected to be updated through a sequence of key-value tuples .

Converting lists and dictionaries to integers or floating-point numbers is disallowed because these data types are collections and not inherently representable as single numeric values. Lists contain ordered sequences of elements, and dictionaries contain key-value pairs, neither of which can inherently represent a singular numeric state without a defined or expected schema, leading to TypeErrors . This highlights the necessity for explicit formats or a mapping process (such as a length or specific key retrieval) when such conversions are required.

Python allows conversion of strings to lists or tuples because strings are sequences of characters. Each character can be iterated over, making them inherently compatible with the iterable nature of lists and tuples. This compatibility ensures each character of the string becomes an element of the resultant list or tuple . This reflects Python's design philosophy of leveraging inherent relationships among data types to allow useful transformations.

When attempting to convert a string (e.g., 'hello') directly to an integer or float, a ValueError occurs. Specifically, the message for int conversion is 'invalid literal for int() with base 10' , and for float conversion, it is 'could not convert string to float' . This indicates that strings must represent valid numeric literals to be compatible with numeric types during conversion.

When attempting to convert a list to a dictionary in Python, a TypeError occurs indicating the inability to convert elements to a sequence usable as dictionary key-value pairs. Lists must contain pairs, which are explicitly convertible to key-value pairs in a dictionary. Without this format, such a conversion is nonsensical, thus, it fails with a TypeError .

You might also like