学习目标:
函数入门
学习内容:
# 封装代码(方便管理),实现代码重用
'''
定义函数
def 函数名称(参数):
代码1
代码2
...
'''
# name_list = ['小明', '小刚']
#
#
# def list_info():
# print(name_list[0])
# print('列表的长度:', len(name_list))
# print('list:', name_list)
# print('type:', type(name_list))
# print('id', id(name_list))
#
#
# name_list.append('小雨')
# list_info() # 调用函数
# 简单的函数
# def print_info():
# print('hello world')
#
#
# print_info()
# 参数 让函数更加灵活
# def print_info(data): # 参数只会作用于里面的数据
# print(data)
# print_info([1])
#
# def list_sum(list_data): # 将列表中的数据求和
# total = 0
# for i in list_data:
# total = total + i
# print(total)
#
#
# list1 = [1, 2, 3, 4]
# list_sum(list1)
#
#
# def list_len(list_data):
# i = 0
# for _ in list_data:
# i += 1
# print(i)
#
#
# list_len(list1)
# 返回值 return 返回的参数
# def list_sum(list_data): # 将列表中的数据求和
# """
# 将列表中的数据求和
# :param list_data: 传入一个列表
# :return: 数字
# """
# total = 0
# for i in list_data:
# total = total + i
# return total # return 整个函数将会直接结束 后面的代码将不会运行
# list1 = [1, 21, 356, 4444]
# data = list_sum(list1)
# print(data)
# 需要用到列表中的和
# 说明文档
# help(len)
# help(list_sum)
# 注解
# def data_sum(a: int, b: int, c: float) -> int: # 传入多个参数和输出多个参数
# """
#
# :param a:数字
# :param b: 数字
# :param c: 数字
# :return: 数字,数字
# """
# return a + b , b + c
#
#
# data = data_sum(1, 2, 3.33)
# print(data)
# 嵌套调用
# def test_a():
# print('a')
#
#
# def test_b():
# test_a()
# return 'b'
#
#
# print(test_b())
# set 去重
# list_data = [3, 4, 2, 2, 4, 0, 1, 4]
# def rem_repeat(list_in):
# set1 = list(set(list_in))
# return set1
#
#
# data = rem_repeat(list_data)
# print(data)
课后作业:
# 去重
def remove_same(data):
list1 = []
for i in data:
if i not in list1:
list1.append(i)
return list1
list_data = [3, 4, 2, 2, 4, 0, 1, 4]
list_data = remove_same(list_data) # 去重好的数据
print(list_data)
学习产出:
- 技术笔记 1 遍