Labs / Python OOP

The most beginner-friendly Object Oriented programming tutorial on the entire network: 39 Common composite data types in Python - Sets

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents
The most beginner-friendly Object Oriented programming tutorial on the entire network: 39 Common composite data types in Python - Sets

Abstract:

In Python, a set is a commonly used composite data type. A set is an unordered collection of unique elements. Unlike lists and tuples, elements in a set are unordered, and each element can only appear once.

FreakStudio's Blog

You who study embedded systems, don't you know Object Oriented yet? ?!

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 00 Introduction to Object Oriented Design Methodology

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 01 Basic Concepts of Object Oriented Programming

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 02 Python Implementation of Classes and Objects - Creating Classes with Python

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 03 Python Implementation of Classes and Objects - Adding Attributes to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 04 Python Implementation of Classes and Objects – Adding Methods to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 05 Python Implementation of Classes and Objects - PyCharm Code Labels

The most beginner-friendly Object Oriented programming tutorial across the entire network: 06 Python implementation of classes and objects - data encapsulation of custom classes

The most beginner-friendly Object Oriented programming tutorial on the entire network: 07 Python implementation of classes and objects - type annotations

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 08 Python Implementation of Classes and Objects - @property Decorator

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 09 Python Implementation of Classes and Objects – Relationships Between Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 10 Python Implementation of Classes and Objects - Class Inheritance and Liskov Substitution Principle

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 11 Python Implementation of Classes and Objects - Subclass Calling Parent Class Methods

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 12 Python Implementation of Classes and Objects - Using Python's logging Module to Output Program Runtime Logs

The most beginner-friendly Object Oriented programming tutorial across the network: 13 Python implementation of classes and objects - installation and usage of Sourcetrail, a visualization tool for reading code

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 14 Python Implementation of Classes and Objects – Static Methods and Class Methods of Classes

The most beginner-friendly Object Oriented programming tutorial across the network: 15 Python implementation of classes and objects - the __slots__ magic method

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Web: 16 Python Implementation of Classes and Objects - Polymorphism, Method Overriding and Open Closed Principle

The most beginner-friendly Object Oriented programming tutorial across the network: 17 Python implementation of classes and objects - duck typing and "file-like object"

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 18 Python Implementation of Classes and Objects - Multiple Inheritance and Serial Port Data Curve Plotting with PyQtGraph

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 19 Python Implementation of Classes and Objects – Using PyCharm to Automatically Generate File Comments and Function Comments

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 20. Python Implementation of Classes and Objects – Implementation of Composition Relationship and CSV File Saving

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 21 Python Implementation of Classes and Objects - Organization of Multiple Files: Modules and Packages

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 22 Python Implementation of Classes and Objects - Exceptions and Syntax Errors

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 23 Python Implementation of Classes and Objects - Throwing Exceptions

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 24 Python Implementation of Classes and Objects - Exception Capture and Handling

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 25 Python Implementation of Classes and Objects - Python Determines Input Data Types

The most beginner-friendly Object Oriented programming tutorial across the network: 26 Python implementation of classes and objects - context managers and the with statement

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 27 Python Implementation of Classes and Objects – Exception Hierarchy and Custom Exception Class Implementation in Python

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 28 Python Implementation of Classes and Objects - A Comprehensive Summary of Python Programming Principles, Philosophy and Specifications

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 29 Python Implementation of Classes and Objects – Assertions, Defensive Programming and the Usage of the help function

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 30 Python's Built-in Data Types - The object Root Class

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 31 Python's Built-in Data Types - Object and Type

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 32 Python's Built-in Data Types - Class and Instance

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 33 Python's Built-in Data Types – The Relationship Between Object and Type

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 34 Python's Built-in Data Types - Common Composite Data Types in Python: Tuples and Named Tuples

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 35 Python's Built-in Data Types - Docstrings and the __doc__ Attribute

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 36 Python's Built-in Data Type - Dictionary

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 37 Common Composite Data Types in Python - Lists and List Comprehensions

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 38 Common Composite Data Types in Python – Implementing stack, Queue and Deque with Lists

More exciting content can be found here:

Speed Up Your Python: A Quick Guide to Python Parallel Computing

A Comprehensive Guide to the Debugging Principles of CM3 Microcontrollers

