DEV Community

Akash Singh
Akash Singh

Posted on

Basic Python: 4. Python Data Types

Every value in Python has a data type, which determines what kind of data it represents and what operations can be performed on it. Some common data types in Python are:

Table of Contents

  1. Numeric Data Types
    • 1.1 Integer (int)
    • 1.2 Floating-Point (float)
    • 1.3 Complex (complex)
  2. Textual Data Types
    • 2.1 String (str)
    • 2.2 String Operations
  3. Sequence Data Types
    • 3.1 List (list)
    • 3.2 Tuple (tuple)
    • 3.3 Range (range)
  4. Mapping Data Type
    • 4.1 Dictionary (dict)
  5. Set Data Type
    • 5.1 Set (set)
    • 5.2 Frozenset (frozenset)
  6. Boolean Data Type
    • 6.1 bool: Boolean Values
  7. Binary Types
    • 7.1 bytes: Bytes
    • 7.2 bytearray: Byte Arrays
  8. NoneType
    • 8.1 None: The None Object
  9. Type Conversion (Casting)
  10. User-Defined Classes
  11. Checking Data Types

1. Numeric Data Types

1.1 Integer (int)

Integers represent whole numbers without decimal points. They can be positive or negative.

x = 5
y = -10
Enter fullscreen mode Exit fullscreen mode

1.2 Floating-Point (float)

Floating-point numbers are numbers with decimal points. They are used to represent real numbers.

pi = 3.14159
temperature = -2.5
Enter fullscreen mode Exit fullscreen mode

1.3 Complex (complex)

Complex numbers consist of a real part and an imaginary part.

z = 2 + 3j
Enter fullscreen mode Exit fullscreen mode

2. Textual Data Types

2.1 String (str)

Strings are sequences of characters enclosed in single or double quotes.

name = "Akash"
message = 'Hello, world!'
Enter fullscreen mode Exit fullscreen mode

2.2 String Operations

Strings support various operations like concatenation, slicing, and formatting.

greeting = "Hello"
name = "Akash"
full_greeting = greeting + ", " + name  # Concatenation
substring = full_greeting[7:12]  # Slicing
formatted_message = f"{greeting}, {name}"  # String formatting
Enter fullscreen mode Exit fullscreen mode

3. Sequence Data Types

3.1 List (list)

Lists are ordered collections that can contain elements of different types.

fruits = ["apple", "banana", "orange"]
Enter fullscreen mode Exit fullscreen mode

3.2 Tuple (tuple)

Tuples are similar to lists but are immutable.

coordinates = (3, 5)
Enter fullscreen mode Exit fullscreen mode

3.3 Range (range)

Ranges represent sequences of numbers.

numbers = range(1, 6)  # Creates a range from 1 to 5
Enter fullscreen mode Exit fullscreen mode

4. Mapping Data Type

4.1 Dictionary (dict)

Dictionaries store key-value pairs and allow fast lookup.

person = {
    "name": "Akash",
    "age": 23,
    "city": "Pune"
}
Enter fullscreen mode Exit fullscreen mode

5. Set Data Type

5.1 Set (set)

Sets are unordered collections of unique elements.

colors = {"red", "green", "blue"}
Enter fullscreen mode Exit fullscreen mode

5.2 Frozenset (frozenset)

Frozensets are like sets but are immutable.

immutable_colors = frozenset({"red", "green", "blue"})
Enter fullscreen mode Exit fullscreen mode

6. Boolean Data Type

6.1 bool: Boolean Values

Boolean values represent truth (True) or falsehood (False).

is_valid = True
Enter fullscreen mode Exit fullscreen mode

7. Binary Types

7.1 bytes: Bytes

Bytes represent sequences of bytes (integers from 0 to 255).

data = b"hello"
Enter fullscreen mode Exit fullscreen mode

7.2 bytearray: Byte Arrays

Byte arrays are mutable sequences of bytes.

mutable_data = bytearray(data)
Enter fullscreen mode Exit fullscreen mode

8. None Type

8.1 None: The None Object

The None object represents the absence of a value.

result = None
Enter fullscreen mode Exit fullscreen mode

9. User-Defined Classes

You can define your own data types using classes.

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
Enter fullscreen mode Exit fullscreen mode

10. Type Conversion (Casting)

Python allows you to convert between different data types using type casting functions.

x = "5"
y = int(x)  # Casting to integer
Enter fullscreen mode Exit fullscreen mode

11. Checking Data Types

Python provides the type() function to determine the data type of a value. This function returns the class type of the argument. Here's how you can use it:

print(type(age))          # Output: <class 'int'>
print(type(temperature))  # Output: <class 'float'>
print(type(name))         # Output: <class 'str'>
print(type(is_student))   # Output: <class 'bool'>
print(type(fruits))       # Output: <class 'list'>
print(type(coordinates))  # Output: <class 'tuple'>
print(type(person))       # Output: <class 'dict'>
print(type(unique_numbers))  # Output: <class 'set'>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)