4.2. Data typing#
Interactive page
This is an interactive book page. Press the launch button at the top right side.
In the previous section, you’ve learned about various types of variables in Python. From writing your own lines of code, you may have already noticed some features of Python when working with different data types. In fact, based on the way a programming languague handles variable types, we make the following dinstinctions between different languages:
Strongly vs. weakly typed language.
Dynamically vs. statically typed language.
Data typing in Python is strong and dynamic. Let’s explore what that means.
4.2.1. Strong and weak typing#
Strong and weak typing refer to how strictly a programming language enforces a type. In a strongly typed language, such as Python, once a variable is declared to be of a given type (e.g., string), then it’s bound to that type. Because types are strictly enforced, you have to do explicit conversion if you wish to change a variable type, e.g., if you want to make your string into an integer, you must explicitly tell Python to do so. Otherwise, you will encounter a bug. The features of strongly typed languages are:
Type enforcement: operations between incompatible types are not allowed without explicit conversion.
Type checking: type errors are raised at runtime if your attempt to perform an operation on incompatible types. Let’s see what this means in practice - try to run the code below. What do you observe?
a = 5
b = "10"
c = a + b
In this example, we tried to sum up an integer (a
) and a string (b
), so Python raised an error.
In other words, Python won’t convert the string “10” into an integer 10 behind the scenes.
If you want it to do so, you must give Python explicit instructions using the built-in int()
function:
a = 5
b = "10"
c = a + int(b)
print(c)
Unlike strongly typed languages like Python, there are also weakly typed languages, such as C or JavaScript. In these languages, type rules are more relaxed - they automatically perform implicit type conversion (also known as coercion). The implicit conversion of types, where types are more flexible, leads to a more fluid code. However, it may also introduce subtle bugs that are difficult to catch.
4.2.2. Dynamic and static typing#
In dynamically typed languages, such as Python, types of variables are determined at runtime rather than at compile time. In practice, this means that you don’t have to declare (define) a variable type when you create a variable. Instead, the interpreter infers the type of each variable based on the value you assigned to it.
In this example code, Python will infer that a
is an integer and b
is a string (strings are recognized by the use of quotation marks " "
).
Add new lines of code in the cell below to verify that this is indeed true.
Recall that you can use type()
function (e.g., type(a)
to verify the type of a
).
a = 5
b = "10"
The features of dynamically typed languages are:
Type inference: types are inferred based on the value assigned to a variable.
Type flexibility: variables can change type.
In a statically typed language, such as Java, type is determined at compile time.
In practice, this means that each variable has to be explicitly declared before being used, and type cannot be changed throughout the program.
For instance, before assigning a value of 5 to variable a
, in Java you would need to declare that a
is going to be an integer.
import micropip
await micropip.install("jupyterquiz")
from jupyterquiz import display_quiz
import json
with open("questions1.json", "r") as file:
questions=json.load(file)
display_quiz(questions, border_radius=0)