Sunday, September 4, 2022

Python data types continued.....

Python provides a set data type for storing sets of data. A set is not a sequence because it does not have a specific order.

Python also provides a single mapping data type, dict, which is used for creating dictionaries. A dictionary in Python is not a dictionary in the everyday sense, although there are some similarities between the two: A key in the dictionary maps to a particular value, enabling you to look up that value.

Understanding the Set Data Type

In Python, the set data type enables you to store multiple values in a single variable. The set data type has the following characteristics:

• It contains elements. The elements, also called members, are the discrete objects that make up the set.

• Each element is unique. A set cannot have duplicate elements. By contrast, a list or a tuple can have duplicate elements.

• It is unordered. The elements in a set have no specific order. This means you cannot refer to an element in a set by its index or position.

• It is immutable. Once you have created a set, you cannot change its existing items, but you can add further items to the set if you need to.

Understanding the Mapping Data Type

Python’s mapping category contains a single data type, dict, which is used for dictionaries. A dictionary consists of key/value pairs, with the key in each pair giving you access to set, retrieve, or modify the associated collection of information in the value.

A dictionary is unordered; you access the data by supplying the appropriate key rather than an index  value.

A dictionary is mutable, so you can change its contents after creating it.

Python’s Classes

In Python, a class is a kind of template you use for creating a new object of a particular type. You can create a class object to organize the functions and other code in a particular project.

That sounds nebulous, but if you work with office productivity software, you are likely used to a similar paradigm. For example, if you need to create many memos of the same type in Microsoft Word, you may create a custom memo template containing the layout and formatting for the memo, and perhaps some VBA code for automation. That memo template is analogous to a Python class.

Understanding the Instance Data Type

In Python, an instance is an individual object created from a particular class. For example, say you create a class that contains the functions needed to run a particular data‐aggregation and assessment task. When you want to work on that data, you create an instance of the class — or, to use the formal term, you instantiate the class.

Continuing the previous example, when you need to produce a memo, you create a new document based on your memo template rather than using the memo template itself. The document is analogous to an instance of the template class.

Understanding the Exception Data Type

In Python, an exception is an object representing an error that occurred during code. Will will see later how to work with Python’s built‐in exceptions to handle errors when they occur and how to create custom exceptions.

Share:

0 comments:

Post a Comment