Python Data Types

What Are Data Types in Python?

A data type defines the nature of the value a variable can hold. Python is a dynamically typed language, meaning you don’t need to declare the type of a variable explicitly. Python assigns the data type automatically based on the value you assign to a variable.

Example:

x = 10          # Integer
y = 3.14 # Float
z = "Hello" # String

Categories of Python Data Types

Python data types can be broadly categorized into:

  1. Numeric Types
  2. Sequence Types
  3. Mapping Types
  4. Set Types
  5. Boolean Type
  6. None Type

1. Numeric Types

Numeric data types are used to store numbers. Python supports three numeric types:

Integer (int): Whole numbers, positive or negative, without a decimal point.
Example:

age = 25
print(type(age)) # Output: <class 'int'>

Floating Point (float): Numbers with a decimal point or in exponential form.
Example:

price = 19.99
print(type(price)) # Output: <class 'float'>

Complex (complex): Numbers with a real and an imaginary part. Represented as a + bj.
Example:

num = 3 + 4j
print(type(num)) # Output: <class 'complex'>

2. Sequence Types

Sequence types are used to store collections of items in an ordered manner.

a. Strings (str)

A string is a sequence of characters enclosed in single, double or triple quotes.
Example:

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

b. Lists (list)

Lists are ordered, mutable collections of items. They can store elements of different data types.
Example:

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

c. Tuples (tuple)

Tuples are ordered, immutable collections of items.
Example:

coordinates = (10, 20, 30)
print(type(coordinates)) # Output: <class 'tuple'>

d. Ranges (range)

Represents a sequence of numbers and is often used in loops.
Example:

nums = range(1, 10)
print(type(nums)) # Output: <class 'range'>

3. Mapping Types

Dictionaries (dict)

Dictionaries store data in key-value pairs. Keys must be unique and immutable.
Example:

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

4. Set Types

Sets (set)

Sets are unordered collections of unique items.
Example:

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

Frozen Sets (frozenset)

Immutable version of a set.
Example:

immutable_set = frozenset([1, 2, 3])
print(type(immutable_set)) # Output: <class 'frozenset'>

5. Boolean Type

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

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

6. None Type

The None type represents the absence of a value.
Example:

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

Type Conversion in Python

Python allows you to convert values from one type to another using typecasting functions.

Example:

# Convert integer to string
num = 10
str_num = str(num)
print(type(str_num)) # Output: <class 'str'>

# Convert string to float
price = "19.99"
float_price = float(price)
print(type(float_price)) # Output: <class 'float'>

Leave a Comment