#!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. cube定时器延时不准确原因

    昨天晚上测试32cube配置好定时器,以1ms为一次中断,然后在程序中做了一个1s的延时,结果发现实际延时5s左右,百思不得其解,仔细查看cube配置也没问题.最后我打开生成工程文件夹里面的ioc文件 ...

  2. 1 Spring MVC 原理

    1. 注解式  Spring MVC 响应流程: 重要的接口和类的简单说明: DispatcherServlet:前端控制器,用于接收请求. HandlerMapping接口:用于处理请求的映射. D ...

  3. 识别有效的IP地址和掩码并进行分类统

    #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int c ...

  4. java 多线程—— 线程等待与唤醒

    java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...

  5. 无法查找或打开pdb文件

    工具->选项->调试{常规->启动源服务支持,符号->Microsoft符号服务器} 如果再不行.要重新生成一下,(不是重新生成解决方案)

  6. sass学习笔记2

    今天介绍sass在重用代码时最具威力的两个功能.一个是嵌套(Nesting),一个混合(Mixin). 我们在写CSS通过需要多个后代选择器组合到一起才能定位到目标元素上,而这定义过程,此元素的父元素 ...

  7. js模拟类

    ECMAScript6已经支持了class,但之前版本都不支持类,但是可以通过一些方法来模拟类. js中的类,既是重点,也是难点,很多时候都感觉模棱两可. 首先强调一下js中很重要的3个知识点:thi ...

  8. 聚类clustering

    聚类:把相似的东西分到一组,是无监督学习. 聚类算法的分类: (1)基于划分聚类算法(partition clustering):建立数据的不同分割,然后用相同标准评价聚类结果.(比如最小化平方误差和 ...

  9. [转载]javaEE规范和SSH三大框架到底有什么关系

    转载自: http://blog.csdn.net/bingjing12345/article/details/20641891 1994-2000 年是互联网的大航海时代. 请注意,下面的时间点及其 ...

  10. Windows 7中无法访问FTP的解决方法

    解决: netsh advfirewall set global StatefulFTP disable