Python has several built-in data types that help in storing and manipulating different kinds of data. Below is a detailed discussion of the various data types in Python:


1. Numeric Types

Numeric data types store numbers and support arithmetic operations.

a. int (Integer)

  • Represents whole numbers (positive, negative, or zero).
  • No limit on size (depends on available memory).

Example:

x = 100  # Integer
y = -25  # Negative Integer
z = 0    # Zero

b. float (Floating-Point)

  • Represents decimal (real) numbers.
  • Supports scientific notation.

Example:

a = 10.5  # Floating-point number
b = -3.14
c = 2e3   # Equivalent to 2000.0

c. complex (Complex Number)

  • Represents numbers with a real and an imaginary part.
  • Imaginary part is denoted using j.

Example:

num = 3 + 5j  # Complex number
print(num.real)  # Output: 3.0
print(num.imag)  # Output: 5.0

2. Sequence Types

Sequence types store multiple values in an ordered manner.

a. str (String)

  • Represents a sequence of characters (text).
  • Strings are immutable (cannot be changed after creation).
  • Can be enclosed in single (' ') or double (" ") quotes.

Example:

s1 = "Hello"
s2 = 'Python'
s3 = """Multiline
String"""
print(s1[0])  # Output: H

b. list (List)

  • Ordered, mutable, and allows duplicate values.
  • Can store different data types.

Example:

my_list = [10, 20, 30, "Python", 5.5]
my_list.append(40)  # Adds an element
print(my_list[2])   # Output: 30

c. tuple (Tuple)

  • Ordered, immutable, and allows duplicate values.
  • Faster than lists.

Example:

my_tuple = (1, 2, "Hello", 4.5)
print(my_tuple[1])  # Output: 2

3. Set Types

Set types store unordered, unique elements.

a. set (Set)

  • Unordered and does not allow duplicates.
  • Mutable but elements must be immutable (e.g., numbers, strings, tuples).

Example:

my_set = {1, 2, 3, 3, 4, 5}
print(my_set)  # Output: {1, 2, 3, 4, 5}
my_set.add(6)

b. frozenset (Immutable Set)

  • Like a set but immutable.

Example:

f_set = frozenset([1, 2, 3, 4])
# f_set.add(5)  # This will raise an error

4. Mapping Type

Mapping types store key-value pairs.

a. dict (Dictionary)

  • Stores unordered, mutable key-value pairs.
  • Keys must be unique and immutable.

Example:

my_dict = {"name": "Alice", "age": 25, "city": "New York"}
print(my_dict["name"])  # Output: Alice
my_dict["age"] = 26  # Updating value

5. Boolean Type

a. bool (Boolean)

  • Represents True or False (internally stored as 1 or 0).

Example:

is_active = True
is_admin = False
print(5 > 3)  # Output: True

6. Binary Types

Used for handling binary data.

a. bytes

  • Immutable sequence of bytes.

Example:

b = bytes([65, 66, 67])
print(b)  # Output: b'ABC'

b. bytearray

  • Mutable version of bytes.

Example:

ba = bytearray([65, 66, 67])
ba[0] = 68
print(ba)  # Output: bytearray(b'DBC')

c. memoryview

  • Creates a view of a byte object without copying data.

Example:

mv = memoryview(b"Hello")
print(mv[0])  # Output: 72 (ASCII of 'H')

7. None Type

a. NoneType

  • Represents the absence of a value (null in other languages).

Example:

x = None
print(x)  # Output: None

Summary Table

Data Type Example Properties
int x = 100 Whole numbers
float y = 3.14 Decimal numbers
complex z = 2 + 3j Complex numbers
str "Hello" Text data
list [1, 2, 3] Ordered, mutable
tuple (1, 2, 3) Ordered, immutable
set {1, 2, 3} Unordered, unique
frozenset frozenset({1,2}) Immutable set
dict {"a": 1, "b": 2} Key-value pairs
bool True / False Boolean values
bytes b"ABC" Immutable binary data
bytearray bytearray(b"ABC") Mutable binary data
memoryview memoryview(b"Hello") Binary view
NoneType None No value

Would you like more details on any of these? 🚀


Leave a Reply

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