Python Data Types

Introduction

In Python, data types are used to classify various types of data. Each variable in Python has a data type, which defines what operations can be performed on that variable. Understanding data types is crucial for effective programming in Python.

Python has several built-in data types categorized as follows:

Data Type Categories

Text Type

  • str: Represents a sequence of characters.

Numeric Types

  • int: Integer numbers, positive or negative, without a decimal point.
  • float: Floating-point numbers, which are numbers with a decimal point.
  • complex: Complex numbers, which have a real part and an imaginary part.

Sequence Types

  • list: Ordered, mutable collections of items.
  • tuple: Ordered, immutable collections of items.
  • range: Represents a sequence of numbers, commonly used in loops.

Mapping Type

  • dict: Collections of key-value pairs, where each key is unique.

Set Types

  • set: Unordered collections of unique items.
  • frozenset: Immutable sets.

Boolean Type

  • bool: Represents one of two values: True or False.

Binary Types

  • bytes: Immutable sequences of bytes.
  • bytearray: Mutable sequences of bytes.
  • memoryview: Allows access to the internal data of objects that support the buffer protocol.

None Type

  • NoneType: Represents the absence of a value or a null value.

Data Types with Examples

Text Type

str: Strings are sequences of characters, enclosed in single, double, or triple quotes.

Example:

name = "John"
greeting = 'Hello, World!'
multiline_text = """This is a
multiline string."""

Numeric Types

int: Integers are whole numbers.

Example:

age = 25
score = -10

float: Floats are numbers with a decimal point.

Example:

temperature = 36.6
price = -45.99

complex: Complex numbers have a real part and an imaginary part.

Example:

z = 3 + 5j

Sequence Types

list: Lists are ordered and mutable collections of items.

Example:

numbers = [1, 2, 3, 4, 5]
mixed_list = [1, "Hello", 3.14, True]

tuple: Tuples are ordered and immutable collections of items.

Example:

coordinates = (10, 20)
person = ("Alice", 30, "Engineer")

range: Represents a sequence of numbers, often used in loops.

Example:

range_example = range(1, 10)
for i in range_example:
    print(i)

Mapping Type

dict: Dictionaries are collections of key-value pairs.

Example:

student = {
    "name": "John",
    "age": 25,
    "grades": [85, 90, 92]
}

Set Types

set: Sets are unordered collections of unique items.

Example:

fruits = {"apple", "banana", "cherry"}
unique_numbers = {1, 2, 3, 4, 5}

frozenset: Immutable sets.

Example:

frozen_set_example = frozenset([1, 2, 3, 4])

Boolean Type

bool: Booleans represent one of two values: True or False.

Example:

is_active = True
is_done = False

Binary Types

bytes: Immutable sequences of bytes.

Example:

byte_data = b"Hello"

bytearray: Mutable sequences of bytes.

Example:

byte_array_data = bytearray(5)

memoryview: Allows access to the internal data of objects that support the buffer protocol.

Example:

byte_data = b"Hello"
mv = memoryview(byte_data)

None Type

NoneType: Represents the absence of a value.

Example:

result = None

Type Conversion

Python allows you to convert values from one data type to another, known as type casting.

Examples:

  1. Integer to Float:

    x = 5
    y = float(x)  # y will be 5.0
    
  2. Float to Integer:

    a = 10.5
    b = int(a)  # b will be 10
    
  3. String to Integer:

    num_str = "100"
    num = int(num_str)  # num will be 100
    
  4. Integer to String:

    num = 100
    num_str = str(num)  # num_str will be "100"
    

Checking Data Types

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

Example:

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

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

Conclusion

Understanding Python data types is essential for effective programming. Different data types allow you to work with different kinds of data and perform appropriate operations. By mastering data types, you can write more robust and error-free code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top