“Type” of a Python Variable
We'll cover the following
Type refers to the kind of data being defined. For instance, this could be numerical or alphabetic, etc.
Checking the Type of a variable
Python understands user intent and sets the type of the variable accordingly. Let’s explore the types of the defined variables by using the type()
method.
moving_average = 10.2name_of_language = "Python"main_languages = ['Python', 'JavaScript', 'C++', 'Java', 'GO']print(type(moving_average))print(type(name_of_language))print(type(main_languages))
Changing the Type of a variable
Variables can be reassigned as needed, and the redefined variable then contains the updated value and its type. Let’s reassign a string to the moving_average
variable, which previously consisted of a floating point number.
moving_average = "ten point five"print(type(moving_average))