Day2-----dictionary, function

Table of contents

1. Dictionary 

1. Dictionary definition and access

​edit

with data

 access value

The length of the dictionary and the number of key-value pairs

2. Addition and modification of dictionaries

​edit

3. Delete data from dictionary

​edit

4. The traversal data of the dictionary

8. enumerate() combines subscripts and values ​​into tuples for lists

9. Public methods

+ Support operations on strings, lists, and tuples to get a new container

* Integer copy, support string, list, tuple operation, get a new container

in/not in judges existence or non-existence, and supports operations on strings, lists, tuples, and dictionaries. Note: If it is a dictionary, it is judged whether the key value exists or not

max/min For dictionaries, the size of the key value of the dictionary to be compared

​edit

Two, function

Definition: It can realize a specific function, the combination of multiple lines of code

 

1. Function documentation comment help (function name) view comment

2. Function parameters When defining a function, define the formal parameters, and pass the actual parameters when calling the function. The number of actual parameters and the number of formal parameters are the same

3. Local variables The variable scope defined inside the function: inside the current function, cannot be used outside the life cycle: it is created when the function is called, and is destroyed after the function call ends

4. Global variables

Scope of variables defined outside the function: inside the function (call and cannot be modified directly, use the keyword global when you want to modify)

5. Function return value return

Return the value after return to terminate the function running without writing the return value, and return None by default

6. Return multiple data

Use a list return [a,b,...] use a dictionary separated by commas (default is a tuple) return a,b,c,..

7. Function nested call

8. Function application



1. Dictionary 

1. Dictionary definition and access

my_dict={}
my_dict1=dict()

with data

my_dict={'name':'ly','age':18,'like':['学习','唱歌','购物']}
print(my_dict)

 access value

# 通过key值访问,不存在时,报错
print(my_dict['age'])
print(my_dict['like'][1])
# 字典.get(key,数值) 不存在时,返回指定的数值,未指定时,返回none
print(my_dict.get('name'))
print(my_dict.get('ll',1))
print(my_dict.get('ll'))

The length of the dictionary and the number of key-value pairs

print(len(my_dict))

2. Addition and modification of dictionaries

# 字典[key]=数值 key存在,修改元数据,不存在则直接添加
my_dict['age']=19
print(my_dict)
my_dict['school']='xysu'
# 注意 key的1与1.0是同一个key值
my_dict[1]='int'
print(my_dict)
my_dict[1.0]='float'
print(my_dict)

3. Delete data from dictionary

# 根据key值删除
# del key值
del my_dict[1]
print(my_dict)
# pop 返回删除的key对应的value值
a=my_dict.pop('age')
print(a)
# clear 清空所有键值对
my_dict.clear()
print(my_dict)
# del 字典名 删除变量
del my_dict

4. The traversal data of the dictionary

my_dict={'name':'ly','age':18,'sex':'男'}
# for循环,直接遍历key值
for key in my_dict:
    print(key,my_dict[key])
# 字典.keys 获取字典中所有key值,返回dict_keys,组成类型为list,可以使用for循环
for key in my_dict.keys():
    print(key)
# 字典.values 获取字典中所有value值,返回dict_values,组成类型为list,可以使用for循环
for value in my_dict.values():
    print(value)
# 字典.items 获取字典中所有键值对,返回dict_items,组成类型为元组,可以使用for循环
for item in my_dict.items():
    print(item[0],item[1])
for key,value in my_dict.items():
    print(key,value)

8. enumerate() combines subscripts and values ​​into tuples for lists

my_list=['a','b','c']
for i in enumerate(my_list):
    print(i)

9. Public methods

  • +Support operations on strings, lists, and tuples to get a new container

  • * 整数Copy, support operations on strings, lists, and tuples to get a new container

  • in/not inJudging whether it exists or not, it supports operations on strings, lists, tuples, and dictionaries. Note: If it is a dictionary, it is judged whether the key value exists or not

  • max/minFor dictionaries, the size of the key value of the dictionary being compared

Two, function

Definition: It can realize a specific function, the combination of multiple lines of code

 

1. Function documentation comment

help (function name) view comment

2. Function parameters

When defining a function, define the formal parameters, and pass the actual parameters when calling the function. The number of actual parameters and the number of formal parameters are the same

3. Local variables

Variables defined inside
the function Scope: Inside the current function, cannot be used outside
Life cycle: Created when the function is called, and destroyed after the function call ends

4. Global variables


Variables defined outside
the function Scope: inside the function (call and cannot be modified directly, when you want to modify, use the keyword global)

5. Function return value return

Return the value after return
to terminate the function running.
You can not write the return value, and return None by default.

6. Return multiple data

Use a list return [a,b,...]
use a dictionary
separated by commas (default is a tuple) return a,b,c,..

7. Function nested call

def func1():
    print('func1 start..')
    print('func1 end..')
def func2():
    print('func2 start..')
    func1()
    print('func2 end..')
func1()
print('*****************')
func2()

8. Function application

# 打印
def print_line():
    print('-'*30)
def print_lines(n):
    for i in range(n):
        print_line()
print_lines(5)
# 求和 求平均数
def my_sum(a,b,c):
    return a+b+c
print(my_sum(2,4,6))
def my_avg(a,b,c):
    res=my_sum(a,b,c)
    return  res/3   # float
print(my_avg(2,4,6))

Guess you like

Origin blog.csdn.net/m0_46493223/article/details/126058892