DEV Community

Kevin Naidoo
Kevin Naidoo

Posted on

Python is strongly typed?

Speak to almost any TypeScript developer and they are obsessed with types. This is a good thing, types are important and are a good safety layer to prevent silly bugs.

In JavaScript you can do this:

x = 1
y = "cc"
result = x+y
Enter fullscreen mode Exit fullscreen mode

The result would be "1cc". This is bad, very bad. It means JavaScript is weakly typed and the chances of creating silly runtime bugs are much higher.

This is why TypeScript is so important and useful.

Python on the other hand has type safety, just try running the above code in Python. You will get an exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Enter fullscreen mode Exit fullscreen mode

Python is strongly typed, however, it is "dynamically typed" as well. This means you can assign variables without types and you can overwrite variables with a different type.

C# for example is strongly typed and statically typed. Like Python, C# will complain if you try to add strings and integers together.

In Addition, C# enforces declaring types up front. Thus, if you storing a float in a variable, you have to:

  1. Declare the variable with a type.
  2. Maintain the type for its lifetime. This means you can't change a variable declared as a float to a string.

Note: C# does support the "var" keyword, which allows the compiler to detect the type automatically based on the variable's data. This is known as "Implicit typing".

Top comments (2)

ryanlwh profile image
Ryan Lee

I think you meant result = x+y in your first example.

kwnaidoo profile image
Kevin Naidoo

Thanks! good spot and much appreciated. Fixed.