Learn Python the fifth day of learning content summary (function sequence + + + collection of dictionaries)
1, the definition and use of the python function
- No-argument function
- There are function parameters
- With return function values
### 函数的定义
'''
无参函数:
def <函数名>():
语句内容
有参函数:
def <函数名>(参数):
语句内容
在这里可以调用语句内容
def <函数名>():
语句内容
return
'''
demo no-argument function:
def forever():
name = 'forever'
age = 18
height = 180
print(f'{name}的个人信息,姓名:{name},年龄:{age},身高:{height}')
# 调用函数
forever()
operation result:
forever personal information, name: forever, Age: 18, Height: 180
There are demo function parameters:
def forever_parameter(name,age,height):
print(f'{name}的个人信息,姓名:{name},年龄:{age},身高:{height}')
# 调用带参数的函数方法
forever_parameter('nick',18,185)
operation result:
nick of personal information, name: nick, Age: 18, Height: 185
demo return value of a function of no arguments:
def forever_return():
name = 'forever'
age = 18
height = 180
return name
# 调用带返回值的无参函数
print(forever_return())
operation result:
forever
2, the collection operation Types
Unordered set is a combination of a plurality of elements
- Consistent set of concepts collection types and mathematics
- Disorder, unique to each element, the same element is not present between the set of elements
- Collection of elements can not be changed, it can not be variable data type
- A set of braces {}, said elements separated by commas
- Establishing a set {Type}, or set ()
- Build-empty type, you must use the set ()
Specific methods for the collection operation (and examples):
a = {'forever','big',18 ,180}
ab = {'nick','big',20,185,'handsome'}
# 输出a和ab都有的元素
print(a&ab)
# 输出a和ab中所有的元素
print(a|ab)
# 输出a和ab非相同的元素
print(a^ab)
# 输出a 不在 ab 中的元素
print(a-ab)
arr = {'new','arr'}
print(arr)
arr.add('abc')
print(arr)
arr.discard('new')
print(arr)
arr.remove('arr')
print(arr)
s2 = arr.copy()
print(s2)
num = len(arr)
print(num)
3, a list of types and operation
The list is an extension of sequence type, very common
- List is a sequence type, can be modified at will to create
- Square brackets [] or list () to create, between elements separated by commas,
- The list of elements of different types can be no length limit
Specific examples:
ss = ['123','abc','forever','nick',10,180]
print(ss)
## 列表的内置方法
# 替换列表中第i元素为新的元素
ss[0] = '1234'
# 用另一个列表代替 原有列表切片位置中的元素
ss[0:3] = [1,2,3]
print(ss)
Function or method | description |
---|---|
ls[i] = x | Alternatively the i-th list element is ls x |
ls[i: j: k] = lt | After replacing the list with ls lt element sections corresponding sublist |
ls the [i] | I deleted the first element of the list ls |
del ls[i: j: k] | Delete the first list ls i to j-k is a long step to the elements |
ls += lt | Ls update list, the list will be added to the list of elements lt ls of |
ls = n * | Update the list ls, whose elements are repeated n times |
List type manipulation functions and methods
Function or method | description |
---|---|
ls.append(x) | In the list of the last ls add an element x |
ls.clear() | Delete all of the elements in the list ls |
ls.copy() | Generate a new list, all elements assigned ls |
ls.insert(i,x) | Adding elements x i of the list of ls |
ls.pop(i) | The i-th position of the list element ls removed and remove the element |
ls.remove(x) | The first element in the list that appears in x delete ls |
ls.reverse() | Ls will list the elements in reverse |
4, and operation of a dictionary
A dictionary is the embodiment of "map" of
- Key-value pair: the key is the data index extension
- Dictionary is a collection of key-value pairs, the key to the disorder between
- And using braces {} dict () to create key-value pair with a colon: indicates
Dictionary handlers and methods:
Function or method | description |
---|---|
of d [k] | D button to delete the dictionary data corresponding to the value k |
k in d | Determine whether the key k d in the dictionary, if in return True, otherwise False |
d.keys() | Return all the key information in the dictionary d |
d.values() | All the information returned value in the dictionary d |
d.items() | D return dictionary of all the key information |
Dictionary type operating functions and methods:
Function or method | description |
---|---|
d.get(k, ) | Bond k is present, the corresponding value is returned, the return value is not |
d.pop(k, ) | Bond k is present, the corresponding values are fetched, the return value is not |
d.popitem () | D taken randomly from the dictionary in a key-value pairs in the form of tuples to return |
d.clear() | Remove all key-value pairs |
len (d) | Returns the number of elements in the dictionary d |
Specific examples:
people = {'name':'forever','age':18,'weight':140,'height':180}
# print(people)
# 字典类型的增加
people.setdefault('money',10000000)
print(people)
# 字典类型的删除
people.pop('weight')
print(people)
# 字典类型的修改
people['age']=20
print(people)
# 字典类型的查找
print(people['name'])
# 循环输出字典的所有键值对标识
for i in people.keys():
print(i,end=' ')
# 循环输出字典的所有标识的值
for i in people.values():
print(i,end=' ')
# 循环输出字典的所有键值对
for i in people.items():
print(i)
字典嵌套
dict_test = {'O':{'eng':'1245435','enn':'123535'}}
print(dict_test)
to sum up
Today, no time, to keep, such as free in summary, hey hey hey