After half a month of dedicated work, the complete summary of embedded technology stack is now released

The "Martial Arts Manual" for Computer Science Competitions

Avnet ZUBoard 1CG Development Board — A New Choice for Deep learning

Deploy the SenseCraft model to the Grove Vision AI V2 image processing module

Access to Documents and Code:

You can download the document via the following link:

https://github.com/leezisheng/Doc

19 74

This document mainly introduces how to use Python for Object Oriented programming, and requires readers to have a basic understanding of Python syntax and microcontroller development. Compared with other blogs or books that explain Python Object Oriented programming, this document is more detailed and focuses on embedded host computer applications. It takes the common serial port data transmission and reception, data processing, dynamic graph drawing and other functions between the host computer and the slave computer as application examples, and uses the Sourcetrail code software to visually read the code to facilitate readers' understanding.

The link to obtain the relevant sample code is as follows:

https://github.com/leezisheng/Python-OOP-Demo

Main Text

Python also supports the set data type. A set is an unordered container consisting of unique elements. Its basic uses include membership testing and eliminating duplicate elements. Set objects support mathematical operations such as union, intersection, difference, and symmetric difference. You can create a set using curly braces or the set () function. Note that to create an empty set, you can only use set () instead of {}, as {} creates an empty dictionary.

In Python, a set can hold any hashable object, not just numbers. A hashable object is the same kind of object that can be used as a key in a dictionary, so lists and dictionaries are excluded.

Like mathematical sets, they only store a backup of objects. Just like dictionaries, sets are unordered. Both leverage underlying hash-based data structures to ensure efficiency. Since they are unordered, elements in a set cannot be looked up via an index. The primary purpose of using a set is to divide the world into two parts: "those that exist in the set" and "those outside the set". It is easy to check whether an item is in the set or iterate over all items in the set, but if you want to sort them, you must first convert the set into a list.

Here are some sample codes for collection-related operations:

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)
# Check whether a member is in the set
print('orange' in basket)
print('crabgrass' in basket)
# Create two empty sets
a = set('abracadabra')
print(a)
b = set('alacazam')
print(b)
# Letters in a but not in b
print(a - b)
# Letters in a or b
print(a | b)
# Letters in both a and b
print(a & b)
# Letters in a or b but not in both
print(a ^ b)

The following are the running results:

The most beginner-friendly Object Oriented programming tutorial on the entire network: 39 Common composite data types in Python - Sets

Here we list some of the operation methods for sets for readers' reference:

Method

Description

add()

Add elements to the collection.

clear()

Removes all elements from the collection.

copy()

Copy a set.

difference()

Returns the difference set of multiple sets.

difference_update()

Removes the elements from the set that are also present in the specified set.

discard()

Removes the specified element from the collection.

intersection()

Returns the intersection of the sets.

intersection_update()

Returns the intersection of the sets.

isdisjoint()

Determine whether two sets contain the same elements; return True if they do not, and False otherwise.

issubset()

Determine whether the specified set is a subset of the parameter set of this method.

issuperset()

Determine whether the parameter set of this method is a subset of the specified set.

pop()

Randomly remove an element.

remove()

Removes the specified element.

symmetric_difference()

Returns a set of the elements that are not duplicated in the two sets.

symmetric_difference_update()

Removes from the current set any elements that are present in another specified set, and inserts into the current set the elements that are present in the other specified set but not in the current set.

union()

Returns the union of two sets.

update()

Add elements to the collection.

len()

Calculate the number of elements in a set.

It is evident from the operation methods of sets that sets are designed for performing operations with other sets, rather than merely serving as a container. If you have data from two different sources and need to quickly combine them in a certain form to determine whether the data overlaps or differs, you can use set operations to compare them efficiently. Alternatively, if the input data may contain duplicate processed data, you can use sets to compare them and only process the new data.

Similar to lists, sets also support comprehensions, however when using the `in` keyword to check whether an element exists in a container, sets are more efficient than lists. When using the syntax `value in container` on a list or set container, it returns True if there is an element in the container equal to `value`, otherwise it returns False. In a list, this will iterate through all objects until a matching value is found, whereas for a set, it only needs to compute the hash value and check for its existence.This means that no matter how large the set is, the time required to check whether a value exists is fixed, whereas for a list, the longer the list length, the longer the time required to search for a value.

347622978