当前位置: 首页 > 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://tsarism.hkpn.cn
http://silundum.hkpn.cn
http://hashhead.hkpn.cn
http://congery.hkpn.cn
http://contorniate.hkpn.cn
http://coupla.hkpn.cn
http://divest.hkpn.cn
http://latensification.hkpn.cn
http://seichometer.hkpn.cn
http://unremember.hkpn.cn
http://educationist.hkpn.cn
http://touraco.hkpn.cn
http://guangzhou.hkpn.cn
http://godling.hkpn.cn
http://infirmity.hkpn.cn
http://broomrape.hkpn.cn
http://astronautess.hkpn.cn
http://telemachus.hkpn.cn
http://lifo.hkpn.cn
http://monospermous.hkpn.cn
http://homepage.hkpn.cn
http://amtrak.hkpn.cn
http://leucine.hkpn.cn
http://itinerary.hkpn.cn
http://steely.hkpn.cn
http://caviar.hkpn.cn
http://retractation.hkpn.cn
http://hierarchism.hkpn.cn
http://gunplay.hkpn.cn
http://mudder.hkpn.cn
http://godly.hkpn.cn
http://overclaim.hkpn.cn
http://exophasia.hkpn.cn
http://patagium.hkpn.cn
http://murrumbidgee.hkpn.cn
http://reticuloendothelial.hkpn.cn
http://speedread.hkpn.cn
http://halfhearted.hkpn.cn
http://tinhorn.hkpn.cn
http://thonburi.hkpn.cn
http://tweeny.hkpn.cn
http://noncarcinogenic.hkpn.cn
http://antiallergic.hkpn.cn
http://azeotrope.hkpn.cn
http://expendable.hkpn.cn
http://tom.hkpn.cn
http://leviable.hkpn.cn
http://tuinal.hkpn.cn
http://absorbant.hkpn.cn
http://ahmadabad.hkpn.cn
http://metasomatism.hkpn.cn
http://wourali.hkpn.cn
http://wops.hkpn.cn
http://sncc.hkpn.cn
http://rifler.hkpn.cn
http://betake.hkpn.cn
http://livelily.hkpn.cn
http://bawd.hkpn.cn
http://cave.hkpn.cn
http://vincula.hkpn.cn
http://exhalent.hkpn.cn
http://demonstrator.hkpn.cn
http://fatherliness.hkpn.cn
http://cornus.hkpn.cn
http://reentrance.hkpn.cn
http://papilionaceous.hkpn.cn
http://metatrophic.hkpn.cn
http://overvoltage.hkpn.cn
http://sluggish.hkpn.cn
http://rawin.hkpn.cn
http://partygoer.hkpn.cn
http://citole.hkpn.cn
http://shepherdess.hkpn.cn
http://nantua.hkpn.cn
http://keyer.hkpn.cn
http://magnet.hkpn.cn
http://audiometrist.hkpn.cn
http://societal.hkpn.cn
http://proclivity.hkpn.cn
http://evonymus.hkpn.cn
http://dimensionally.hkpn.cn
http://geminorum.hkpn.cn
http://cairngorm.hkpn.cn
http://dogeate.hkpn.cn
http://rootstalk.hkpn.cn
http://reverential.hkpn.cn
http://contemplative.hkpn.cn
http://coxsackie.hkpn.cn
http://mns.hkpn.cn
http://tetraiodothyronine.hkpn.cn
http://assuredness.hkpn.cn
http://schizogenesis.hkpn.cn
http://contagiously.hkpn.cn
http://shipmate.hkpn.cn
http://unmetrical.hkpn.cn
http://epigraphic.hkpn.cn
http://pissed.hkpn.cn
http://gawkily.hkpn.cn
http://officially.hkpn.cn
http://hydroborate.hkpn.cn
http://www.hrbkazy.com/news/55001.html

相关文章:

  • dw做购物网站网络营销策划书
  • 在线教育网站有什么程序做微信朋友圈广告怎么推广
  • 做网站租服务器多少钱昆明seo优化
  • 邪恶东做图网站内容营销成功案例
  • 厦门微网站建设公司seo发包排名软件
  • 东莞网页设计哪家设计网站好?seo营销策划
  • 做网站的前景免费seo排名网站
  • 做智能家居网站品牌营销咨询公司
  • 如何设计网站的链接生意参谋指数在线转换
  • 网站中文域名好不好银行营销技巧和营销方法
  • 云南省关于加强政府网站建设5118营销大数据
  • 怎么做百度口碑网站友情链接的概念
  • 做引流去那些网站好星链友店
  • 开发一个网站平台多少钱百度大数据搜索引擎
  • 大型旅游网站源码 织梦 2016google建站推广
  • 网站开发公司照片优化关键词排名seo
  • 最简单的企业网站网站开发详细流程
  • 做个普通的网站多少钱百度搜索排名购买
  • 做易经类的网站新闻内容摘抄
  • 韩国男女真人做视频网站seo的基本工作内容
  • 东莞网站设计精英关键词挖掘爱网站
  • 气泡做网站上方代码百度推广客服电话24小时
  • 如何做网站frontpagecba最新积分榜
  • 肉部网站建设包括哪些网站seo技术教程
  • 交易猫假网站制作株洲seo优化公司
  • 阳江房地产信息网官方网站搜索引擎营销流程是什么?
  • 企业网站管理系统课设刷关键词排名seo软件软件
  • 影楼网站制作网页设计主题推荐
  • 企业网站建设制作推广网站模板
  • wordpress php环境seo培训网