Python3基础-函数实例学习
内置函数
绝对值函数
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基础-函数实例学习的更多相关文章
- Python3基础 函数 关键字参数 的示例
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- Python3基础 函数名.__doc__显示一个函数的单行与多行函数文档
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- Python3基础 函数 递归 阶乘与斐波那契数列
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Python3基础 函数 函数名赋值操作
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- Python3基础 函数 参数为list 使用+=会影响到外部的实参
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- Python3基础 函数 参数 多个参数都有缺省值,需要指定参数进行赋值
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- Python3基础——函数
ython 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可 ...
- Python3基础 函数 收集参数+普通参数 的示例
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- Python3基础 函数 默认值参数示例
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
随机推荐
- WebApi HTTP Put 405 Method not allowed
总结列表: 1. There is already an open DataReader associated with this Connection which must be closed fi ...
- 【转载】Sqlserver中查询窗口显示行号
在Sqlserver中编写语句的时候,有时候因为业务逻辑比较复杂,编写的语句会比较多,此时如果编辑器中显示代码的行号,则对于我们的语句编写有很好的辅助作用.sqlserver默认未开启行号显示功能,可 ...
- swoole扩展实现真正的数据库连接池
php的数据库连接池一直以来都是一个难题,很多从php语言转向java的项目,大多数原因可能都是因为java有更好的连接池实现.php的mysql扩展提供了长连接的API,但在php机器数量较多,规模 ...
- MyBatis:Pagehelper分页
对于分页插件这里选择查询所有用户的信息,以列表返回 前端只需输入分页数的数据既可 service实现类也很方便,甚至我都开始有点喜欢上这种Example的SQL形式了. 最后页面调用url的json信 ...
- Add Again(重复元素排序) UVA11076
Add Again Summation of sequence of integers is always a common problem in Computer Science. Rather t ...
- xml方式封装数据方法
1.xml方式封装数据方法 2.demo <?php xml方式封装数据方法 /** * [xmlEncode description] * @param [type] $code [descr ...
- 带你使用JS-SDK自定义微信分享效果
前言 想必各位在写wap端时都遇到过这样的场景吧 ----自定义分享标题.图片.描述 接下来小编给大家讲解下分享相关操作 预期效果 原始的分享效果: 使用微信JS-SDK的分享效果: 可以看出缩略图, ...
- 3:Python条件语句
1.if基本语句 if 条件: 内部代码块 内部代码块 else …… print('……') 2.if支持嵌套 if 1==1: if 2==2 print(n1) print(n2) else: ...
- vue-cli 3.x 开发插件并发布到 npm
为了摆脱咸鱼的身份,我给自己定了一个开源项目的目标 于是抽空写了一个 textarea,打算发布到 npm 的时候却遇到了问题 之前用 vue-cli 2.x 的时候,打包配置项非常透明,可以很容易的 ...
- 2018-11-21 手工翻译Vue.js源码第一步:14个文件重命名
背景 对现有开源项目的代码进行翻译(文件名/命名/注释) · Issue #107 · program-in-chinese/overview 简单地说, 通过翻译源码, 提高项目代码可读性(对于母语 ...