Python is dynamically typed, which means a variable can store any type, and that type can change.
For example, if I make a number variable, I can later change that variable to a string:
speed = 5
speed = "five"
In almost all circumstances, it's a bad idea to change the type of a variable. The "proper" thing to do is to just create a new one. For example:
speed = 5
speed_description = "five"
Languages that aren't dynamically typed are statically typed, such as Go (which you'll learn in a later course). In a statically typed language, if you try to assign a value to a variable of the wrong type, an error would crash the program.
If Python were statically typed, the first example from before wouldn't allow the second line, speed = "five"
. The computer would give an error along the lines of you can't assign a string value ("five") to a number variable (speed)
.
Is changing the type of a variable generally a good idea?