Is Python considered a strongly or weakly typed language?

Is Python strongly typed?

I’ve seen articles claiming that Python is a strongly typed language, but I’m a bit confused. For example, in the following code:

bob = 1
bob = "bob"

The variable bob changes types during runtime. I thought that in strongly typed languages, you couldn’t change types like this at runtime. Maybe my understanding of strong vs weak typing is too simplistic.

So, is Python strongly typed or weakly typed?

Having worked with Python for a while, I can tell you that Python is strongly typed and dynamically typed. Let me break it down:

  • Strong typing means variables in Python don’t automatically change type. For instance, if a variable holds a string, it won’t suddenly change to a number just because it contains digits. Any type change needs to be intentional.
  • Dynamic typing means the type of a variable is determined at runtime, not during compilation.

Here’s a quick example:

bob = 1
bob = "bob"

In this case, bob first holds an integer and later a string. Python is flexible here because variables aren’t bound to a specific type — it’s their value that dictates the type. You can always check the type at runtime with type(bob).

This is different from statically typed languages like C++ where types are fixed at compile time. Python allows the value to change dynamically while maintaining a strong type system at runtime.

Exactly, @akanshasrivastava.1121! Just adding to that, Python is strongly typed, but it’s also dynamically typed, which makes it a bit more flexible than some other languages. What I mean is, Python maintains strict rules when it comes to type compatibility during runtime. So, if you try to add a string to an integer, Python will stop you right away by raising an error. This keeps things predictable even in a dynamically typed environment.

For example:

bob = 1
bob = "bob"

Notice here, bob is reassigned from an integer to a string, and this works because Python variables don’t have an inherent type. The type is assigned to the object itself, not the variable. Initially, bob points to an integer, but later, it points to a string. This flexibility is key to Python’s design, which makes it both strongly typed and dynamic.

Great points, @prynka.chatterjee! I totally agree. To elaborate further, Python is strongly typed, meaning it ensures that types are strictly enforced during runtime. The cool thing is that you don’t have to declare types up front, but Python will still make sure that operations between incompatible types won’t go unnoticed.

For example:

bob = 1
bob = "bob"

Here, bob can hold any type because Python doesn’t tie variables to specific types. But, if you were to try combining incompatible types — like adding an integer and a string — Python will immediately raise an error, showing that the language is strongly typed.

Unlike statically typed languages, Python’s approach gives you both flexibility and safety, which is why it’s loved by many developers.