内置函数

绝对值函数

x = abs(100)
y = abs(-20)
print('x=100的绝对值为:{}'.format(x))
print('y=-20的绝对值为:{}'.format(y))
x=100的绝对值为:100
y=-20的绝对值为:20

求最大值、最小值、求和函数

print("(1, 2, 3, 4)中最大max的元素为:{}".format(max(1, 2, 3, 4)))
print("(1, 2, 3, 4)中最小min的元素为:{}".format(min(1, 2, 3, 4)))
print("(1, 2, 3, 4)中最元素累加和sum为:{}".format(sum([1, 2, 3, 4])))
(1, 2, 3, 4)中最大max的元素为:4
(1, 2, 3, 4)中最小min的元素为:1
(1, 2, 3, 4)中最元素累加和sum为:10

模块中的函数

import random
char_set = "abcdefghijklmnopqrstuvwxyz0123456789"
print("char_set长度{}".format(len(char_set)))
char_set[random.randint(0, 35)]
char_set长度36

'j'

自定义函数

自定义绝对值函数

def my_abs(x):
"判断x的类型,如果不是int和float,则出现类型错误。"
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
# 判断x的正负
if x >= 0:
return x
else:
return -x
print("自定义绝对值函数的调用:采用-函数名()的形式 my_abs(-20) = {}".format(my_abs(-20)))
自定义绝对值函数的调用:采用-函数名()的形式 my_abs(-20) = 20

自定义移动函数

import math  # 导入数学库
def move(x, y, step, angle=0):
nx = x + step * math.cos(angle)
ny = y + step * math.sin(angle)
return nx, ny x, y = move(100, 100, 60, math.pi/6)
print("原始坐标(100, 100),沿着x轴逆时针pi/6移动60后的坐标为:新坐标 (x, y) = ({}, {})".format(x, y))
原始坐标(100, 100),沿着x轴逆时针pi/6移动60后的坐标为:新坐标 (x, y) = (151.96152422706632, 130.0)

自定义打印函数

# 该函数传入的参数需要解析字典形式
def print_scores(**kw):
print(' Name Score')
print('------------------')
for name, score in kw.items():
print('%10s %d' % (name, score))
print() # 用赋值方式传参
print_scores(Adam=99, Lisa=88, Bart=77)
      Name  Score
------------------
Adam 99
Lisa 88
Bart 77
data = {
'Adam Lee': 99,
'Lisa S': 88,
'F.Bart': 77
}
# 用字典形式传参,需要解析,用两个*
print_scores(**data)
      Name  Score
------------------
Adam Lee 99
Lisa S 88
F.Bart 77
# 各种混合参数的形式定义的函数,一般遵行一一对应
def print_info(name, *, gender, city='Beijing', age):
print('Personal Info')
print('---------------')
print(' Name: %s' % name)
print(' Gender: %s' % gender)
print(' City: %s' % city)
print(' Age: %s' % age)
print() print_info('Bob', gender='male', age=20)
print_info('Lisa', gender='female', city='Shanghai', age=18)
Personal Info
---------------
Name: Bob
Gender: male
City: Beijing
Age: 20 Personal Info
---------------
Name: Lisa
Gender: female
City: Shanghai
Age: 18

递归阶乘函数

# 利用递归函数计算阶乘
# N! = 1 * 2 * 3 * ... * N
def fact(n):
if n == 1:
return 1
return n * fact(n-1) print('fact(1) =', fact(1))
print('fact(5) =', fact(5))
print('fact(10) =', fact(10))
fact(1) = 1
fact(5) = 120
fact(10) = 3628800

递归函数移动汉诺塔

def move(n, a, b, c):
if n == 1:
print('move', a, '-->', c)
else:
move(n-1, a, c, b)
move(1, a, b, c)
move(n-1, b, a, c) move(4, 'A', 'B', 'C')
move A --> B
move A --> C
move B --> C
move A --> B
move C --> A
move C --> B
move A --> B
move A --> C
move B --> C
move B --> A
move C --> A
move B --> C
move A --> B
move A --> C
move B --> C

