调用函数:

#!/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调用和定义的更多相关文章

  1. python学习笔记_集合的定义和常用方法

    1.认识集合 定义: s={1,2,3,4,5} s=set("hello") s=set(["steven","job","da ...

  2. Python学习笔记(八)

    Python学习笔记(八): 复习回顾 递归函数 内置函数 1. 复习回顾 1. 深浅拷贝 2. 集合 应用: 去重 关系操作:交集,并集,差集,对称差集 操作: 定义 s1 = set('alvin ...

  3. Deep learning with Python 学习笔记(10)

    生成式深度学习 机器学习模型能够对图像.音乐和故事的统计潜在空间(latent space)进行学习,然后从这个空间中采样(sample),创造出与模型在训练数据中所见到的艺术作品具有相似特征的新作品 ...

  4. Deep learning with Python 学习笔记(1)

    深度学习基础 Python 的 Keras 库来学习手写数字分类,将手写数字的灰度图像(28 像素 ×28 像素)划分到 10 个类别 中(0~9) 神经网络的核心组件是层(layer),它是一种数据 ...

  5. Python学习笔记之模块与包

    一.模块 1.模块的概念 模块这一概念很大程度上是为了解决代码的可重用性而出现的,其实这一概念并没有多复杂,简单来说不过是一个后缀为 .py 的 Python 文件而已 例如,我在某个工作中经常需要打 ...

  6. Python学习笔记之类与对象

    这篇文章介绍有关 Python 类中一些常被大家忽略的知识点,帮助大家更全面的掌握 Python 中类的使用技巧 1.与类和对象相关的内置方法 issubclass(class, classinfo) ...

  7. Python学习笔记之函数

    这篇文章介绍有关 Python 函数中一些常被大家忽略的知识点,帮助大家更全面的掌握 Python 中函数的使用技巧 1.函数文档 给函数添加注释,可以在 def 语句后面添加独立字符串,这样的注释被 ...

  8. Python学习笔记(四)函数式编程

    高阶函数(Higher-order function) Input: 1 abs Output: 1 <function abs> Input: 1 abs(-10) Output: 1 ...

  9. Python 学习笔记(下)

    Python 学习笔记(下) 这份笔记是我在系统地学习python时记录的,它不能算是一份完整的参考,但里面大都是我觉得比较重要的地方. 目录 Python 学习笔记(下) 函数设计与使用 形参与实参 ...

随机推荐

  1. 项目实战15.2—企业级堡垒机 jumpserver快速入门

    必备条件 硬件条件 ① 一台安装好 Jumpserver 系统的可用主机(堡垒机) ② 一台或多台可用的 Linux.Windows资产设备(被管理的资产) 服务条件 (1)coco服务 ① 鉴于心态 ...

  2. 利用JAVA多线程来提高数据处理效率

    肿瘤大数据挖掘中经常需要处理上百亿行的文本文件,这些文件往往高达数百GB,假如文件结构简单统一,那么用sed和awk 处理是非常方便和快速的.但有时候会遇到逻辑较为复杂的处理流程,这样我一般会用JAV ...

  3. COS对象存储服务的使用

    ---------------------------------------------------------------------------------------------[版权申明:本 ...

  4. Android自定义View(LimitScrollerView-仿天猫广告栏上下滚动效果)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/53303872 本文出自:[openXu的博客] 1分析 2定义组合控件布局 3继承最外层控件 ...

  5. NLP系列(1)_从破译外星人文字浅谈自然语言处理基础

    作者:龙心尘 &&寒小阳 时间:2016年1月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50543337 ht ...

  6. 剑指Offer——知识点储备-网络基础

    剑指Offer--知识点储备-网络基础 计算机网络 http和https的区别 (1)http是http协议运行在tcp之上,所传输的内容都是明文,客户端和服务器端都无法验证对方的身份. (2)htt ...

  7. How Do I Declare A Block in Objective-C? [备忘]

    How Do I Declare A Block in Objective-C? As a local variable: returnType (^blockName)(parameterTypes ...

  8. Kafka系列之-Kafka入门

    接下来的这些博客,主要内容来自<Learning Apache Kafka Second Edition>这本书,书不厚,200多页.接下来摘录出本书中的重要知识点,偶尔参考一些网络资料, ...

  9. React Native组件只Image

    不管在Android还是在ios原生的开发中,图片都是作为控件给出来的,在RN中也有这么一个控件(Image).根据官网的资料,图片分为本地静态图片,网络图片和混合app资源.一下分类介绍来源官网. ...

  10. [ExtJS5学习笔记]第八节 Extjs5的Ext.toolbar.Toolbar工具条组件及其应用

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38515499 本文作者:sushengmiyan ------------------ ...