Here’s a detailed explanation of Python Data Types for your Python Programming:


What are Data Types?

In Python, data types represent the kind of data a variable can store. Python is a dynamically-typed language, which means you don’t need to explicitly define the type of a variable—it’s determined automatically based on the value assigned.


Common Python Data Types

  1. Numeric Types
    • int: Stores whole numbers. age = 25 # Integer
    • float: Stores numbers with decimals. price = 19.99 # Float
    • complex: Stores complex numbers (real + imaginary parts). num = 3 + 4j # Complex number
  2. Text Type
    • str: Stores sequences of characters (text). name = "Alice"
  3. Boolean Type
    • bool: Stores either True or False. is_valid = True
  4. Sequence Types
    • list: Stores an ordered collection of items (mutable). fruits = ["apple", "banana", "cherry"]
    • tuple: Stores an ordered collection of items (immutable). coordinates = (10, 20, 30)
    • range: Represents a sequence of numbers. numbers = range(5) # Creates 0, 1, 2, 3, 4
  5. Mapping Type
    • dict: Stores key-value pairs. person = {"name": "Alice", "age": 25}
  6. Set Types
    • set: Stores an unordered collection of unique items. unique_numbers = {1, 2, 3, 4}
    • frozenset: An immutable version of a set. frozen = frozenset({1, 2, 3})
  7. Binary Types
    • bytes: Immutable sequence of bytes. data = b"hello"
    • bytearray: Mutable sequence of bytes. data = bytearray(5)
    • memoryview: Memory view of a binary object. mem_view = memoryview(b"hello")
  8. None Type
    • NoneType: Represents the absence of a value. result = None

Checking Data Types

You can check the type of a variable using the type() function:

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

y = "Hello"
print(type(y))  # Output: <class 'str'>

Type Conversion

Python provides built-in functions to convert data types (type casting):

  1. Implicit Type Conversion (done automatically): x = 5 # int y = 2.5 # float z = x + y # float (Python automatically converts int to float) print(z) # Output: 7.5
  2. Explicit Type Conversion (done manually): # Convert int to float x = 10 y = float(x) # y is now 10.0 # Convert string to int age = "25" age_int = int(age) # Converts "25" to 25

Detailed Examples for Each Data Type

1. Numeric Types

# Integer
x = 42
print(x, type(x))  # Output: 42 <class 'int'>

# Float
pi = 3.14
print(pi, type(pi))  # Output: 3.14 <class 'float'>

# Complex
num = 1 + 2j
print(num, type(num))  # Output: (1+2j) <class 'complex'>

2. String

# String
message = "Hello, Python!"
print(message, type(message))  # Output: Hello, Python! <class 'str'>

3. Boolean

is_active = True
print(is_active, type(is_active))  # Output: True <class 'bool'>

4. List

fruits = ["apple", "banana", "cherry"]
print(fruits, type(fruits))  # Output: ['apple', 'banana', 'cherry'] <class 'list'>

5. Tuple

dimensions = (1920, 1080)
print(dimensions, type(dimensions))  # Output: (1920, 1080) <class 'tuple'>

6. Dictionary

person = {"name": "Alice", "age": 25}
print(person, type(person))  # Output: {'name': 'Alice', 'age': 25} <class 'dict'>

7. Set

unique_numbers = {1, 2, 3, 4}
print(unique_numbers, type(unique_numbers))  # Output: {1, 2, 3, 4} <class 'set'>

8. None Type

result = None
print(result, type(result))  # Output: None <class 'NoneType'>

Example Code

Here’s a complete example that covers different data types:

# Numeric Types
age = 30          # int
price = 19.99     # float
complex_num = 3 + 5j  # complex

# String
name = "Alice"

# Boolean
is_member = True

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

# Tuple
coordinates = (10, 20)

# Dictionary
person = {"name": "Alice", "age": 25}

# Set
unique_items = {1, 2, 3}

# None
result = None

# Print all types
print(type(age))         # <class 'int'>
print(type(price))       # <class 'float'>
print(type(name))        # <class 'str'>
print(type(is_member))   # <class 'bool'>
print(type(fruits))      # <class 'list'>
print(type(coordinates)) # <class 'tuple'>
print(type(person))      # <class 'dict'>
print(type(unique_items))# <class 'set'>
print(type(result))      # <class 'NoneType'>

