Basic data types and built-in methods 02

Keywords: Python

tuple

Purpose: store multiple types of values (variable types are not allowed)

Definition method: data is stored in parentheses and separated by commas (the value cannot be changed)

When defining a container type, if there is only one value in it, add a comma after the value. In a tuple, no addition is a string

Tuple Type Summary: ordered, immutable, multiple values stored

t1 = ('a','b','c','a')  # t1 = tuple(('a','b','c'))
print(t1)
print(type(t1))

//Print results:
('a', 'b', 'c', 'a')
<class 'tuple'>

common method

1. Index value

t1 = ('a','b','c','a')
print(t1[0])

//Print results:
a

2. Index slice

t1 = ('a','b','c','a')
print(t1[0:2])

//Print results:
('a', 'b')

3. Member operation

t1 = ('a','b','c','a')
print('a' in t1 )
print('a'not in t1 )

//Print results:
True
False

4. len: get the number of elements in the current tuple

t1 = ('a','b','c','a')
print(len(t1))

//Print results:
4

5. Count: count the number of elements in the current tuple

t1 = ('a','b','c','a')
print(t1.count('a'))

//Print results:
2

6. Index: get the index value of the current tuple, and specify the search range

t1 = ('a','b','c','a')
print(t1.index('b'))

//Print results:
1

Dictionary dict

Purpose: the name of the dictionary indicates the purpose of this data structure. A normal book is suitable for reading from the beginning to the end. If you like, you can quickly turn to any page, which is a bit like a list in Python. Dictionaries (everyday dictionaries and python dictionaries) are designed to make it easy for you to find specific words (keys) to learn their definitions (values)

Definition method: store data by braces, define key value pairs by key:value, and separate each key value pair by comma

key: must be immutable type value: can be any type

# 1,
d1 = {'name':'egon','age':'84'}
print(d1)
# 2,
d2 = dict({'name':'egon','age':'84'})
print(d2)
# 3,
l1 = ['name','age']
l2 = ['egon','84']
z1 = zip(l1,l2)
print(dict(z1))

//Print results:
{'name': 'egon', 'age': '84'}
{'name': 'egon', 'age': '84'}
{'name': 'egon', 'age': '84'}

common method

1. Value according to the key:value mapping relationship (can be saved or modified)

d1 = {'name':'egon','age':'84'}
print(d1['name'])
print(d1['age'])
d1['name'] = 'tank'
d1['gender'] = 'male'
print(d1)

//Print results:
egon
84
{'name': 'tank', 'age': '84', 'gender': 'male'}

2. Member operation in, not in

d1 = {'name':'egon','age':'84'}
print('egon' in d1)

//Print results:
False

3. len: get the number of key value pairs in the dictionary

d1 = {'name':'egon','age':'84'}
print(len(d1))

//Print results:
2

4. Get: get the value of the specified key. If it does not exist, it returns None by default

d1 = {'name':'egon','age':'84'}
print(d1.get('gender'))
# The second parameter can be used to modify the content returned by default
print(d1.get('gender','no'))

//Print results:
None
no

5. keys, value, items: combined with for recycling

d1 = {'name':'egon','age':'84'}
print(d1.keys())    # Return all key
print(d1.values())  # Return all value
print(d1.items())   # Return all key value pairs, list elements on return
for keys in d1.keys():
    print(keys)
for value in d1.values():
    print(value)
for items in d1.items():
    print(items)

//Print results:
dict_keys(['name', 'age'])
dict_values(['egon', '84'])
dict_items([('name', 'egon'), ('age', '84')])
name
age
egon
84
('name', 'egon')
('age', '84')

6. pop: specify key to delete, with return value

d1 = {'name':'egon','age':'84'}
d1.pop('name')
print(d1)

//Print results:
{'age': '84'}

7. popitem: randomly delete a group of key value pairs, and the key value pairs returned are tuples

d1 = {'name':'egon','age':'84'}
d1.popitem()
print(d1)

//Print results:
{'name': 'egon'}

8. update: replace old dictionary with salary dictionary

d1 = {'name':'egon','age':'84'}
d2 = {'1':'a'}
d1.update(d2)
print(d1)
d1.update({'name':'tank'})
print(d1)

//Print results:
{'name': 'egon', 'age': '84', '1': 'a'}
{'name': 'tank', 'age': '84', '1': 'a'}

9. fromkeys: a new dictionary will be generated. For the first parameter, each element of the first parameter will be the key, and the next parameter will be the value to form a new dictionary

d1 = {'name':'egon','age':'84'}
print(dict.fromkeys([1,2,3],['ke','k1']))

//Print results:
{1: ['ke', 'k1'], 2: ['ke', 'k1'], 3: ['ke', 'k1']}

10. setdefault: the value of the new key value pair is returned if the key does not exist

d1 = {'name':'egon'}
print(d1.setdefault('name',1))
print(d1)

//Print results:
egon
{'name': 'egon'}

aggregate

Purpose: de duplication, relation operation

Definition method: data is stored by braces, and each element is separated by commas

Defining an empty set must be defined using set()

Type Summary: unordered (no index), variable, multiple values stored

Can be added or deleted, but cannot be changed

Common methods:

Consortium:
Intersection:
Difference sets:
Symmetric difference set:^

# Two identical elements are not possible in a collection
python_student = {'egon', 'jason', 'tank', 'owen', 'egon'}
linux_student = {'frank', 'alex', 'egon'}
go_student = {'egon'}

print(python_student)
print(python_student | linux_student)
print(python_student & linux_student)
print(python_student - linux_student)
print(linux_student - python_student)
print(python_student ^ linux_student)
print(python_student > go_student)      #Judging parent set
print(python_student < linux_student)    #Judgement subset


//Print results:
{'jason', 'egon', 'tank', 'owen'}
{'tank', 'alex', 'owen', 'jason', 'egon', 'frank'}
{'egon'}
{'jason', 'tank', 'owen'}
{'frank', 'alex'}
{'alex', 'frank', 'tank', 'owen', 'jason'}
True
False

Type Summary:

Posted by ashrust on Wed, 06 Nov 2019 02:40:46 -0800