Introduction
Key Concepts Explanation
Dynamic Typing
Python variables or objects are dynamically typed, meaning data types are inherited based on the values assigned to the variables. Unlike statically typed languages, Python does not enforce data types until runtime.
i = 10
type(i) == int
Specifying Data Types
Starting from Python 3, we can specify data types for variables or objects, but it is informational and not enforced.
j: int = 10 # Data type specified as int
j: int = 'Hello'
print(j)
type(j)
Hands-On Tasks
- Declare a variable without specifying a data type and assign a value to it. Check the type.
- Declare a variable with a data type specified and assign a value of a different type. Check the type.
Conclusion
In Python, variables and objects are dynamically typed, and data types can be specified but are not enforced. Remember to check the type of variables and objects using the type
function to understand their data type. Practice declaring variables with and without specifying data types to familiarize yourself with Python’s dynamic typing.