Member-only story
Python 101 — Type Error, Type Checking, and Type Conversion; Mathematical Operations
One of the initial things you’ll notice as you venture into coding is how data is categorized into different “types.” Misunderstanding or misusing these types can lead to type errors, but fortunately, Python provides tools and techniques to both prevent and handle these issues. 💪
Type Error
In Python, a TypeError is raised when an operation or function is applied to an object of an inappropriate type.
Python is dynamically-typed, meaning that you don’t have to declare a variable’s type ahead of time. However, it’s strongly-typed, meaning once a variable has a type (like a string or an integer), you can’t just use it any way you want.
# This will raise a TypeError because you can't add a string to an integer
result = "Hello" + 5
# This will raise a TypeError because you can't iterate over an integer
for i in 5:
print(i)
Type Checking
Type checking is the process of verifying and enforcing the constraints of types. It can be done either at compile-time (in languages like Java) or at runtime (in languages like Python).
Type checking ensures that operations or actions are being done on compatible types. In a…