混合参数函数

def hello(greeting, *args):
if (len(args)==0):
print('%s!' % greeting)
else:
print('%s, %s!' % (greeting, ', '.join(args))) hello('Hi') # => greeting='Hi', args=()
hello('Hi', 'Sarah') # => greeting='Hi', args=('Sarah')
hello('Hello', 'Michael', 'Bob', 'Adam') # => greeting='Hello', args=('Michael', 'Bob', 'Adam') names = ('Bart', 'Lisa')
hello('Hello', *names) # => greeting='Hello', args=('Bart', 'Lisa')
Hi!
Hi, Sarah!
Hello, Michael, Bob, Adam!
Hello, Bart, Lisa!

参考

Python3基础-函数实例学习的更多相关文章

  1. Python3基础 函数 关键字参数 的示例

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

  2. Python3基础 函数名.__doc__显示一个函数的单行与多行函数文档

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

  3. Python3基础 函数 递归 阶乘与斐波那契数列

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. Python3基础 函数 函数名赋值操作

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  5. Python3基础 函数 参数为list 使用+=会影响到外部的实参

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  6. Python3基础 函数 参数 多个参数都有缺省值,需要指定参数进行赋值

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  7. Python3基础——函数

    ython 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可 ...

  8. Python3基础 函数 收集参数+普通参数 的示例

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

  9. Python3基础 函数 默认值参数示例

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

随机推荐

  1. Oracle 数据库导出数据泵(EXPDP)文件存放的位置

    数据泵是服务器端工具,导出的文件是放在数据库所在的服务器上,当然我们知道可以通过directory目录对象来控制.目录对象默认有四个级别,当然是有优先级顺序的,优先级从上往下 1.每个文件单独的指定具 ...

  2. C#与C++数据类型比较及结构体转换[整理]

    //c++:HANDLE(void   *)                          ----    c#:System.IntPtr//c++:Byte(unsigned   char)  ...

  3. 操作失败: 无法更改关系,因为一个或多个外键属性不可以为 null

    报错:操作失败: 无法更改关系,因为一个或多个外键属性不可以为 null  . 同时修改主表和从表的数据,想用EF主表T_ReviewPlan中某个对象item删除item对应的从表T_ReviewS ...

  4. [android] 采用aidl绑定远程服务

    aidl:android interface definition language 安卓接口定义语言 在两个不同的应用程序里面使用同一个接口 使用场景:调用支付宝服务进行支付 先写远程服务端Seri ...

  5. Centos6.5安装MySQL5.6备忘记录

    Centos6.5安装MySQL5.6 1. 查看系统状态 [root@itzhouq32 tools]# cat /etc/issue CentOS release 6.5 (Final) Kern ...

  6. Mac包管理神器Homebrew

    概念 简称brew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件,相当于Red hat的yum.Ubuntu的apt-get. 安装命令 ruby -e "$( ...

  7. JavaScript黑客是这样窃取比特币的,Vue开发者不用担心!

    如果你是JavaScript或者区块链开发者,如果你有关注区块链以及比特币,那么你应该听说了比特币钱包Copay被黑客攻击的事情.但是,你知道这是怎么回事吗? 总结 比特币钱包copay依赖event ...

  8. vue(一)使用vue-cli搭建项目

    一.安装node.js 去官网下载安装node.js:    https://nodejs.org/en/ 安装完成后,可以在命令行工具(Windows是cmd,苹果是终端控制)输入node -v 和 ...

  9. Android项目实战(四十一):游戏和视频类型应用 状态栏沉浸式效果

    需求:  手机app ,当打游戏或者全屏看视频的时候会发现这时候手机顶部的状态栏是不显示的,当我们从手机顶端向下进行滑动或手机底端向上滑动的时候,状态栏会显示出来,如果短暂的几秒时间没有操作的话,状态 ...

  10. Stable Fur Generation on Mesh

    After tested the Maya 2015 XGen Grooming, we dropped it, that's really slow and unstable, totally no ...