Python学习笔记 - function调用和定义
调用函数:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 函数调用
>>> abs(100)
100
>>> abs(-110)
110
>>> abs(12.34)
12.34
>>> abs(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: abs() takes exactly one argument (2 given)
>>> abs('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
>>> max(1, 2)
2
>>> max(2, 3, 1, -5)
3
>>> int('123')
123
>>> int(12.34)
12
>>> str(1.23)
'1.23'
>>> str(100)
'100'
>>> bool(1)
True
>>> bool('')
False
>>> a = abs # 变量a指向abs函数,相当于引用
>>> a(-1) # 所以也可以通过a调用abs函数
1
>>> n1 = 255
>>> n2 = 1000
>>> print(hex(n1))
0xff
>>> print(hex(n2))
0x3e8
定义函数:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#函数定义
def myAbs(x):
if x >= 0:
return x
else:
return -x
a = 10
myAbs(a)
def nop(): # 空函数
pass
#pass语句什么都不做
#实际上pass可以用来作为占位符,比如现在还没想好怎么写函数
#代码,就可以先写一个pass,让代码运行起来。
if age >= 18:
pass
#缺少了pass,代码就会有语法错误
>>> if age >= 18:
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
>>> myAbs(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: myAbs() takes 1 positional argument but 2 were given
>>> myAbs('A')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in myAbs
TypeError: unorderable types: str() >= int()
>>> abs('A')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
def myAbs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x >= 0:
return x
else:
return -x
>>> myAbs('A')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in myAbs
TypeError: bad operand type
# 返回两个值?
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(x, y)
151.96152422706632 70.0
#其实上面只是一种假象,Python函数返回的仍然是单一值
>>> r = move(100, 100, 60, math.pi / 6)
>>> print(r)
(151.96152422706632, 70.0)
#实际上返回的是一个tuple!
#但是,语法上,返回一个tuple可以省略括号,
#而多个变量可以同时接受一个tuple,按位置赋给对应的值
#所以,Python的函数返回多值实际就是返回一个tuple
#但是写起来更方便
#函数执行完毕也没有return语句时,自动return None。
#练习
import math
def quadratic(a, b, c):
x1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a)
x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a)
return x1, x2
x1, x2 = quadratic(2, 5, 1)
print(x1, x2)
>>> import math
>>> def quadratic(a, b, c):
... x1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a)
... x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a)
... return x1, x2
...
>>> x1, x2 = quadratic(2, 5, 1)
>>> print(x1, x2)
-0.21922359359558485 -2.2807764064044154
Python学习笔记 - function调用和定义的更多相关文章
- python学习笔记_集合的定义和常用方法
1.认识集合 定义: s={1,2,3,4,5} s=set("hello") s=set(["steven","job","da ...
- Python学习笔记(八)
Python学习笔记(八): 复习回顾 递归函数 内置函数 1. 复习回顾 1. 深浅拷贝 2. 集合 应用: 去重 关系操作:交集,并集,差集,对称差集 操作: 定义 s1 = set('alvin ...
- Deep learning with Python 学习笔记(10)
生成式深度学习 机器学习模型能够对图像.音乐和故事的统计潜在空间(latent space)进行学习,然后从这个空间中采样(sample),创造出与模型在训练数据中所见到的艺术作品具有相似特征的新作品 ...
- Deep learning with Python 学习笔记(1)
深度学习基础 Python 的 Keras 库来学习手写数字分类,将手写数字的灰度图像(28 像素 ×28 像素)划分到 10 个类别 中(0~9) 神经网络的核心组件是层(layer),它是一种数据 ...
- Python学习笔记之模块与包
一.模块 1.模块的概念 模块这一概念很大程度上是为了解决代码的可重用性而出现的,其实这一概念并没有多复杂,简单来说不过是一个后缀为 .py 的 Python 文件而已 例如,我在某个工作中经常需要打 ...
- Python学习笔记之类与对象
这篇文章介绍有关 Python 类中一些常被大家忽略的知识点,帮助大家更全面的掌握 Python 中类的使用技巧 1.与类和对象相关的内置方法 issubclass(class, classinfo) ...
- Python学习笔记之函数
这篇文章介绍有关 Python 函数中一些常被大家忽略的知识点,帮助大家更全面的掌握 Python 中函数的使用技巧 1.函数文档 给函数添加注释,可以在 def 语句后面添加独立字符串,这样的注释被 ...
- Python学习笔记(四)函数式编程
高阶函数(Higher-order function) Input: 1 abs Output: 1 <function abs> Input: 1 abs(-10) Output: 1 ...
- Python 学习笔记(下)
Python 学习笔记(下) 这份笔记是我在系统地学习python时记录的,它不能算是一份完整的参考,但里面大都是我觉得比较重要的地方. 目录 Python 学习笔记(下) 函数设计与使用 形参与实参 ...
随机推荐
- c++DLL编程详解
DLL(Dynamic Link Library)的概念,你可以简单的把DLL看成一种仓库,它提供给你一些可以直接拿来用的变量.函数或类.在仓库的发展史上经历了“无库-静态链接库-动态链接库”的时代. ...
- 关于centos版本安装ethereum钱包
安装go wget https://studygolang.com/dl/golang/go1.9.linux-amd64.tar.gz --no-check-certificatetar -zxvf ...
- 小程序上拉下拉共存时不可使用scroll-view的解决方法
使用 bindscrolltolower ,必须搭配使用的 scroll-view 会导致小程序 "enablePullDownRefresh": true 下拉不能使用. 解决方 ...
- python学习之路基础篇(第八篇)
一.作业(对象的封装) 要点分析 1.封装,对象中嵌套对象 2.pickle,load,切记,一定要先导入相关的类二.上节内容回顾和补充 面向对象基本知识: 1.类和对象的关系 2.三大特性: 封装 ...
- python学习之路基础篇(第五篇)
前四天课程回顾 1.python简介 2.python基本数据类型 类: int:整型 | str:字符串 | list:列表 |tuple:元组 |dict:字典 | set:集合 对象: li = ...
- Android 性能优化(一)内存篇
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/54377370 本文出自:[余志强的博客] 本博客同时也发布在 Hoo ...
- 安卓高级6 玩转AppBarLayout,更酷炫的顶部栏 Toolbar
原文大神地址:http://www.jianshu.com/p/d159f0176576 上一篇文章[<CoordinateLayout的使用如此简单 >]上一篇文章<Coordin ...
- XListView下拉刷新和上拉加载更多详解
转载本专栏每一篇博客请注明转载出处地址,尊重原创.博客链接地址:小杨的博客 http://blog.csdn.net/qq_32059827/article/details/53167655 市面上有 ...
- RE模块错误已解决.
下面这个错误是由于在正则[...]的内部,减号'-'是一个有特殊含义的字符(代表字符范围) 所以如果需要在[...]内匹配减号'-',需要用反斜杠'\'转义. >>> import ...
- String、StringBuffer、StringBuilder对比
1.String 用于存放字符的数组被声明为final的,因此只能赋值一次,不可再更改.这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间. Str ...