Data Types

In Python, data types define the kind of data a variable can hold. Knowing data types is essential because it helps you understand how to work with variables and their values. Data types tell Python the kind of data you’re working with, like numbers, text, or collections of items. Python automatically detects the type of data when you assign a value to a variable (no need to declare it explicitly).

Standard Data Types in Python

Python has the following standard data types:

  1. Numeric
  2. Text
  3. Boolean
  4. Sequence
  5. Set
  6. Mapping
  7. Binary Types

1. Numeric Data Types

Numeric data types are used to store numbers.

a. Integer (int):

  • Whole numbers (positive or negative) without decimals.
  • Example:
Python
Copy
x = 10
y = -5
print(type(x))  # Output: <class 'int'>

b. Float (float):

  • Numbers with decimals.
  • Example:
Python
Copy
pi = 3.14
temperature = -12.5
print(type(pi))  # Output: <class 'float'>

c. Complex (complex):

  • Numbers with a real and imaginary part.
  • Example:
Python
Copy
z = 3 + 4j
print(type(z))  # Output: <class 'complex'>

2. Text Data Type

String (str):

  • Strings are used to store text and are enclosed in single or double quotes or triple quotes.
  • Example :
Python
Copy
name = "Alice"
greeting = 'Hello, World!'
message = '''This is 
            Test
            Message'''
print(type(name))  # Output: <class 'str'>

3. Boolean Data Type

Boolean (bool):

  • Represents True or False.
  • Example:
Python
Copy
is_adult = True
is_student = False
print(type(is_adult))  # Output: <class 'bool'>

4. Sequence Data Types

These are used to store collections of items.

a. List (list):

  • Ordered, mutable (can be changed), and allows duplicates.
  • Example:
Python
Copy
fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # Output: apple
fruits.append("orange")  # Add an item
print(fruits)  # Output: ['apple', 'banana', 'cherry', 'orange']

b. Tuple (tuple):

  • Ordered, immutable (cannot be changed), and allows duplicates.
  • Example:
Python
Copy
numbers = (1, 2, 3)
print(numbers[1])  # Output: 2

c. Range (range):

  • Used to generate a sequence of numbers.
  • Example:
Python
Copy
numbers = range(5)  # Sequence from 0 to 4
print(list(numbers))  # Convert range to list: [0, 1, 2, 3, 4]

5. Set Data Types

Sets are used to store unique items.

a. Set (set):

  • Unordered, mutable, and does not allow duplicates.
  • Example:
Python
Copy
unique_numbers = {1, 2, 3, 3, 4}
print(unique_numbers)  # Output: {1, 2, 3, 4}
unique_numbers.add(5)
print(unique_numbers)  # Output: {1, 2, 3, 4, 5}

b. Frozenset (frozenset):

  • Like a set, but immutable.
  • Example:
Python
Copy
frozen_set = frozenset([1, 2, 3])
print(frozen_set)  # Output: frozenset({1, 2, 3})

6. Mapping Data Type

Dictionary (dict):

  • Stores data in key-value pairs.
  • Example:
Python
Copy
person = {
    "name": "Alice",
    "age": 25,
    "city": "New York"
}
print(person["name"])  # Output: Alice
person["age"] = 26  # Update a value
print(person)  # Output: {'name': 'Alice', 'age': 26, 'city': 'New York'}

Other Special Data Types

1. NoneType (None):

  • Represents the absence of a value.
  • Example:
Python
Copy
result = None
print(type(result))  # Output: <class 'NoneType'>

2. Bytes and Bytearray:

  • Used for binary data.
  • Example:
Python
Copy
data = b"hello"
print(type(data))  # Output: <class 'bytes'>

Dynamic Typing in Python

  • Python is dynamically typed, meaning you don’t need to declare the type of a variable. It’s automatically inferred.
  • Example:
Python
Copy
x = 10       # Integer
x = "hello"  # Now a string
print(x)     # Output: hello