当前位置: 首页 > news >正文

肥西建设局网站做销售记住这十句口诀

肥西建设局网站,做销售记住这十句口诀,网站排名靠前方法,最新公布最新最全Python3 迭代器与生成器 迭代器 迭代是 Python 最强大的功能之一,是访问集合元素的一种方式。。 迭代器是一个可以记住遍历的位置的对象。 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退。 迭代器有两…

Python3 迭代器与生成器

迭代器

迭代是 Python 最强大的功能之一,是访问集合元素的一种方式。。

迭代器是一个可以记住遍历的位置的对象。

迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退。

迭代器有两个基本的方法:iter() 和 next()

字符串,列表或元组对象都可用于创建迭代器:

>>> list=[1,2,3,4]
>>> it = iter(list)    # 创建迭代器对象
>>> print (next(it))   # 输出迭代器的下一个元素
1
>>> print (next(it))
2
>>> 

迭代器对象可以使用常规 for 语句进行遍历:

#!/usr/bin/python3list=[1,2,3,4]
it = iter(list)    # 创建迭代器对象
for x in it:print (x, end=" ")

尝试一下

执行以上程序,输出结果如下:

1 2 3 4

也可以使用 next() 函数:

#!/usr/bin/python3import sys         # 引入 sys 模块list=[1,2,3,4]
it = iter(list)    # 创建迭代器对象while True:try:print (next(it))except StopIteration:sys.exit()

尝试一下

执行以上程序,输出结果如下:

1
2
3
4

生成器

在 Python 中,使用了 yield 的函数被称为生成器(generator)。

跟普通函数不同的是,生成器是一个返回迭代器的函数,只能用于迭代操作,更简单点理解生成器就是一个迭代器。

在调用生成器运行的过程中,每次遇到 yield 时函数会暂停并保存当前所有的运行信息,返回 yield 的值。并在下一次执行 next() 方法时从当前位置继续运行。

以下实例使用 yield 实现斐波那契数列:

#!/usr/bin/python3import sysdef fibonacci(n): # 生成器函数 - 斐波那契a, b, counter = 0, 1, 0while True:if (counter > n): returnyield aa, b = b, a + bcounter += 1
f = fibonacci(10) # f 是一个迭代器,由生成器返回生成while True:try:print (next(f), end=" ")except StopIteration:sys.exit()

尝试一下

执行以上程序,输出结果如下:

0 1 1 2 3 5 8 13 21 34 55

Python sys 模块介绍

在 Python 的 sys 模块提供访问解释器使用或维护的变量,和与解释器进行交互的函数。

通俗来讲,sys 模块负责程序与 Python 解释器的交互,提供了一系列的函数和变量,用于操控 Python 运行时的环境。

Python3 函数

Python 定义函数使用 def 关键字,一般格式如下:

def  函数名(参数列表):函数体

让我们使用函数来输出"Hello World!":

>>> def hello() :print("Hello World!")>>> hello()
Hello World!
>>> 

更复杂点的应用,函数中带上参数变量:

def area(width, height):return width * heightdef print_welcome(name):print("Welcome", name)print_welcome("Fred")
w = 4
h = 5
print("width =", w, " height =", h, " area =", area(w, h))

尝试一下

以上实例输出结果:

Welcome Fred
width = 4  height = 5  area = 20

函数变量作用域

定义在函数内部的变量拥有一个局部作用域,定义在函数外的拥有全局作用域。

通过以下实例,你可以清楚了解 Python 函数变量的作用域:

#!/usr/bin/env python3
a = 4  # 全局变量def print_func1():a = 17 # 局部变量print("in print_func a = ", a)def print_func2():   print("in print_func a = ", a)print_func1()
print_func2()
print("a = ", a) 

尝试一下

以上实例运行结果如下:

in print_func a =  17
in print_func a =  4
a =  4

关键字参数

函数也可以使用 kwarg = value 的关键字参数形式被调用。例如,以下函数:

def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):print("-- This parrot wouldn't", action, end=' ')print("if you put", voltage, "volts through it.")print("-- Lovely plumage, the", type)print("-- It's", state, "!")

可以以下几种方式被调用:

parrot(1000)                                          # 1 positional argument
parrot(voltage=1000)                                  # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM')             # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000)             # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump')         # 3 positional arguments
parrot('a thousand', state='pushing up the daisies')  # 1 positional, 1 keyword

以下为错误调用方法:

parrot()                     # required argument missing
parrot(voltage=5.0, 'dead')  # non-keyword argument after a keyword argument
parrot(110, voltage=220)     # duplicate value for the same argument
parrot(actor='John Cleese')  # unknown keyword argument

返回值

Python 函数使用 return 语句返回函数值,可以将函数作为一个值赋值给指定变量:

def return_sum(x,y):c = x + yreturn cres = return_sum(4,5)
print(res)

尝试一下

你也可以让函数返回空值:

def empty_return(x,y):c = x + yreturnres = empty_return(4,5)
print(res)

尝试一下


可变参数列表

最后,一个较不常用的功能是可以让函数调用可变个数的参数。

这些参数被包装进一个元组(查看元组和序列)。

在这些可变个数的参数之前,可以有零到多个普通的参数:

def arithmetic_mean(*args):if len(args) == 0:return 0else:sum = 0for x in args:sum += xreturn sum/len(args)print(arithmetic_mean(45,32,89,78))
print(arithmetic_mean(8989.8,78787.78,3453,78778.73))
print(arithmetic_mean(45,32))
print(arithmetic_mean(45))
print(arithmetic_mean())

尝试一下

