加载模块
模块或称为包、库,是已经写好的函数库,使用之前需要先加载。
使用import关键字,直接加载
import pandas
使用as 起别名
import pandas as pd
加载指定的函数
from pandas import Series,DataFrame
函数
简单函数
使用def 关键字定义函数
#定义函数
def my_func():
print('加油')
#调用函数
my_func()#打印加油
增加参数
#定义函数
def my_func(s):
print(s)
#调用函数
my_func('加油')
增加返回值
使用return增加返回值
#定义函数
def my_func(s):
return s.strip()
#调用函数
my_func(' hello ')
默认值参数
#定义函数
def my_func(s='hello'):
return s.strip()
my_func() #调用函数
my_func(' 加油 ')
匿名函数
定义一个匿名函数,返回给定数字的二次方。
s=lambda x:x**2
调用匿名函数
s(3) #返回9