40. Collection of Python data types
1. Python data types
There are 8 common data types in Python:
[data type data]
1. String: str, characters enclosed in quotation marks.
2. Integer: int, a number without a decimal point.
3. Floating point number: float, a number with a decimal point.
4. Boolean type: bool, only 2 values ( True and False ).
[Data of container type]
5. List: list, use English square brackets [ ] to store one of python8 data types, or any number of them.
6. Tuple: tuple, use English parentheses ( ) to store one of python8 data types, or any of several.
7. Dictionary: dict, use English braces { } to store multiple pairs of data with corresponding relationships.
8. Set: set, use English braces { } to store unordered, non-repeated, and immutable data.
2. What is a set
set[set]: set, a group, a set.
A collection is an iterable, unordered container type of data that cannot contain duplicate elements.
3. Characteristics of collections
-
Sets are represented by curly braces { }.
-
The elements of a collection are unordered.
-
The elements of a collection cannot be repeated.
-
The elements of a collection can only be of immutable data types.
-
Collections themselves are mutable data types.
[Kind tips]
The elements of a collection are immutable.
Collections themselves are mutable: that is, collection elements can be added, removed, and emptied.
4. The Syntax of Collections
The collection mainly consists of 3 parts:
- English braces { }
- elements of a collection
- Elements are separated by commas
5. Create a new empty collection
5.1 Use English braces { } to create an empty dictionary instead of an empty collection
Using English square brackets [ ] we can create an empty list.
Using English parentheses ( ) we can create an empty tuple.
Using curly braces { } we can create an empty dictionary.
[Kind tips]
Both dictionaries and collections are represented by English braces { }, but English braces { } can only create empty dictionaries but not empty collections.
# create an empty list none_list = [ ] # Create a new empty tuple none_tuple = ( ) # create an empty dictionary none_dict ={ } # type function view data type print(type(none_list)) print(type(none_tuple)) print(type(none_dict))
[Terminal output]
<class 'list'> <class 'tuple'> <class 'dict'>
none[nʌn]: none, none, default value.
5.2 Create an empty collection with the set function
# create an empty collection none_set = set( ) # type function view data type print(type(none_set))
[Terminal output]
<class 'set'>
[code analysis]
-
none_set is my name for the empty set.
-
= is an assignment symbol.
-
set is the function name.
-
The function name set is followed by an English parenthesis ( ).
6. Create a collection
There are usually 2 ways to create a non-empty collection:
Method 1: Add elements directly in English braces { }.
Method 2: Use the set function to convert the iterable object into a collection.
6.1 Create a collection whose elements are strings
# A collection whose elements are strings set_1 = {'a','a','a','a'} print('A collection whose elements are strings',set_1)
[Terminal output]
A collection whose elements are strings {'a'}
[code analysis]
The element types of the above collections are all strings.
Strings are immutable data types.
Although 4 character strings 'a' are added in the curly brackets { } when creating the set, the elements of the set cannot be repeated, so only 1 character string 'a' is reserved in the output result.
6.2 Create a set whose elements are integers
# set of integers set_2 = {1, 3, 9, 9, 9, 9} print('set of integers',set_2)
[Terminal output]
set of integers {1, 3, 9}
[code analysis]
The element types of the above collections are all integers.
Integers are immutable data types.
Although 6 integers are added in the curly brackets { } when creating the set, the elements of the set cannot be repeated, so only 1 integer 9 is reserved in the output result.
6.3 Create a collection whose elements are floating-point numbers
# A collection whose elements are floating-point numbers set_3 = {0.5, 1.1, 1.1 ,1.1} print('A collection whose elements are floating-point numbers',set_3)
[Terminal output]
A collection whose elements are floating-point numbers {0.5, 1.1}
[code analysis]
The element types of the above collections are all floating point numbers.
Floating point numbers are immutable data types.
Although 4 floating-point numbers are added in the curly brackets { } when creating a collection, the elements of the collection cannot be repeated, so only 1 floating-point number 1.1 is reserved in the output result.
6.4 Create a set whose elements are tuples
# A collection whose elements are tuples # The tuple is represented by () set_4 = {('Life is too short', 'I use Python'),('Life is too short', 'I use Python'),('Life is too short', 'I use Python')} print('A collection whose elements are tuples',set_4)
[Terminal output]
A collection whose elements are tuples {('Life is too short', 'I use Python')}
[code analysis]
The element types of the above collections are tuples.
Tuples are immutable data types.
Although 3 identical tuples are added in the curly brackets { } when creating a collection, the elements of the collection cannot be repeated, so only 1 tuple is retained in the output result.
6.5 Creating collections whose elements are Boolean values
# A collection whose elements are Boolean values # Boolean: True, False set_5 = {True, False, True, False} print('A collection whose elements are Boolean values',set_5)
[Terminal output]
A collection whose elements are Boolean values {False, True}
[code analysis]
The element types of the above collections are all boolean values.
Boolean values are immutable data types.
Although 4 Boolean values are added in the curly brackets { } when creating a collection, the elements of the collection cannot be repeated, so only 2 Boolean values are reserved in the output result.
6.6 The elements of a collection cannot be lists
# collection whose elements are lists # The list is represented by [] set_6 = {['Zhang San','Li Si','Zhang San']} print('collection whose elements are lists',set_6)
[Terminal output]
TypeError: unhashable type: 'list'
[code analysis]
The elements of the collection are lists, and the list is a variable data type, and the program reports an error.
unhashable type: 'list': Unhashable type: 'list'.
6.7 The elements of a collection cannot be dictionaries
# A collection whose elements are dictionaries # Dictionaries are also represented by {} dict_1 = { 'name':'Andy', 'age':'20', 'sex':'female' } set_7 = {dict_1} print('A collection whose elements are dictionaries',set_7)
[Terminal output]
TypeError: unhashable type: 'dict'
[code analysis]
The elements of the collection are dictionaries, dictionaries are variable data types, and the program reports an error.
unhashable type: 'dict': Unhashable type: 'dict'.
6.8 The elements of a collection cannot be collections
# A collection whose elements are collections # Sets are represented by {} set_8 = {{'a','b'}} print('A collection whose elements are collections',set_8)
[Terminal output]
TypeError: unhashable type: 'set'
The elements of the collection are collections, which are variable data types, and the program reports an error.
unhashable type: 'set': Unhashable type: 'set'.
6.9. Creating Collections Containing Multiple Data Types
# The elements of the collection contain multiple data types set_9 = {'a','b',True, 2, 9.9 , 9.9 } print('The elements are collections of various data',set_9)
[Terminal output]
The elements are collections of various data {True, 2, 'b', 9.9, 'a'}
[Kind tips]
The collection is unordered: the order of the collection elements output after clicking Run may be different from the order of the elements we created the collection.
6.10 Convert an iterable to a collection with the set function
This knowledge point is written in a separate section.
7. The elements of a collection can only be immutable data types
Immutable data types:
-
string
-
integer
-
floating point number
-
tuple
-
Boolean type
Mutable data types:
-
the list
-
dictionary
-
gather