Python : Data Types

In Python, data types represent the type of values that variables can hold. Python has several built-in data types that you can use to store and manipulate different kinds of data. Here are some of the commonly used data types in Python:

Numeric Types:

  • int: Integers represent whole numbers, positive or negative, without decimal points.
  • float: Floating-point numbers represent decimal numbers.

Examples:

x = 5       # int
y = 3.14    # float

 

Text Type:
str: Strings represent sequences of characters enclosed in single quotes (`'`) or double quotes (`"`).

Examples:

name = 'Alice'        # str
greeting = "Hello"    # str

 

Boolean Type:
bool : Booleans represent truth values, either `True` or `False`.

Examples:

is_true = True       # bool
is_false = False     # bool

 

Sequence Types:

  • list: Lists are ordered, mutable sequences that can contain elements of different data types.
  • tuple: Tuples are ordered, immutable sequences that can contain elements of different data types.
  • range: Range represents an immutable sequence of numbers.

Examples:

numbers = [1, 2, 3, 4, 5]     # list
coordinates = (10, 20)        # tuple
count = range(1, 6)           # range

 

Mapping Type:
 dict : Dictionaries are unordered, mutable collections of key-value pairs.

Examples:

person = {"name": "Alice", "age": 25}    # dict
 

Set Types:

  • set : Sets are unordered, mutable collections of unique elements.
  • frozenset : Frozensets are unordered, immutable collections of unique elements.

Examples:

fruits = {"apple", "banana", "cherry"}    # set
vowels = frozenset("aeiou")                # frozenset

 

Other Types:
None : None represents the absence of a value or a null value.

Example:

result = None    # None
 

These are just a few of the built-in data types available in Python. Additionally, Python provides the ability to define custom data types using classes.

Understanding and correctly using the appropriate data types is essential for performing operations and manipulating data effectively in your Python programs.