Skip to main content

Introduction to Python Data Types

Python Data Types

Python provides several built-in data types that are used to represent different kinds of information.

Here are some of the commonly used data types in Python:

Numeric Types

  • int: Represents integers (whole numbers) like 5, -10, 100.
  • float: Represents floating-point numbers with decimal values like 3.14, -2.5, 0.75.
  • complex: Represents complex numbers in the form a + bj, where a and b are real numbers and j represents the imaginary unit.
# int
x = 5
y = -10

# float
pi = 3.14
temperature = -2.5

# complex
z = 2 + 3j
w = -1j

Text Type:

  • str: Represents strings of characters enclosed in single quotes (') or double quotes ("). Strings can contain letters, digits, symbols, and whitespace.
# str
name = 'John'
message = "Hello, World!"

Boolean Type

  • bool: Represents a boolean value that can be either True or False. Boolean values are used in conditional statements and logical operations.
# bool
is_true = True
is_false = False

Sequence Types

  • list: Represents an ordered and mutable collection of items. Lists are enclosed in square brackets ([]) and can contain elements of different types.
  • tuple: Represents an ordered and immutable collection of items. Tuples are enclosed in parentheses (()) and can contain elements of different types.
  • range: Represents an immutable sequence of numbers. Ranges are commonly used in loops and generate a sequence of numbers based on the specified start, stop, and step values.
# list
fruits = ['apple', 'banana', 'orange']
numbers = [1, 2, 3, 4, 5]

# tuple
coordinates = (10, 20)
person = ('John', 25)

# range
countdown = range(10) # Generates numbers from 0 to 9

Mapping Type

  • dict: Represents an unordered collection of key-value pairs. Dictionaries are enclosed in curly braces ({}) and are used to store and retrieve values based on unique keys.
# dict
student = {'name': 'Alice', 'age': 20}
scores = {'math': 90, 'science': 85, 'history': 92}

Set Types

  • set: Represents an unordered collection of unique elements. Sets are enclosed in curly braces ({}) and can be created by passing iterable objects or using the set() constructor.
  • frozenset: Similar to sets, frozensets represent an immutable collection of unique elements.
# set
unique_numbers = {1, 2, 3, 4, 5}

# frozenset
immutable_set = frozenset({1, 2, 3})

How to check data types

You can use the built-in type() function to check the data type of a variable or a value.

The type() function returns the type of the specified object.

Here's how you can use it:

x = 5
y = 3.14
name = "John"
is_true = True
fruits = ['apple', 'banana', 'orange']
student = {'name': 'Alice', 'age': 20}

print(type(x)) # <class 'int'>
print(type(y)) # <class 'float'>
print(type(name)) # <class 'str'>
print(type(is_true)) # <class 'bool'>
print(type(fruits)) # <class 'list'>
print(type(student)) # <class 'dict'>

In the above example:

  • The type() function is used to determine the data type of each variable. The output shows the respective data types enclosed in <class '...'>.

You can also use the isinstance() function to check if an object belongs to a specific data type. It returns True if the object is an instance of the specified type, and False otherwise.

As an example:

x = 5
if isinstance(x, int):
print("x is an integer")
else:
print("x is not an integer")

In this case:

  • The isinstance() function checks if x is an instance of the int type and prints the corresponding message.