DEV Community

Mahalakshmi C
Mahalakshmi C

Posted on

Python Data Types

What are Data Types?

  • Data types define the type of data that a variable can store.

  • Python provides different built-in data types to store numbers, text, collections, and logical values.

Python is a dynamically typed language, so there is no need to declare the data type explicitly. The data type is automatically determined based on the assigned value.

Types of Data Types in Python

Python has several built-in data types.

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

1. Numeric Data Types

Numeric data types are used to store numerical values.

Python provides three main numeric data types.

a) int (Integer)

The int data type is used to store whole numbers without decimal points.

Example:

age = 24
salary = 25000

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

Output:

24
25000
<class 'int'>
Enter fullscreen mode Exit fullscreen mode

b) float

The float data type is used to store numbers with decimal points.

Example:

price = 99.50
percentage = 85.5

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

Output:

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

c) complex

The complex data type is used to store complex numbers. It contains a real part and an imaginary part.

Example:

number = 5 + 3j

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

Output:

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

2. String Data Type (str)

A string is a collection of characters enclosed within single quotes, double quotes, or triple quotes.

Strings are used to store text values.

Example:

name = "Mahalakshmi"
language = 'Python'

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

Output:

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

3. Boolean Data Type (bool)

The Boolean data type represents only two values: True and False.

It is mainly used in conditional statements and logical operations.

Example:

is_student = True
is_working = False

print(is_student)
print(is_working)
print(type(is_student))
Enter fullscreen mode Exit fullscreen mode

Output:

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

4. Sequence Data Types

Sequence data types are used to store multiple values in an ordered collection.

Python provides three important sequence data types.

a) List

  • A list is an ordered and mutable collection of elements.
  • It allows duplicate values and can store different data types.

Example:

fruits = ["Apple", "Mango", "Orange"]

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

Output:

['Apple', 'Mango', 'Orange']
<class 'list'>
Enter fullscreen mode Exit fullscreen mode

b) Tuple

A tuple is an ordered and immutable collection of elements. Once created, its elements cannot be changed.

Example:

numbers = (10, 20, 30)

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

Output:

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

c) Range

The range data type represents a sequence of numbers. It is commonly used in loops.

Example:

numbers = range(1, 6)

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

Output:

[1, 2, 3, 4, 5]
<class 'range'>
Enter fullscreen mode Exit fullscreen mode

5. Set Data Type

  • A set is an unordered collection of unique elements.
  • It does not allow duplicate values.

Example:

numbers = {10, 20, 30, 10}

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

Output:

{10, 20, 30}
<class 'set'>
Enter fullscreen mode Exit fullscreen mode

6. Dictionary Data Type (dict)

  • A dictionary is used to store data in key-value pairs.
  • Each key is unique, and values can be of different data types.

Example:

student = {
    "name": "John",
    "age": 22,
    "course": "Python"
}

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

Output:

{'name': 'John', 'age': 22, 'course': 'Python'}
John
<class 'dict'>
Enter fullscreen mode Exit fullscreen mode

7. None Data Type

The None data type represents the absence of a value or a null value.

Example:

result = None

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

Output:

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

Top comments (0)