Basic data types and built-in methods

Keywords: Python

Basic data types and built-in methods (2)

I. built in method of list

  • Count: count the number of specified elements in the current list
s1 = ['y', 'a', 'f', 'e', 'n', 'g', '6', '6', '6']
s1.count('6')
print(s1.count('6'))
>>>3
  • Index: get the index of the current element, and specify the search range
s1 = ['y', 'a', 'f', 'e', 'n', 'g', '6', '6', '6']
s1.index('6')
print(s1.index('6'))
>>>6
s1.index("6", 7)
print(s1.index("6", 7))
>>>7
  • Sort: sort, default reverse=False, i.e. ascending sort
s1 = [1, 2, 5, 6, 7, 8, 9]
s1.sort()
print(s1)
>>>[1, 2, 5, 6, 7, 8, 9]

s1 = [1, 2, 5, 6, 7, 8, 9]
s1.sort(reverse=True)
print(s1)
>>>[9, 8, 7, 6, 5, 2, 1]
  • sorted:python's built-in sorting. When sorting, a new list will be produced, and the original data will not change
s1 = [1, 2, 5, 6, 7, 8, 9]
a = sorted(s1)
print(a)
>>>[1, 2, 5, 6, 7, 8, 9]

s1 = [1, 2, 5, 6, 7, 8, 9]
a = sorted(s1, reverse=True)
print(a)
>>>[9, 8, 7, 6, 5, 2, 1]
  • Clear: clear all data in the list
s1 = [1, 2, 5, 6, 7, 8, 9]
s1.clear()
print(s1)
>>>[]
  • Queues and stacks
'''
//Queue: first in, first out
l1 = []
l1.append(1)
l1.append(2)
l1.append(3)
print(l1)
>>>[1,2,3]
l1.pop[0]
print(l1)
>>>[2,3]
l1.pop[0]
print(l1)
>>>[3]
l1.pop[0]
print(l1)
>>>[]

//Stack: first in first out
l1 = []
l1.append(1)
l1.append(2)
l1.append(3)
print(l1)
>>>[1,2,3]
l1.pop()
print(l1)
>>>[1,2]
l1.pop()
print(l1)
>>>[1]
l1.pop()
print(l1)
>>>[]

'''
  • Summary

    The list can store multiple values, orderly (ordered if there is index), variable

Two, Yuan Zu

  • Definition and usage
#Purpose: store multiple values of different types (variable types are not allowed)
#Definition method: data is stored in parentheses. Data and data are separated by commas. (value cannot be changed)
    #When defining a container type, if there is only one value in it, add a comma after the value (very important)
    #If it is not added in the tuple, it is a string
  • common method
"""
1,Index value (positive, negative)
2,Index slice
3,Member operation in ,not in
4,len()
"""
1,Index value
t1 = ('y', 'a', 'f', 'e', 'n', 'g')
print(t1[1])
>>>a

t1 = ('y', 'a', 'f', 'e', 'n', 'g')
print(t1[-2])
>>>n

2,Index slice
t1 = ('y', 'a', 'f', 'e', 'n', 'g')
print(t1[2:])
>>>('f', 'e', 'n', 'g')

3,Member operation
t1 = ('y', 'a', 'f', 'e', 'n', 'g')
print('a' in t1)
>>>True

4,len()
t1 = ('y', 'a', 'f', 'e', 'n', 'g')
print(len(t1))
>>>6
  • Summary

    Orderly, multiple values can be saved, immutable

Three, dictionary

  • Definition and definition method
Definition mode:Data is stored through braces, through key:value To define key value pair data, each key value pair is separated by commas
        key:Must be an immutable type
        value: Can be any type
        
//Three definitions:
1,important
 d1 = {
'name': 'yafeng',
'age': '18',
'hobby': 'study'
}
print(d1)
>>>{'name': 'yafeng', 'age': '18', 'hobby': 'study'}
2,important
d2 = dict({'name': 'yafeng', 'age': '18', 'hobby': 'study'})
print(d2)
>>>{'name': 'yafeng', 'age': '18', 'hobby': 'study'}
3,Can understand
l1 = ['name', "age", "hobby"]
l2 = ['yafeng', 18, "study"]
z1 = zip(l1, l2)
print(dict(z1))
>>>{'name': 'yafeng', 'age': 18, 'hobby': 'study'}
  • Priority method
"""
1. Priority:
    1. Take the value according to the key:value mapping relationship (can be saved or desirable)
    2. Member operation in, not in. Default key
    3. len() -- get the number of key value pairs in the current dictionary
"""
  • Built in method get
get:Get assigned key If the value does not exist, the default value is None,The second parameter can be used to modify the content returned by default
d1 = {
    'name': 'yafeng',
    'age': '18',
    'hobby': 'study'
    }
print(d1.get('name'))
>>>yafeng
  • keys,values,items
keys Return all key
values Return all values
items Return all key value pairs, return value is list
d1 = {
    'name': 'yafeng',
    'age': '18',
    'hobby': 'study'
    }
for key in d1.keys():
    print(key)
for value in d1.values():
    print(value)
for key, value in d1.items():
    print(key, value)
key, value = ("name", 'age')
>>>name
   age
   hobby
   yafeng
   18
   study
   name yafeng
   age 18
   jhobby study
  • pop
pop Appoint key To delete, return to the corresponding value value
a = d1.pop('age')
print(d1)
print(a)
>>>{'name': 'yafeng', 'hobby': 'study'}
>>>18
  • popitem
A key value pair will pop up randomly. If there is a return value, the return value is a primitive ancestor
d1.popitem()
print(d1)
>>>{'name': 'yafeng', 'age': '18'}
  • update
Replace old dictionary with new one
d1.update({'gender': 'male'})
print(d1)
>>>{'name': 'yafeng', 'age': '18', 'hobby': 'study', 'gender': 'male'}
  • fromkeys
To produce a new dictionary, take each element in the first parameter (iteratable object) as the key and the second parameter as the value to form a new dictionary
print(dict.fromkeys([1, 2, 3], ['ke', 'k1']))
>>>{1: ['ke', 'k1'], 2: ['ke', 'k1'], 3: ['ke', 'k1']}
  • setdefault
key If there is no new key value pair, return to new if there is a return value value,key There is a corresponding value
print(d1.setdefault('height', '1.77'))
print(d1)
>>>1.77
>>>{'name': 'yafeng', 'age': '18', 'hobby': 'study', 'height': '1.77'}

Four, set

# Purpose: de duplication and relation operation
# Definition method: data is stored by braces, and each element is separated by commas
# To define an empty set, you must use set() to define
# l1 = []
# s1 = ""
# d1 = {}
# ss1 = set()
# Common methods:

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

"""
1,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)
# print(python_student < linux_student)

# l1 = [1, 2, 3, 1, 2, 9, 1, 5, 6, 7]
# print(l1)
#
# s1 = set(l1)
# print(s1)
# print(type(s1))
# l2 = list(s1)
# print(l2)
# print(type(l2))

for i in python_student:
    print(i)
  • Summary

    Unordered, variable, store multiple values

Five, summary

"""
Save one: integer, floating point, string
 Store multiple values: list, tuple, dictionary, set


Variable or immutable:
    Variable:; list, dictionary, set
    Immutable: integer, floating point, string, tuple

Ordered or unordered:
    Order: string, list, tuple
    Disorder: dictionary, set

Occupied space:
    Dictionaries
    list
    tuple
    aggregate
    Character string
    Numeric type
"""

Posted by ICEcoffee on Wed, 06 Nov 2019 01:44:04 -0800