一、创建函数

>>> import math

>>> x=1

>>> y=math.sqrt

>>> callable(x) #显示False

>>> callable(y) #显示True

1、def,用来创建函数

>>> def hello(name)

>>>  return 'Hello, '+name+'!'

>>> print(hello('world'))

显示:Hello, world

2、为函数增加说明

>>> def square(x):

>>>  'Calculates the square of the number x.'

>>>  return x *x

>>> square.__doc__

显示:'Calculates the square of the number x.'

通过help查看函数

>>> help(square)

二、函数参数

1、抽象函数

>>> def init(data):

>>>  data['first']={}

>>>  data['middle']={}

>>>  data['last']={}

>>> storage={}

>>> init(storage)

>>> storage

显示:{'middle':{},'last':{},'first':{}}

2、收集参数

>>> def print_params(x,y,z=3,*pospar,**keypar)

>>>  print(x,y,z)

>>>  print(pospar)

>>>  print(keypar)

显示:1 2 3    (5,6,7)  {'foo':1,'bar':2}

注:‘*’,单个星号用来联合普通参数,‘**’,两个星号用来处理关键字

3、power计算乘积

>>> power(3,2)

显示:9

4、interval,间隔

>>> interval(10)

显示:[0,1,2,3,4,5,6,7,8,9]

>>> interval(1,5)

显示:[1,2,3,4]

5、当参数名和全局变量名一样时,全局变量会被屏蔽,不能直接访问,如果要想访问,就需要借助globals

>>> def combine(parameter):

>>>  print(parameter+globals()['parameter'])

>>> parameter='berry'

>>> combine('Shrub')

显示:Shrubberry

Python学习笔记之抽象的更多相关文章

  1. python学习笔记3_抽象

    这一步的学习四个知识点,如何将语句组织成函数,参数,作用域(scope),和递归 一.函数 1.抽象和结构 抽象可以节省很多的工作量,实际上它的作用更大,它是使得计算机程序让人读懂的关键(这也是最基本 ...

  2. 【python学习笔记】7.更加抽象

    [python学习笔记]7.更加抽象 类的定义就是执行代码块 在内存保存一个原始实例,可以通过类名来访问 类的实例化,是创建一个原始实例的副本, 并且所有成员变量与原始实例绑定 通过修改实例变量,可以 ...

  3. 【python学习笔记】6.抽象

    [python学习笔记]6.抽象 创建函数: 使用def语句定义函数,不用声明参数类型,和返回值类型 def function_name(param1, param2): 'this is docum ...

  4. python学习笔记4_类和更抽象

    python学习笔记4_类和更抽象 一.对象 class 对象主要有三个特性,继承.封装.多态.python的核心. 1.多态.封装.继承 多态,就算不知道变量所引用的类型,还是可以操作对象,根据类型 ...

  5. 【Python学习笔记之二】浅谈Python的yield用法

    在上篇[Python学习笔记之一]Python关键字及其总结中我提到了yield,本篇文章我将会重点说明yield的用法 在介绍yield前有必要先说明下Python中的迭代器(iterator)和生 ...

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

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

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

    本节讲卷积神经网络的可视化 三种方法 可视化卷积神经网络的中间输出(中间激活) 有助于理解卷积神经网络连续的层如何对输入进行变换,也有助于初步了解卷积神经网络每个过滤器的含义 可视化卷积神经网络的过滤 ...

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

    本节介绍基于Keras的使用预训练模型方法 想要将深度学习应用于小型图像数据集,一种常用且非常高效的方法是使用预训练网络.预训练网络(pretrained network)是一个保存好的网络,之前已在 ...

  9. Deep learning with Python 学习笔记(2)

    本节介绍基于Keras的CNN 卷积神经网络接收形状为 (image_height, image_width, image_channels)的输入张量(不包括批量维度),宽度和高度两个维度的尺寸通常 ...

随机推荐

  1. A ship is always safe at the shore - but that is not what it is built for.

    A ship is always safe at the shore - but that is not what it is built for. 船靠岸边总是安全的,但那不是建造它的目的.

  2. Spring源代码解析

    Spring源代码解析(一):IOC容器:http://www.iteye.com/topic/86339 Spring源代码解析(二):IoC容器在Web容器中的启动:http://www.itey ...

  3. python读取和写入csv文件

    读取csv文件: def readCsv(): rows=[] with file(r'E:\py\py01\Data\system.csv','rb') as f: reads=csv.reader ...

  4. Orcle数据库 表的 内置函数 内链接 外连接 相关练习题

  5. MongoDB由于目标计算机积极拒绝,无法连接

    遇到这个问题的时候,可以通过以下步骤解决: 1.打开Mongo安装包:进入Mongo下的data文件夹下的db文件夹,找到Mongod.lock,删除. 2.在命令行中输入: mongod.exe - ...

  6. 贪吃蛇,JavaScript,效果,鼠标事件

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>鼠 ...

  7. [LeetCode_2] Add Two Numbers

    LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...

  8. 使用ajax分页

    前台页面: <table class="table table-hover"> <thead> <tr> <th class='hidde ...

  9. js创建节点及其属性

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. 《简明python教程》笔记一

    读<简明Python教程>笔记: 本书的官方网站是www.byteofpython.info  安装就不说了,网上很多,这里就记录下我在安装时的问题,首先到python官网下载,选好安装路 ...