What are Python data types? List some built-in data types in Python.

Farihatulmaria
5 min readMay 20, 2024

In Python, data types represent the type of data that can be manipulated and processed by the interpreter. Python supports several built-in data types that are used to store and represent different kinds of information. Here are some of the key built-in data types in Python along with detailed explanations and examples:

1. Numeric Types:

a. Integer (int):

Represents whole numbers without any decimal point.

c. Complex (complex):
Represents numbers with a real and imaginary part.

b. Float (float):

Represents numbers with a decimal point.

pi = 3.14159
temperature = -10.5

c. Complex (complex):

Represents numbers with a real and imaginary part.

z = 2 + 3j

2. Sequence Types:

a. String (str):

Represents a sequence of characters enclosed within single, double, or triple quotes.

message = "Hello, world!"

b. List (list):

Represents an ordered collection of items that can be of different data types.

numbers = [10, 20, 30, 40]
fruits = ["apple", "banana", "orange"]

c. Tuple (tuple):

Represents an ordered and immutable collection of items.

coordinates = (10, 20)
colors = ("red", "green", "blue")

3. Mapping Type:

Dictionary (dict):

Represents a collection of key-value pairs enclosed within curly braces {}.

student = {"name": "Alice", "age": 25, "grade": "A"}

4. Set Types:

a. Set (set):

Represents an unordered collection of unique items enclosed within curly braces {}

Example 3: Mapping Type (Dictionary)

b. Frozen Set (frozenset):

Represents an immutable set, similar to set but cannot be modified after creation.

weekdays = frozenset({'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'})

5. Boolean Type:

Boolean (bool):

Represents a boolean value (True or False).

is_active = True
is_adult = False

6. None Type:

None (NoneType):

Represents a null or empty value.

result = None

Examples Demonstrating Python Data Types:

Example 1: Numeric Types

# Numeric types: int, float, complex
x = 10
y = 3.14
z = 5 + 2j

Example 2: Sequence Types (String, List, Tuple)

# String
name = "Alice"
# List
numbers = [10, 20, 30]
# Tuple
coordinates = (5, 10)

Example 3: Mapping Type (Dictionary)

# Dictionary
student = {"name": "Bob", "age": 30, "grade": "B"}

Example 4: Set Types (Set, Frozen Set)

# Set
colors = {'red', 'green', 'blue'}
# Frozen set
vowels = frozenset({'a', 'e', 'i', 'o', 'u'})

Example 5: Boolean Type

# Boolean
is_active = True
is_adult = False

Example 6: None Type

# NoneType
result = None

Notes on Data Types in Python:

  • Python is dynamically typed, meaning you don’t need to specify the data type explicitly while declaring variables. The interpreter determines the type based on the value assigned.
  • Python supports type conversion (casting) between different data types using built-in functions (e.g., int(), float(), str()).
  • Each data type in Python has its own set of operations and methods for manipulation. For example, strings have methods like upper(), lower(), and lists have methods like append(), pop().

Understanding and utilizing these built-in data types is fundamental to writing effective Python code. Choosing the appropriate data type based on the requirements of your program can improve performance, readability, and maintainability of your code.

Let’s explore Python’s built-in data types in more detail and discuss additional aspects, including type conversion, type checking, and common operations associated with each data type.

Additional Details on Python Data Types:

1. Numeric Types (int, float, complex):

  • Operations: Python supports arithmetic operations such as addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (**) on numeric types.
# Numeric operations
x = 10
y = 3.5
z = 2 + 3j

sum_result = x + y # Addition of int and float
product_result = x * z # Multiplication of int and complex

2. Sequence Types (str, list, tuple):

  • Indexing and Slicing: Sequence types support indexing and slicing to access elements or subsequences.
# String indexing and slicing
message = "Hello, world!"
print(message[0]) # 'H' (indexing)
print(message[7:12]) # 'world' (slicing)

# List indexing and slicing
numbers = [10, 20, 30, 40, 50]
print(numbers[2]) # 30
print(numbers[1:4]) # [20, 30, 40]

# Tuple indexing and slicing
coordinates = (5, 10)
print(coordinates[0]) # 5

3. Dictionary (dict):

  • Key-Value Pairs: Dictionaries store data as key-value pairs and allow efficient retrieval of values using keys.
# Dictionary example
student = {"name": "Alice", "age": 25, "grade": "A"}
print(student["name"]) # 'Alice'
print(student["age"]) # 25

4. Set Types (set, frozenset):

  • Set Operations: Sets support mathematical operations like union (|), intersection (&), the difference (-), and symmetric difference (^).
# Set operations
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

print(set1 | set2) # Union: {1, 2, 3, 4, 5, 6}
print(set1 & set2) # Intersection: {3, 4}
print(set1 - set2) # Difference: {1, 2}

5. Boolean Type (bool):

  • Logical Operations: Boolean values (True and False) are used in conditional statements (if, while, for) and logical operations (and, or, not).
# Boolean operations
is_active = True
is_adult = False

if is_active and not is_adult:
print("User is active but not an adult.")

6. Type Conversion (Casting):

  • Explicit Conversion: You can convert between different data types using type conversion functions like int(), float(), str(), list(), tuple(), set(), dict(), etc.
# Type conversion examples
x = 10
float_x = float(x) # Convert int to float
str_x = str(x) # Convert int to string

numbers = [1, 2, 3]
tuple_numbers = tuple(numbers) # Convert list to tuple

7. Type Checking:

  • Using type() Function: You can check the type of an object using the type() function or isinstance() function for checking against a specific type.
# Type checking examples
x = 10
print(type(x)) # <class 'int'>
print(isinstance(x, int)) # True

Common Operations on Data Types:

  • Each data type in Python comes with a set of built-in methods and operations. For example:
  • Strings have methods like upper(), lower(), split(), join().
  • Lists support methods like append(), pop(), sort(), index().
  • Dictionaries have methods like keys(), values(), items(), get().

Summary:

Understanding Python’s built-in data types and their operations is fundamental for effective programming in Python. Python’s versatility and ease of use make it suitable for a wide range of applications, from simple scripting tasks to complex data processing and software development projects. Continuously practicing with these data types and exploring their capabilities will enhance your proficiency as a Python programmer.

--

--