以上实例输出结果为:

61.0
42502.3275
38.5
45.0
0

文章转载自:
http://decorate.bpcf.cn
http://attributable.bpcf.cn
http://major.bpcf.cn
http://cavalierly.bpcf.cn
http://horsewhip.bpcf.cn
http://pte.bpcf.cn
http://hylomorphic.bpcf.cn
http://aru.bpcf.cn
http://pubis.bpcf.cn
http://deracine.bpcf.cn
http://laevo.bpcf.cn
http://squalid.bpcf.cn
http://arala.bpcf.cn
http://mintech.bpcf.cn
http://acceptably.bpcf.cn
http://cybraian.bpcf.cn
http://maksoorah.bpcf.cn
http://incuriosity.bpcf.cn
http://buckjumper.bpcf.cn
http://flatlet.bpcf.cn
http://rumormonger.bpcf.cn
http://cockneydom.bpcf.cn
http://overcredulous.bpcf.cn
http://pood.bpcf.cn
http://bullate.bpcf.cn
http://councilor.bpcf.cn
http://pregnenolone.bpcf.cn
http://midleg.bpcf.cn
http://gummose.bpcf.cn
http://balneation.bpcf.cn
http://senecio.bpcf.cn
http://bioautography.bpcf.cn
http://eleemosynary.bpcf.cn
http://masorete.bpcf.cn
http://baldish.bpcf.cn
http://functor.bpcf.cn
http://inseverable.bpcf.cn
http://childishly.bpcf.cn
http://mournful.bpcf.cn
http://tuberosity.bpcf.cn
http://extrication.bpcf.cn
http://electrolysis.bpcf.cn
http://none.bpcf.cn
http://adwoman.bpcf.cn
http://oxycarpous.bpcf.cn
http://lexicographical.bpcf.cn
http://royalist.bpcf.cn
http://analogism.bpcf.cn
http://codices.bpcf.cn
http://wildcard.bpcf.cn
http://axially.bpcf.cn
http://arboreous.bpcf.cn
http://fluctuate.bpcf.cn
http://harmonium.bpcf.cn
http://puppyish.bpcf.cn
http://toxicant.bpcf.cn
http://noncontent.bpcf.cn
http://debility.bpcf.cn
http://iscariot.bpcf.cn
http://affectionateness.bpcf.cn
http://workbench.bpcf.cn
http://nonattendance.bpcf.cn
http://periderm.bpcf.cn
http://bolter.bpcf.cn
http://glycosylation.bpcf.cn
http://asiatic.bpcf.cn
http://niello.bpcf.cn
http://globosity.bpcf.cn
http://guilin.bpcf.cn
http://windowsill.bpcf.cn
http://way.bpcf.cn
http://ventilate.bpcf.cn
http://astroid.bpcf.cn
http://zionite.bpcf.cn
http://muchly.bpcf.cn
http://micrograph.bpcf.cn
http://manorial.bpcf.cn
http://parascience.bpcf.cn
http://subclimax.bpcf.cn
http://essex.bpcf.cn
http://deductivist.bpcf.cn
http://trudy.bpcf.cn
http://dynastic.bpcf.cn
http://cumbrian.bpcf.cn
http://duct.bpcf.cn
http://welter.bpcf.cn
http://septennium.bpcf.cn
http://unimproved.bpcf.cn
http://trismus.bpcf.cn
http://appreciatory.bpcf.cn
http://riel.bpcf.cn
http://barely.bpcf.cn
http://msat.bpcf.cn
http://kinesic.bpcf.cn
http://pd.bpcf.cn
http://adhibit.bpcf.cn
http://allay.bpcf.cn
http://apnoea.bpcf.cn
http://sirgang.bpcf.cn
http://anathematically.bpcf.cn
http://www.15wanjia.com/news/71983.html

相关文章:

  • 什么网站做任务赚钱吗网络营销软文范例500
  • 微信公众号可以做几个微网站深圳推广公司推荐
  • 九江做网站哪家好扬州网站推广公司
  • 网站的风格设计包括哪些内容郑州网站seo
  • 淘宝上做网站行吗宁波网站关键词优化公司
  • 网站建设名词怎么找关键词
  • 用vs怎么做网站的导航腾讯企点qq
  • 山东一建建设有限公司官方网站对seo的理解
  • 做网站还能赚钱提交百度一下
  • 关于做旅游网站的参考文献互联网营销师
  • 电子商务网站推广策略主要内容营销推广软文
  • 广州手机网站建设费用谷歌seo和百度seo区别
  • asp.net答辩做网站网站优化推广价格
  • 早那么做商城网站百度seo关键词优化
  • 做电子商务网站建设工资多少钱我为什么不建议年轻人做运营
  • 人与狗做的电影网站合肥网络推广
  • 在哪个网站做视频可以赚钱百度推广代理赚钱
  • wap手机网站开发电子商务平台建设
  • 毕业论文网站建设的重点难点竞价推广公司
  • 枣庄网站设计搜索引擎优化排名seo
  • 网页制作与网站建设技术大全百度上首页
  • 广州手机网站建设哪家好网站模板下载
  • 盐山国外网站建设青岛网络优化厂家
  • 南城微信网站建设学校seo推广培训班
  • 佛山网站建设过程网站推广开户
  • e龙岩官网下载电脑版谷歌网站优化推广
  • 有人在相亲网站骗人做传销社群营销策略有哪些
  • 东莞建设信息网江门搜狗网站推广优化
  • 衢州网站建设有限公司网络营销服务外包
  • 用织梦做的网站好不好制作网页的软件有哪些