Python Data Science Toolbox
程序员文章站
2024-01-30 16:43:28
...
Python Data Science Toolbox
User-defined functions
● Define functions without parameters
● Define functions with one parameter
● Define functions that return a value
● Later: multiple arguments, multiple return values
参数的作用范围
global scope;
eg:
glbal new_val;
nonlocal n;
local scope;
Default and flexible arguments
- 默认的参数:
def power(number,pow = 1):
… - flexible arguments: *args(1)
将实参中按照位置传值,多出来的值都给args,且以元组的方式呈现。 - flexible arguments: **kwargs
表示形参中按照关键字传值把多余的传值以字典的方式呈现
lambda 函数
匿名函数
add= lambda x,y : x+y
add(1,2)
>>3
map函数:map(func, seq)
nums=[48,6,9,21,1]
square_all = map(lambda num: num **2 ,nums)
print(list(square_all))
>>[2304,36,81,441,1]
错误处理
def sqrt(x):
if x < 0:
raise ValueError("x must be non-negative")
try:
return x ** 0.5
expect TypeError:
print("x must be an int or float")
推荐阅读
-
Python Data Science Toolbox Part 1 Learning 1 - User-defined functions
-
Python Data Science Toolbox
-
10-Python-Data-Science-Toolbox(Part 1)
-
Python 官方文档 中文版 5. Data Structures
-
Introduction to Data Science in Python Week 4
-
Coursera | Introduction to Data Science in Python(University of Michigan)| Assignment4
-
【课程】Introduction to Data Science in Python Week 1
-
Introduction to Data Science in Python 第 2 周 Assignment
-
【课程】Introduction to Data Science in Python
-
introduction to data science w4