#!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. Deep Learning 12_深度学习UFLDL教程:Sparse Coding_exercise(斯坦福大学深度学习教程)

    前言 理论知识:UFLDL教程.Deep learning:二十六(Sparse coding简单理解).Deep learning:二十七(Sparse coding中关于矩阵的范数求导).Deep ...

  2. SQL Server常用语句

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  3. 火狐 about:config

    1. 允许脚本关闭页面 dom.allow_scripts_to_close_windows -> true 2. 不要显示图片 permissions.default.image -> ...

  4. java 解决汉诺塔问题

    //汉诺塔问题//HanYang 2016/10/15 import java.util.Scanner; //输出public class Hanuota { public static void ...

  5. POJ 2446 最小点覆盖

    Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14787   Accepted: 4607 Descr ...

  6. [转]Android,Yocto,Meego构建系统的区别

    http://m.blog.csdn.net/blog/sonach_tjsd/6647829

  7. 单例模式(Singleton Pattern)

    动机: 在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例,才能确保它们的逻辑正确性.以及良好的效率. 如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例? 这应该是类设 ...

  8. bcopy函数

    函数原型:void bcopy(const  void  *src,  void  *dest,  int  n) 头文件:#include <string.h> 函数功能:将src指针指 ...

  9. sqoop连接oracle与mysql&mariadb的错误

    错误说明: 由于我的hadoop的集群是用cloudera manager在线自动安装的,因此他们的安装路径必须遵循cloudera的规则,这里只有查看cloudera的官方文档了,请参考:http: ...

  10. Android启动模式(三种)

    1,标准启动模式 通过任务栈,每点一次button,将每一个实例都压入,然后点返回键时候,就弹出之前压入的实例. 每一次的地址都是不同的 测试代码:通过创建一个button和textView来显示本身 ...