#!pyton2
#-*- coding:utf-8 -*- for letter in "Python":
print "Current letter is:",letter fruits=["apple","mango","pear"] for fruit in fruits:
print fruit for index in range(len(fruits)):
print fruits[index] > python2 test.py
Current letter is: P
Current letter is: y
Current letter is: t
Current letter is: h
Current letter is: o
Current letter is: n
apple
mango
pear
apple
mango
pear
numbers=[1,2,3,4,5]
odd=[]
even=[] while len(numbers)>0:
number=numbers.pop()
if number%2==0:
even.append(number)
else:
odd.append(number) print "numbers: ",numbers
print "odd :" ,odd
print "even:",even numbers: []
odd : [5, 3, 1]
even: [4, 2]
#break and continue

i=1
while i<10:
i+=1
if i%2 >0:
continue
print i i=1
while 1:
print i
i+=1
if i>4:
break 2
4
6
8
10
1
2
3
4
#while,else

while count<3:
print count," is less than 3"
count+=1
else:
print count, "is not less than 3" #死循环
flag=1
while flag==1:
print "True!" 0 is less than 3
1 is less than 3
2 is less than 3
3 is not less than 3
True!
True!
True!
True!
True!
True!
True!
#!python2
#-*- coding:utf-8 -*- def test_fun(num,step):
i=0
numbers=[]
while i<num:
print "At the top i is %d " %i
numbers.append(i) i=i+step
print "Numbers now: ",numbers
print "At the bottom i is %d " % i print "The numbers:"
for n in numbers:
print n def test_fun2(num,step):
numbers=[]
for i in range(0,num,step):
print "At the top i is %d" %i
numbers.append(i)
print "Numbers now:", numbers print "The numbers:"
for index in range(len(numbers)):
print numbers[index] def find_prime(start_num,end_num):
for num in range(start_num,end_num):
for i in range(2,num):
if num%i==0:
print "%d = %d * %d" %(num,i,num/i)
break;
else:
print "%d is a prime" % num > python2
Enthought Canopy Python 2.7.11 | 64-bit | (default, Jun 11 2016, 11:33:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ex33_2 import *
>>> find_prime(10,20)
10 = 2 * 5
11 is a prime
12 = 2 * 6
13 is a prime
14 = 2 * 7
15 = 3 * 5
16 = 2 * 8
17 is a prime
18 = 2 * 9
19 is a prime
>>> test_fun(8,4)
At the top i is 0
Numbers now: [0]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 4]
At the bottom i is 8
The numbers:
0
4
>>> test_fun2(8,4)
At the top i is 0
Numbers now: [0]
At the top i is 4
Numbers now: [0, 4]
The numbers:
0
4

Python for loop and while loop的更多相关文章

  1. python list comprehension twos for loop 嵌套for循环

    list comprehension 后面可以有多个for loops,每个for后面可以有if [(x, y, x * y)for x in(0,1,2,3)for y in(0,1,2,3)if ...

  2. C 语言:返回两个数组中第一个相同元素的指针(我用了loop 、goto loop标签)

    // //  main.c //  Pointer_search // //  Created by ma c on 15/8/2. //  要求:通过指针查找,实现比较两个有序数组中的元素,输出两个 ...

  3. PYTHON ASYNCIO: FUTURE, TASK AND THE EVENT LOOP

    from :http://masnun.com/2015/11/20/python-asyncio-future-task-and-the-event-loop.html Event Loop On ...

  4. 简单了解一下事件循环(Event Loop)

    关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android.Python.Java和Go,这个也是我们团队的主要技术栈. Github:https:/ ...

  5. Why should I avoid blocking the Event Loop and the Worker Pool?

    Don't Block the Event Loop (or the Worker Pool) | Node.js https://nodejs.org/en/docs/guides/dont-blo ...

  6. PostgreSQL-PL/pgSQL-cursor,loop

    将spam_keyword表word字段的字符全部拆分,只是利用过程语言完成循环的操作而已. create or replace function proc1() returns setof text ...

  7. archlinux 加载loop模块,且设定loop设备个数

    如果loop模块没有编译进内核就要先加载loop模块 modprobe loop 然后更改/etc/modprobe.d/modprobe.conf(有些文章写是在/etc/modprobe.conf ...

  8. Run Loop详解

    Run loops是线程的基础架构部分.一个run loop就是一个事件处理循环,用来不停的调配工作以及处理输入事件.使用run loop的目的是使你的线程在有工作的时候工作,没有的时候休眠. Run ...

  9. Objective-C之run loop详解[转]

    做了一年多的IOS开发,对IOS和Objective-C深层次的了解还十分有限,大多还停留在会用API的级别,这是件挺可悲的事情.想学好一门语言还是需要深层次的了解它,这样才能在使用的时候得心应手,出 ...

随机推荐

  1. mybatis 模糊查询 like

    1.  参数中直接加入%% param.setUsername("%CD%");      param.setPassword("%11%"); <sel ...

  2. 带选择的sql简单用法

    一般写法: select * from itcast_topic order by (case type when 2 then 2 else 1 end ) desc ,postTime desc ...

  3. Linux 远程登录

    Linux一般作为服务器使用,而服务器一般放在机房,你不可能在机房操作你的Linux服务器. 这事我们就需要远程登录到Linux服务器来管理维护系统. Linux系统中是通过ssh服务实现的远程登录功 ...

  4. NetworkComms V3 之同时监听多端口

    NetworkComms网络通信框架序言 NetworkComms通信框架,是一款来自英国的c#语言编写的通信框架,历时6年研发,成熟稳定,性能可靠.   框架支持同时监听服务器上的多个端口,写法如下 ...

  5. 练习使用XRecyclerView,可上拉下拉刷新。

    package com.lixu.testxrecyclerview; import android.support.v7.app.AppCompatActivity; import android. ...

  6. Sharepoint的javascript客户端对象模型获取其他站点的list

    获取当前站点(子站点而不是站点集)下的list var clientContext = new SP.ClientContext.get_current(); var list=clientConte ...

  7. 在你决定从事iOS开发前需要清楚的几个问题

    作者:David McGraw  翻译:丁丁(jackiehoo) 原文:http://www.xmcgraw.com/what-you-need-to-know-to-start-learning- ...

  8. JavaScript基础--面向对象三大特性(八):继承封装多态

    一.构造函数基本用法:function 类名(参数列表){ 属性=参数值} function Person(name,age){ this.name = name; this.age = age; } ...

  9. The Layout Process on Mac OSX and iOS

    First we will recap the steps it takes to bring views on screen with Auto Layout enabled. When you’r ...

  10. fopen和fopen_s用法的比较 【zz】

    在定义FILE * fp 之后,fopen的用法是: fp = fopen(filename,"w").而对于fopen_s来说,还得定义另外一个变量errno_t err,然后e ...