Python: Strongly Typed

When observing Python code it may appear to be loosely typed. If a person considers variable declaration, they may consider Python as loosely typed because a variable can be reassigned to a different type.

%MINIFYHTML456a277baddf6f4d6bb9f8909dd46a6123%
length = 30 length = "30"
Code language: JavaScript (javascript)

The above is valid in Python, as the variable has been reassigned from an integer to a string.

However, Python will not allow casting of an int into a string, nor will it allow the passing of invalid types of numbers:

# below will fail as the integer can't be cast into the string as is: print("My name is Jason and I am " + 8 + " years old") # below will fail because a float can not be used in this integer operation: for i in range(1, 4.5): print(i)
Code language: PHP (php)

While Python is not as strongly typed as Java or C, it does have restrictions on variable types (as seen above.)

About Author /

Leave a Comment

Your email address will not be published.

Start typing and press Enter to search