Bigdata – Knowledge Base

Python – Datatypes

In Python, data types are classified into primitive (or basic) and non-primitive (or composite) types. Let’s explore each category in detail with examples.

Primitive Data Types #

Primitive data types are the most basic types of data. They are the building blocks for data manipulation and are usually built into the language.

  1. Integer (int)
    • Whole numbers, positive or negative, without decimals.
    • Example: age = 25
  2. Float (float)
    • Numbers with decimal points.
    • Example: temperature = 36.6
  3. Boolean (bool)
    • Represents one of two values: True or False.
    • Example: is_student = True
  4. String (str)
    • A sequence of characters enclosed within single or double quotes.
    • Example: name = "Alice"

Non-Primitive Data Types #

Non-primitive data types are more complex data structures. They can hold multiple values and are derived from primitive data types.

  1. List
    • Ordered collection of items, which can be of different types.
    • Mutable, meaning their elements can be changed.
    • Example: codefruits = ["apple", "banana", "cherry"]
  2. Tuple
    • Ordered collection of items similar to a list, but immutable.
    • Example: codecoordinates = (10.0, 20.0)
  3. Set
    • Unordered collection of unique items.
    • Example: codeunique_numbers = {1, 2, 3, 4, 5}
  4. Dictionary
    • Collection of key-value pairs, where each key is unique.
    • Example: student = {"name": "Bob", "age": 22, "is_student": True}

Examples of Using These Data Types #

Summary #

  • Primitive Data Types: int, float, bool, str.
  • Non-Primitive Data Types: list, tuple, set, dictionary.

Primitive types store single values, whereas non-primitive types store collections of values. This distinction is important for understanding how data is managed and manipulated in Python.

What are your feelings
Updated on August 25, 2024