我们可爱的Python讲述.ppt

  1. 1、本文档共86页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
我们可爱的Python讲述

# Producer thread class Producer(threading.Thread): def __init__(self, threadname, queue): threading.Thread.__init__(self, name = threadname) self.sharedata = queue def run(self): for i in range(20): print self.getName(),adding,i,to queue self.sharedata.put(i) time.sleep(random.randrange(10)/10.0) print self.getName(),Finished # Consumer thread class Consumer(threading.Thread): def __init__(self, threadname, queue): threading.Thread.__init__(self, name = threadname) self.sharedata = queue def run(self): for i in range(20): print self.getName(),got a value:,self.sharedata.get() time.sleep(random.randrange(10)/10.0) print self.getName(),Finished‘ # Main thread def main(): queue = Queue() producer = Producer(Producer, queue) consumer = Consumer(Consumer, queue) 示例代码中实现了两个类:生产者类Producer和消费者类Consumer。前者在一个随机的时间内放入一个值到队列queue中然后显示出来,后者在一定随机的时间内从队列queue中取出一个值并显示出来。 print Starting threads ... producer.start() consumer.start() producer.join() consumer.join() print All threads have terminated. 运行结果: Page * Page * if 叙述 for 叙述 break 及 continue 叙述 pass 叙述 while 叙述 try..except叙述 特殊的yield Page * 大概最为人所知的 statement 就是 if 叙述了,举例如下: x = int(raw_input(Please enter a number: )) if x 0: ... x = 0 ... print Negative changed to zero ... elif x == 0: ... print Zero ... elif x == 1: ... print Single ... else: ... print More... elif 的部份可以没有也可以有很多个, else 部分可以有一个也可以没有。 `elif 这个关键词是`else if的简化,而且有减少过分缩排的效果。 用 if ... elif ... elif ... 这样的写法可以来取代在其它一些程序语言中常见的 switch 或是 case 的写法。 Page * 在Python里的 for 叙述的用法与在C或是Pascal里的用法有所不同。不像是在Pascal中一定要执行某个数目的循环,也不像是在C中让使用者决定执行的进度(step)及结束执行的条件,Python的 for 叙述会将一个系列(sequence,像是list或是string)里所有的成员走遍一次,执行的顺序是依照成员在squence里的顺序。以下是一个例子: # Measure some strings: ... a = [cat, window, defenestrate] for x in a: ... print x, len(x) ... cat 3 window 6 defenestrate

文档评论(0)

shuwkb + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档