44
2 A Few More Steps
Python is Both Dynamically and Strongly Typed
Python is a dynamically typed language, since a certain variable name may
refer to objects of different types during the execution of a program. This
means that writing
In [1]: z = 10
# z refers to an integer
In [2]: z = 10.0
# z refers to a float
In [3]: z = ’some string’
# z refers to a string
is perfectly fine and we get no error messages. In statically typed languages
(e.g., C and Fortran), this would not be accepted, since a variable then would
have to be of one particular type throughout.
Python is also a strongly typed language, since it is strict about how you
can combine object types:
In [1]: ’yes’ + ’no’
# add two strings
Out[1]: ’yesno’
In [2]: ’yes’ + 10
# ...try adding string and integer
Traceback (most recent call last):
File "", line 1, in
’yes’ + 10
TypeError: cannot concatenate ’str’ and ’int’ objects
Adding two strings is straight forward, but trying the same with a string
and an integer gives an error message, since it is not well defined.
In a weakly typed language, this could very well give yes10 without
any error message. If so, it would be based on a (vaguely) founded
assumption that this is what the programmer intended (but perhaps it
is just as likely an error?). For more details on these matters, see, e.g.,
https://en.wikipedia.org/wiki/Python_(programming_language).
2.2.6 Operator Precedence
When the arithmetic operators +, -, *, / and ** appear in an expression, Python
gives them a certain precedence. Python interprets the expression from left to right,
taking one term (part of expression between two successive + or -) at a time. Within
each term, ** is done before * and /.
Consider the expression x = 1*5**2 + 10*3 - 1.0/4. There are three terms
here and interpreting this, Python starts from the left. In the first term, 1*5**2, it
first does 5**2 which equals 25. This is then multiplied by 1 to give 25 again. The
second term is 10*3, i.e., 30. So the first two terms add up to 55. The last term gives
0.25, so the final result is 54.75 which becomes the value of x.
2 A Few More Steps
Python is Both Dynamically and Strongly Typed
Python is a dynamically typed language, since a certain variable name may
refer to objects of different types during the execution of a program. This
means that writing
In [1]: z = 10
# z refers to an integer
In [2]: z = 10.0
# z refers to a float
In [3]: z = ’some string’
# z refers to a string
is perfectly fine and we get no error messages. In statically typed languages
(e.g., C and Fortran), this would not be accepted, since a variable then would
have to be of one particular type throughout.
Python is also a strongly typed language, since it is strict about how you
can combine object types:
In [1]: ’yes’ + ’no’
# add two strings
Out[1]: ’yesno’
In [2]: ’yes’ + 10
# ...try adding string and integer
Traceback (most recent call last):
File "
’yes’ + 10
TypeError: cannot concatenate ’str’ and ’int’ objects
Adding two strings is straight forward, but trying the same with a string
and an integer gives an error message, since it is not well defined.
In a weakly typed language, this could very well give yes10 without
any error message. If so, it would be based on a (vaguely) founded
assumption that this is what the programmer intended (but perhaps it
is just as likely an error?). For more details on these matters, see, e.g.,
https://en.wikipedia.org/wiki/Python_(programming_language).
2.2.6 Operator Precedence
When the arithmetic operators +, -, *, / and ** appear in an expression, Python
gives them a certain precedence. Python interprets the expression from left to right,
taking one term (part of expression between two successive + or -) at a time. Within
each term, ** is done before * and /.
Consider the expression x = 1*5**2 + 10*3 - 1.0/4. There are three terms
here and interpreting this, Python starts from the left. In the first term, 1*5**2, it
first does 5**2 which equals 25. This is then multiplied by 1 to give 25 again. The
second term is 10*3, i.e., 30. So the first two terms add up to 55. The last term gives
0.25, so the final result is 54.75 which becomes the value of x.
