Dynamically Typed Language

Image by Alexa from Pixabay

Popular dynamic programming languages include JavaScript, Python, Ruby, PHP, Lua and Perl. Popular statically typed languages include Java, Scala, Haskell, C, C++, C#, Scala, Kotlin, Fortran, Go, Pascal, and Swift. Python is a dynamically typed language. What does dynamically typed mean?
In this post let’s look into that. Of course you can Google the definition but first let’s see how to add two numbers in a few languages.

Statically typed language
Java
int x = 5;
int y = 6;
int sum = x + y;
System.out.println(sum); // sum will be 11 (an integer)
C++
int x = 10;
int y = 20;
int z = x + y; // z will be 30 (an integer)
C#
int x = 10;
int y = 20;
int z = x + y; // z will be 30 (an integer/number)

Scala
var a: Int = 10;
var b: Int = 10;
var sum = a+b  // sum will be 20 ( an integer)

Dynamically typed language
JavaScript
let x = 5;
let y = 2;
let z = x + y;  // Z will be 7
Python
x = 5
y = 10
print(x + y) # result will be 15

Notice anything? Did you notice that in all statically typed languages you have mentioned the data type int? But in Python and JavaScript you have not done it. Because in Python you don’t have to mention the data type like in Java.

Python is dynamically typed (it keeps track of types for you automatically instead of requiring declaration code), but it is also strongly typed (you can perform on an object only operations that are valid for its type). – Learning python by Mark Lutz

I read a little bit more and found a description which led me to another interesting insight “Why Is Dynamic Typing So Often Associated with Interpreted Languages?”
If you read this post, there I mentioned that a computer hardware can understand only low-level language. If we want the computer to understand out commands/a high-level language, it can either be compiled or interpreted.
A compiler is a complex computer program that takes another program written in a high-level language and translates it into an equivalent program in the machine language of some computer. The high-level program is called source code, and the resulting machine code is a program that the computer can directly execute.

An interpreter is a program that simulates a computer that understands a high-level language. Rather than translating the source program into a machine language equivalent, the interpreter analyzes and executes the source code instruction by instruction as necessary.
The standard implementations of Python today compile source code statements to an intermediate format known as byte code and then interpret the byte code. Byte code provides portability, as it is a platform-independent format. However, Python is not compiled all the way down to binary machine code.
So the assumption is that the authors of PHP, Perl, Python, Ruby, Lua, etc. designed dynamic languages, and implemented them using interpreters. They did this because interpreters are much easier to write than compilers.

Reference:

difference between statically typed and dynamically typed languages

why is dynamic typing so often associated with interpreted languages

Learning python by Mark Lutz