Data Types in Python (Python Data Type)

Python provides various data types that help define a variable's value. In this guide, we’ll explore how many data types in Python and their functionality. If you're new to Python, consider starting with an introductory Python tutorial. For those already familiar, you might also find our earlier discussion on Python Comments and Statements useful.

Basic Data Types in Python

Python offers several built-in data types, which can be broadly categorized as follows:

  • Text Type: str
  • Numeric Types: int, float, complex
  • Sequence Types: list, tuple, range
  • Binary Types: bytes, bytearray, memoryview
  • Mapping Type: dict
  • Boolean Type: bool
  • Set Types: set, frozenset

These basic data types in Python form the foundation for handling and manipulating data in Python programs.

Python Data Type: String

Python data type string is a sequence of characters, and it supports Unicode, allowing for a wide range of characters. Strings can be defined using either single or double quotes.

x = "This is a double-quoted string"

y = 'This is a single-quoted string'

print(x)

print(y)

# Using ',' to join multiple strings

print(x, "joined with", y)

# Using '+' to concatenate multiple strings

print(x + " and " + y + " are combined")

This code will produce output similar to the following:

Output:

data types in python (python data type)

Python Data Type: Numeric

Numeric Python data types are used to store numbers, including integers, floating-point numbers, and complex numbers. Here’s a breakdown of these types:

  • int: Represents whole numbers of unlimited length, both positive and negative.
  • long: Used for long integers in Python 2.x (removed in Python 3.x).
  • float: Stores decimal or floating-point numbers, precise up to 15 decimal places.
  • complex: Represents numbers with both real and imaginary parts.

Unlike languages like C or C++, Python does not require explicit data type declarations when creating variables. You can directly assign a value to a variable, and Python will automatically determine its type. 

To check the type of a numeric variable, you can use the type() function, as shown below:

number = 10.5

print(type(number))

Output:

data types in python (python data type)

Python Data Type: List

A list in Python is a flexible and powerful data type. Unlike arrays in C or C++, Python data type lists can store elements of different data types at the same time. Lists are ordered collections of items, created using square brackets ([]) with elements separated by commas (,).

# List containing only integers

numbers = [1, 2, 3, 4, 5]

print("List of integers:", numbers)

# List containing only strings

fruits = ["apple", "banana", "cherry"]

print("List of strings:", fruits)

# List with mixed data types

mixed_list = ["hello", 42, True, 3.14]

print("List with mixed data types:", mixed_list)

# Accessing elements using an index (0-based indexing)

print("First element in numbers:", numbers[0]) # Output: 1

print("Second element in fruits:", fruits[1]) # Output: banana

# Modifying an element in a list

numbers[2] = 99

print("Modified list of integers:", numbers)

# Adding elements to a list

numbers.append(6)

print("List after adding an element:", numbers)

# Removing an element from a list

numbers.remove(99)

print("List after removing an element:", numbers)

Output:

data types in python (python data type)

Python Data Type: Tuples

A Python data type tuple is a sequence of elements, much like a list, but it is immutable, meaning its content cannot be changed after creation. Tuples are defined using parentheses (()) with elements separated by commas (,).

# Tuple containing only integers

numbers = (10, 20, 30, 40)

print("Tuple of integers:", numbers)

# Tuple containing only strings

fruits = ("apple", "banana", "cherry")

print("Tuple of strings:", fruits)

# Tuple with mixed data types

mixed_tuple = ("hello", 42, True, 3.14)

print("Tuple with mixed data types:", mixed_tuple)

# Accessing elements using an index (0-based indexing)

print("First element in numbers:", numbers[0]) # Output: 10

print("Second element in fruits:", fruits[1]) # Output: banana

# Tuple containing nested tuples

nested_tuple = (1, (2, 3), (4, 5, 6))

print("Nested tuple:", nested_tuple)

print("Access nested tuple element:", nested_tuple[1][1])

Output:

data types in python (python data type)

Python Data Type: Dictionary

A dictionary in Python is an unordered collection of data organized as key-value pairs. Similar to a hash table, it allows quick and efficient data retrieval. Dictionaries are defined using curly braces ({}), with each pair represented as key: value. They are especially useful for managing and accessing large datasets efficiently.

# A sample dictionary variable

info = {"name": "Alice", "city": "Wonderland", "age": 25}

# Print value associated with the key "name"

print(info["name"]) # Output: Alice

# Print value associated with the key "city"

print(info["city"]) # Output: Wonderland

# Print value associated with the key "age"

print(info["age"])

Output:

data types in python (python data type)

Conclusion

We've discussed the what are data types in Python and their uses. Be sure to practice each code example on your system, and avoid simply copying and pasting—writing the code yourself helps reinforce your learning. Explore further by referring to the official Python documentation on data types.

Unlock lightning-fast hosting with 10GBVPS—unlimited bandwidth, global locations, and unmatched performance for your website!

Blog