Here’s a detailed explanation of Python Data Types for your Python Programming:


What are Data Types?

In Python, data types represent the kind of data a variable can store. Python is a dynamically-typed language, which means you don’t need to explicitly define the type of a variable—it’s determined automatically based on the value assigned.


Common Python Data Types

  1. Numeric Types
    • int: Stores whole numbers. age = 25 # Integer
    • float: Stores numbers with decimals. price = 19.99 # Float
    • complex: Stores complex numbers (real + imaginary parts). num = 3 + 4j # Complex number
  2. Text Type
    • str: Stores sequences of characters (text). name = "Alice"
  3. Boolean Type
    • bool: Stores either True or False. is_valid = True
  4. Sequence Types
    • list: Stores an ordered collection of items (mutable). fruits = ["apple", "banana", "cherry"]
    • tuple: Stores an ordered collection of items (immutable). coordinates = (10, 20, 30)
    • range: Represents a sequence of numbers. numbers = range(5) # Creates 0, 1, 2, 3, 4
  5. Mapping Type
    • dict: Stores key-value pairs. person = {"name": "Alice", "age": 25}
  6. Set Types
    • set: Stores an unordered collection of unique items. unique_numbers = {1, 2, 3, 4}
    • frozenset: An immutable version of a set. frozen = frozenset({1, 2, 3})
  7. Binary Types
    • bytes: Immutable sequence of bytes. data = b"hello"
    • bytearray: Mutable sequence of bytes. data = bytearray(5)
    • memoryview: Memory view of a binary object. mem_view = memoryview(b"hello")
  8. None Type
    • NoneType: Represents the absence of a value. result = None

Checking Data Types

You can check the type of a variable using the type() function:

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

y = "Hello"
print(type(y))  # Output: <class 'str'>

Type Conversion

Python provides built-in functions to convert data types (type casting):

  1. Implicit Type Conversion (done automatically): x = 5 # int y = 2.5 # float z = x + y # float (Python automatically converts int to float) print(z) # Output: 7.5
  2. Explicit Type Conversion (done manually): # Convert int to float x = 10 y = float(x) # y is now 10.0 # Convert string to int age = "25" age_int = int(age) # Converts "25" to 25

Detailed Examples for Each Data Type

1. Numeric Types

# Integer
x = 42
print(x, type(x))  # Output: 42 <class 'int'>

# Float
pi = 3.14
print(pi, type(pi))  # Output: 3.14 <class 'float'>

# Complex
num = 1 + 2j
print(num, type(num))  # Output: (1+2j) <class 'complex'>

2. String

# String
message = "Hello, Python!"
print(message, type(message))  # Output: Hello, Python! <class 'str'>

3. Boolean

is_active = True
print(is_active, type(is_active))  # Output: True <class 'bool'>

4. List

fruits = ["apple", "banana", "cherry"]
print(fruits, type(fruits))  # Output: ['apple', 'banana', 'cherry'] <class 'list'>

5. Tuple

dimensions = (1920, 1080)
print(dimensions, type(dimensions))  # Output: (1920, 1080) <class 'tuple'>

6. Dictionary

person = {"name": "Alice", "age": 25}
print(person, type(person))  # Output: {'name': 'Alice', 'age': 25} <class 'dict'>

7. Set

unique_numbers = {1, 2, 3, 4}
print(unique_numbers, type(unique_numbers))  # Output: {1, 2, 3, 4} <class 'set'>

8. None Type

result = None
print(result, type(result))  # Output: None <class 'NoneType'>

Example Code

Here’s a complete example that covers different data types:

# Numeric Types
age = 30          # int
price = 19.99     # float
complex_num = 3 + 5j  # complex

# String
name = "Alice"

# Boolean
is_member = True

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

# Tuple
coordinates = (10, 20)

# Dictionary
person = {"name": "Alice", "age": 25}

# Set
unique_items = {1, 2, 3}

# None
result = None

# Print all types
print(type(age))         # <class 'int'>
print(type(price))       # <class 'float'>
print(type(name))        # <class 'str'>
print(type(is_member))   # <class 'bool'>
print(type(fruits))      # <class 'list'>
print(type(coordinates)) # <class 'tuple'>
print(type(person))      # <class 'dict'>
print(type(unique_items))# <class 'set'>
print(type(result))      # <class 'NoneType'>