In this Python tutorial, we will discuss how to fix “typeerror and attributeerror” in python. We will check:
- TypeError: ‘list’ object is not callable.
- TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’.
- Python object has no attribute
- TypeError: python int object is not subscriptable
TypeError: ‘list’ object is not callable
In python, we get this error when we pass the argument inside the print statement, the code contains the round bracket to print each item in the list due to which we get this typeerror.
Example:
my_list = ["Kiyara", "Elon", "John", "Sujain"]
for value in range(len(my_list)):
print(my_list(value))
After writing the above code, Ones you will print “my_list(value)” then the error will appear as a “ TypeError: ‘list’ object is not callable ”. Here, this error occurs because we are using the round bracket which is not correct for printing the items.
You can see the below screenshot for this typeerror in python
To solve this python typeerror we have to pass the argument inside the square brackets while printing the “value” because the list variable works in this way.
Example:
my_list = ["Kiyara", "Elon", "John", "Sujain"]
for value in range(len(my_list)):
print(my_list[value])
After writing the above code, Ones you will print “ my_list[value] ” then the output will appear as a “ Kiyara Elon John Sujain ”. Here, the error is resolved by giving square brackets while printing.
You can refer to the below screenshot how typeerror is resolved.
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
We get unsupported operand type(s) for +: ‘int’ and ‘str’ error when we try to add an integer with string or vice versa as we cannot add a string to an integer.
Example:
a1 = 10
a2 = "5"
s = a1 + a2
print(s)
After writing the above code, Ones you will print “(s)” then the error will appear as a “ TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’ ”. Here, this error occurs because we are trying to add integer and string so it returns an error.
You can see the below screenshot typeerror: unsupported operand type(s) for +: ‘int’ and ‘str’ in python
To solve this python typeerror we have to convert the string value to an integer using the int() method so, in this way we can avoid this error.
Example:
a1 = 10
a2 = "5"
s = a1 + int(a2
)
print(s)
After writing the above code, Ones you will print “ (s) ” then the output will appear as a “ 15 ”. Here, the error is resolved by converting the value of a2 to an integer type, and then it added two values.
You can refer to the below screenshot of how unsupported operand type(s) for +: ‘int’ and ‘str’ is resolved.
Python object has no attribute
In python, we get this attribute error because of invalid attribute reference or assignment.
Example:
a = 10
a.append(20)
print(a)
After writing the above code, Ones you will print “a” then the error will appear as an “ AttributeError: ‘int’ object has no attribute ‘append’ ”. Here, this error occurs because of invalid attribute reference is made and variable of integer type does not support append method.
You can see the below screenshot for attribute error
To solve this python attributeerror we have to give a variable of list type to support the append method in python, so it is important to give valid attributes to avoid this error.
Example:
roll = ['1','2','3','4']
roll.append('5')
print('Updated roll in list: ',roll)
After writing the above code, Ones you will print then the output will appear as an “Updated roll in list: [‘1’, ‘2’, ‘3’, ‘4’, ‘5’] ”. Here, the error is resolved by giving the valid attribute reference append on the list.
You can refer to the below screenshot how attributeerror is resolved.
TypeError: python int object is not subscriptable
This error occurs when we try to use integer type value as an array. We are treating an integer, which is a whole number, like a subscriptable object. Integers are not subscriptable object. An object like tuples, lists, etc is subscriptable in python.
Example:
v_int = 1
print(v_int[0])
- After writing the above code, Once you will print “ v_int[0] ” then the error will appear as a “ TypeError: ‘int’ object is not subscriptable ”.
- Here, this error occurs because the variable is treated as an array by the function, but the variable is an integer.
- You can see we have declared an integer variable “v_int” and in the next line, we are trying to print the value of integer variable “v_int[0]” as a list. Which gives the error.
You can see the below screenshot for typeerror: python int object is not subscriptable
To solve this type of error ‘int’ object is not subscriptable in python, we need to avoid using integer type values as an array. Also, make sure that you do not use slicing or indexing to access values in an integer.
Example:
v_int = 1
print(v_int)
After writing the above code, Once you will print “ v_int ” then the output will appear as “ 1 ”. Here, the error is resolved by removing the square bracket.
You can see the below screenshot for typeerror: python int object is not subscriptable
You may like the following Python tutorials:
- Python if else with examples
- Python For Loop with Examples
- Python read excel file and Write to Excel in Python
- Create a tuple in Python
- Python create empty set
- Python Keywords with examples
- Python While Loop Example
- String methods in Python with examples
- NameError: name is not defined in Python
- Python check if the variable is an integer
This is how to fix python TypeError: ‘list’ object is not callable, TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’, AttributeError: object has no attribute and TypeError: python int object is not subscriptable
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.