{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# 定义一个无参函数\n", "def get_name():\n", " print(\"我叫张三\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我叫张三\n" ] } ], "source": [ "# 调用一个无参函数\n", "get_name()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# 定义一个含参数的函数(name和age都是形参)\n", "def show_infos(name,age):\n", " \"\"\"打印name和age\"\"\"#函数文档说明\n", " print(\"我叫\",name,\"我的年龄是\",age)#这种输出方式会自动以空格连接字符串" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我叫 李四 我的年龄是 22\n" ] } ], "source": [ "# 调用一个含参数的函数\n", "show_infos(\"李四\",22)#实参" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# 定义一个含默认参数(缺省参数)的函数(默认参数必须指向不变对象)\n", "# 比如你设置一个list,改变了内容,则下次调用时,默认参数(缺省参数)的内容就变了,不再是函数定义时的值了\n", "def default_param(name,age=23):\n", " \"\"\"age默认为23\"\"\"\n", " print(\"我叫\",name,\"我的年龄是\",age)#这种输出方式会自动以空格连接字符串" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我叫 张三 我的年龄是 23\n" ] } ], "source": [ "# 调用一个默认参数(缺省参数)的函数\n", "default_param(\"张三\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# 定义有返回值的函数\n", "def div_have_return(a,b):\n", " \"\"\"计算a+b,返回计算结果\"\"\"#函数文档说明\n", " return a+b" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "计算结果为 3\n" ] } ], "source": [ "# 调用有返回值的函数\n", "result=div_have_return(1,2)\n", "print(\"计算结果为\",result)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# 定义含有多个返回值的函数(利用了元组)\n", "def div_have_returns(a,b):\n", " \"\"\"计算a+b的和,计算a-b,并返回两个结果\"\"\"\n", " return (a+b),(a-b)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sum: 3 D-value: -1\n" ] } ], "source": [ "# 调用含有多个返回值的函数\n", "sum,dvalue=div_have_returns(1,2)\n", "print(\"sum:\",sum,\"D-value:\",dvalue)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(3, -1)\n" ] } ], "source": [ "# 测试一下~返回多个值其实是利用了元组\n", "# 多返回值只是一种假象,Python函数返回的仍然是单一值~元组\n", "test=div_have_returns(1,2)\n", "print(test)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "################ 扩充 #################\n", "# 空函数定义\n", "def method():\n", " pass #相当于占位符,ifelse等等里面都是可以出现的\n", "\n", "method()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# 定义函数时,需要确定函数名和参数个数\n", "# 如果有必要,可以先对参数的数据类型做检查\n", "# 函数体内部可以用return随时返回函数结果\n", "# 函数执行完毕也没有return语句时,自动return None\n", "# 函数可以同时返回多个值,但其实就是一个tuple" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "############# 传入多个参数系列 #############" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "######## 通过元组、列表实现 ########\n", "def default_some_params(nums):\n", " \"\"\"借助Tuple和list\"\"\"\n", " sum=0\n", " for item in nums:\n", " sum+=item\n", " return sum" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 元组传入\n", "default_some_params((1,2,3,4,5))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 列表传入\n", "default_some_params(list(range(1,6)))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "######## 定义一个可变参数的函数(名字一般都是*args) ########\n", "# 定义可变参数和定义一个list或tuple参数相比,仅仅在参数前面加了一个*号。\n", "# 在函数内部,接收到的参数是一个tuple。调用该函数时,可以传入任意个参数(包括0个参数)\n", "def default_params(*args):\n", " \"\"\"定义一个可变参数,用来求所有参数的总和\"\"\"\n", " sum=0\n", " for item in args:\n", " sum+=item\n", " return sum" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 调用一个可变参数的函数\n", "default_params(1,2,3,4,5)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 传一个list或者Tuple(参数前面加*)\n", "# 传一个元组\n", "test_tuple=(1,2,3,4,5)\n", "default_params(*test_tuple)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# range(1,6) ==> [1,6) ==> 1,2,3,4,5\n", "test_list=list(range(1,6))\n", "default_params(*test_list)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "######## 调用含关键字参数的函数 ########\n", "# 可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple\n", "# 关键字参数允许你传入0个或任意个含key-value的参数,自动组装为一个dict\n", "def default_kv_params(name,age=23,**kv):\n", " \"\"\"输出输出用户传参\"\"\"\n", " print(\"我叫\",name,\"我的年龄是\",age,\"dict:\",kv)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我叫 dnt 我的年龄是 23 dict: {'web': 'www.baidu.com', 'qq': 110}\n" ] } ], "source": [ "# 调用含关键字参数的函数\n", "default_kv_params(\"dnt\",web=\"www.baidu.com\",qq=110)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "#### 同时定义可变参数+关键字参数 ####\n", "def default_god_params(name,age=23,*args,**kv):\n", " \"\"\"同时有可变参数+关键字参数的使用方法\"\"\"\n", " print(\"我叫\",name,\"我的年龄是\",age,\"list:\",args,\"dict:\",kv)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我叫 dnt 我的年龄是 24 list: (1, 2, 3, 4, 5) dict: {'web': 'www.baidu.com', 'qq': 110}\n" ] } ], "source": [ "# 调用可变参数+关键字参数\n", "# 有名字给kv,没名字给args\n", "default_god_params(\"dnt\",24,1,2,3,4,5,web=\"www.baidu.com\",qq=110)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我叫 dnt 我的年龄是 24 list: (1, 2, 3, 4, 5) dict: {}\n" ] } ], "source": [ "#只调用可变参数\n", "default_god_params(\"dnt\",24,1,2,3,4,5)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我叫 dnt 我的年龄是 23 list: () dict: {'web': 'www.baidu.com', 'qq': 110}\n" ] } ], "source": [ "# 只调用关键字参数\n", "default_god_params(\"dnt\",web=\"www.baidu.com\",qq=110)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我叫 1 我的年龄是 2 list: (3, 4, 5) dict: {'web': 'www.baidu.com', 'qq': 110}\n" ] } ], "source": [ "#### 传元组和字典 ####\n", "test_tuple=(1,2,3,4,5)\n", "test_dict={\"web\":\"www.baidu.com\",\"qq\":110}\n", "\n", "default_god_params(*test_tuple,**test_dict)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }