Introduce Non-Primitive Data Types and give examples
Non-primitive data types, also known as reference data types, are data types that are not defined by a single value but rather can hold multiple values or more complex structures. Unlike primitive data types, which represent single values (like integers, floats, and characters), non-primitive data types can store collections of values or more complex entities. They are typically more flexible and can represent more complex data structures.
Arrays:
numbers = [1, 2, 3, 4, 5] # An array of integers
Strings:
greeting = "Hello, World!" # A string
Lists:
my_list = [1, "apple", 3.14, True] # A list containing different data types
Tuples:
my_tuple = (1, "banana", 3.14) # A tuple
Dictionaries (or Maps):
my_dict = {"name": "Alice", "age": 30, "city": "New York"} # A dictionary
Sets:
my_set = {1, 2, 3, 4, 5} # A set of integers
Classes/Objects:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
my_dog = Dog("Buddy", 5) # An object of the Dog class
Non-primitive data types are essential for organizing and managing complex data in programming. They allow developers to create more sophisticated data structures that can represent real-world entities and relationships, making it easier to write efficient and maintainable code.