
Learn Python Programming
By :

Python is both a strongly typed and a dynamically typed language.
Strongly typed means that Python does not allow implicit type conversions that could lead to unexpected behaviors. Consider the following php code:
<?php
$a = 2;
$b = "2";
echo $a + $b; // prints: 4
?>
In php, variables are prepended with a $
sign. In the above code, we set $a
to the integer number 2
, and $b
to the string "2"
. To add them together, php performs an implicit conversion of $b
, from string to integer. This is referred to as type juggling. This might seem a convenient feature, but the fact that php is weakly typed has the potential to lead to bugs in the code. If we tried to do the same in Python, the result would be much different:
# example.strongly.typed.py
a = 2
b = "2"
print(a + b)
Running the above produces:
$ python ch12/example.strongly.typed.py
Traceback (most recent call last):
File "ch12/example.strongly...