Python: Printing the Type of a Variable and Getting Variable Type

Estimated read time 3 min read

Introduction to Variable Types in Python

In Python, variables are used to store data values. Each variable has a specific type that determines the kind of data it can hold and the operations that can be performed on it. Understanding variable types and being able to determine the type of a variable is essential for writing robust and error-free Python code. In this article, we will explore how to print the type of a variable in Python and discuss the different variable types available.

Determining the Type of a Variable

Using the type() Function

In Python, you can use the built-in type() function to determine the type of a variable. The type() function takes an object as an argument and returns the type of that object. Here’s an example:

number = 10
print(type(number))  # Output: <class 'int'>

In this example, the type() function is used to determine the type of the number variable, which is an integer (int).

Common Variable Types in Python

Python supports several built-in variable types, each serving a specific purpose. Here are some of the most commonly used variable types:

Numeric Types

  1. int: Represents integer values, such as 10, -5, or 0.
  2. float: Represents floating-point or decimal values, such as 3.14, 1.23e-5, or -2.0.
  3. complex: Represents complex numbers in the form a + bj, where a and b are real numbers and j represents the imaginary unit.

Sequence Types

  1. str: Represents strings, which are sequences of characters enclosed in quotes (' or "). For example, 'Hello' or "World".
  2. list: Represents ordered collections of items enclosed in square brackets ([]). Lists can contain elements of different types and can be modified.
  3. tuple: Similar to lists, tuples represent ordered collections of items enclosed in parentheses (()). However, unlike lists, tuples are immutable, meaning their elements cannot be modified once defined.

Mapping Type

  1. dict: Represents key-value pairs enclosed in curly braces ({}). Dictionaries allow you to associate a value with a unique key, enabling efficient lookup and retrieval of data.

Set Types

  1. set: Represents an unordered collection of unique elements enclosed in curly braces ({}). Sets can be used for membership testing and to eliminate duplicate values.
  2. frozenset: Similar to sets, frozensets are immutable sets that cannot be changed once defined.

Boolean Type

  1. bool: Represents boolean values, which can be either True or False. Booleans are commonly used for logical operations and conditional statements.

Printing the Type of a Variable

Using the print() Function

The print() function in Python allows you to display the type of a variable along with other information or messages. Here’s an example:

name = "John"
age = 30

print("Name:", name, "Type:", type(name))
print("Age:", age, "Type:", type(age))

In this example, the print() function is used to display the name, age, and their corresponding types. The type() function is used within the print() function to determine the types.

Type Conversion

Implicit Type Conversion

Angelika Card

Hi all, my name is Angelika and I am one of the authors of the EasyTechh website. Like the rest of our team I am incredibly ambitious and I love helping people.
That's why I write here and not only here ;-) I write interesting and useful for people articles in the IT sphere and a little bit about life.
Enjoy reading.

You May Also Like

More From Author

+ There are no comments

Add yours