DEV Community

Megalraja
Megalraja

Posted on Edited on

Python Data Types: Understanding Different Types of Data

Introduction

In programming, we work with different kinds of data, such as numbers, text, and collections of values.

Python provides different data types to store and manage these values.

A data type defines what kind of value a variable holds and what operations can be performed on it.

Let's understand the different data types available in Python with simple examples.

1. What Are Data Types?

A data type represents the type of value stored in a variable.

For example:

name = "Arun"
age = 25
height = 5.8
is_student = True
Enter fullscreen mode Exit fullscreen mode

Here, Python automatically identifies the data type of each variable.

We can check the data type using the built-in type() function.

print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
Enter fullscreen mode Exit fullscreen mode

2. Types of Data Types in Python

Python has several built-in data types. They can be grouped into the following categories:

  1. Numeric
  2. String
  3. Sequence
  4. Set
  5. Dictionary
  6. Boolean
  7. None

3. Numeric Data Types

Numeric data types are used to store numbers.

Python provides three main numeric types.

Integer (int)

Integers represent whole numbers without decimal points.

age = 25
marks = 95

print(age)
print(type(age))
Enter fullscreen mode Exit fullscreen mode

Output:

25
<class 'int'>
Enter fullscreen mode Exit fullscreen mode

Float (float)

Float represents numbers containing decimal points.

price = 99.50
height = 5.8

print(price)
print(type(price))
Enter fullscreen mode Exit fullscreen mode

Output:

99.5
<class 'float'>
Enter fullscreen mode Exit fullscreen mode

Complex (complex)

Complex numbers contain a real part and an imaginary part.

number = 10 + 5j

print(number)
print(type(number))
Enter fullscreen mode Exit fullscreen mode

Output:

(10+5j)
<class 'complex'>
Enter fullscreen mode Exit fullscreen mode

4. String (str)

A string is used to store text or a sequence of characters.

Strings can be created using single quotes, double quotes, or triple quotes.

name = "Python"
language = 'Programming'

print(name)
print(type(name))
Enter fullscreen mode Exit fullscreen mode

Output:

Python
<class 'str'>
Enter fullscreen mode Exit fullscreen mode

We can also access individual characters using indexing.

word = "Python"

print(word[0])
print(word[1])
Enter fullscreen mode Exit fullscreen mode

Output:

P
y
Enter fullscreen mode Exit fullscreen mode

5. Sequence Data Types

Sequence data types store multiple values in an ordered collection.

Python provides three important sequence types.

List

A list stores multiple values in a single variable.

Lists are ordered and mutable, which means we can modify their elements.

numbers = [10, 20, 30, 40]

numbers.append(50)

print(numbers)
Enter fullscreen mode Exit fullscreen mode

Output:

[10, 20, 30, 40, 50]
Enter fullscreen mode Exit fullscreen mode

Tuple

A tuple is similar to a list, but it is immutable.

Once created, its elements cannot be changed.

numbers = (10, 20, 30, 40)

print(numbers)
print(type(numbers))
Enter fullscreen mode Exit fullscreen mode

Output:

(10, 20, 30, 40)
<class 'tuple'>
Enter fullscreen mode Exit fullscreen mode

Range

Range represents a sequence of numbers, commonly used in loops.

numbers = range(1, 6)

for num in numbers:
    print(num)
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

6. Set (set)

A set is an unordered collection of unique elements.

It automatically removes duplicate values.

numbers = {10, 20, 30, 20, 10}

print(numbers)
Enter fullscreen mode Exit fullscreen mode

Output:

{10, 20, 30}
Enter fullscreen mode Exit fullscreen mode

Sets are useful when we need to store unique values.

7. Dictionary (dict)

A dictionary stores data in key-value pairs.

Each key is used to access its corresponding value.

student = {
    "name": "Arun",
    "age": 25,
    "department": "IT"
}

print(student["name"])
print(student["age"])
Enter fullscreen mode Exit fullscreen mode

Output:

Arun
25
Enter fullscreen mode Exit fullscreen mode

Dictionaries are useful for storing structured information.

8. Boolean (bool)

Boolean data types represent only two values:

  • True
  • False

They are commonly used in conditional statements.

age = 25

is_eligible = age >= 18

print(is_eligible)
print(type(is_eligible))
Enter fullscreen mode Exit fullscreen mode

Output:

True
<class 'bool'>
Enter fullscreen mode Exit fullscreen mode

9. None Type

The None data type represents the absence of a value.

It is commonly used when a variable does not have an assigned value yet.

result = None

print(result)
print(type(result))
Enter fullscreen mode Exit fullscreen mode

Output:

None
<class 'NoneType'>
Enter fullscreen mode Exit fullscreen mode

10. Mutable vs Immutable Data Types

One important concept in Python is mutability.

Mutable: Objects whose contents can be modified after creation.

Examples:

  • List
  • Dictionary
  • Set

Immutable: Objects whose contents cannot be modified after creation.

Examples:

  • Integer
  • Float
  • String
  • Tuple
  • Boolean

Understanding this difference helps us understand how Python handles data.

11. Summary of Python Data Types

Category Data Type Example
Numeric int 100
Numeric float 10.5
Numeric complex 2+3j
Text str "Python"
Sequence list [1, 2, 3]
Sequence tuple (1, 2, 3)
Sequence range range(5)
Set set {1, 2, 3}
Mapping dict {"age": 25}
Boolean bool True
Special NoneType None

Conclusion

Python provides different data types to store and organize different kinds of information.

Understanding data types is one of the fundamental steps in learning Python programming.

Top comments (0)