Python for loop and while loop
#!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的更多相关文章
- 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 ...
- C 语言:返回两个数组中第一个相同元素的指针(我用了loop 、goto loop标签)
// // main.c // Pointer_search // // Created by ma c on 15/8/2. // 要求:通过指针查找,实现比较两个有序数组中的元素,输出两个 ...
- 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 ...
- 简单了解一下事件循环(Event Loop)
关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android.Python.Java和Go,这个也是我们团队的主要技术栈. Github:https:/ ...
- 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 ...
- PostgreSQL-PL/pgSQL-cursor,loop
将spam_keyword表word字段的字符全部拆分,只是利用过程语言完成循环的操作而已. create or replace function proc1() returns setof text ...
- archlinux 加载loop模块,且设定loop设备个数
如果loop模块没有编译进内核就要先加载loop模块 modprobe loop 然后更改/etc/modprobe.d/modprobe.conf(有些文章写是在/etc/modprobe.conf ...
- Run Loop详解
Run loops是线程的基础架构部分.一个run loop就是一个事件处理循环,用来不停的调配工作以及处理输入事件.使用run loop的目的是使你的线程在有工作的时候工作,没有的时候休眠. Run ...
- Objective-C之run loop详解[转]
做了一年多的IOS开发,对IOS和Objective-C深层次的了解还十分有限,大多还停留在会用API的级别,这是件挺可悲的事情.想学好一门语言还是需要深层次的了解它,这样才能在使用的时候得心应手,出 ...
随机推荐
- Java命令提示符编译
Java利用命令提示符编译 1:最简单的方式:直接编译 /** 文件路径:G:\测试项目\java\src 文件名称:JacaText.java 编写时间:2016/6/2 作 者:郑晨辉 编写说明: ...
- python内置函数 1
常用函数 abs(x) abs()返回一个数字的绝对值.如果给出复数,返回值就是该复数的模. >>>print abs(-100) 100 >>>print abs ...
- Find a point on a 'line' between two Vector3
Find a point on a 'line' between two Vector3http://forum.unity3d.com/threads/find-a-point-on-a-line- ...
- Hibernate实现向数据库插入一条数据全过程(Study By Example)
1.数据库(直接在cmd下进入数据库操作亦可) (1)启动Navicat for MySQL (2)打开连接,创建一个数据库,名为testdb (3)新建表user1,表结构如图所示 2.数据库池 ( ...
- GitHub 上有哪些完整的 iOS-App 源码值得参考
作者:wjh2005链接:https://www.zhihu.com/question/28518265/answer/88750562来源:知乎著作权归作者所有,转载请联系作者获得授权. 1. Co ...
- OC面向对象—继承
OC面向对象—继承 一.基本概念 程序的世界和人类的“对象”世界在思想上是没有设么区别的,富二代继承了父母,自然就拥有了父母拥有的所有资源,子类继承了父类同样就拥有了父类所有的方法和属性(成员变量). ...
- IT公司100题-26-左旋转字符串
问题描述: 给定字符串和左旋的字符数,写程序实现字符串的左旋操作.例如对于字符串”12345678″, 左旋转4个字符后,变成”56781234″.要求时间复杂度为O(n),空间复杂度O(1). ...
- JavaScript中的逗号运算符
JavaScript逗号运算符 阅读本文的前提,明确表达式.短语.运算符.运算数这几个概念. 所谓表达式,就是一个JavaScript的“短语”,JavaScript解释器可以计算它,从而生成一个值 ...
- Hive Over HBase
1. 在hbase上建测试表 hbase(main)::> create 'test_hive_over_hbase','f' row(s) in 2.5810 seconds hbase(ma ...
- [DS Basics] Data structures
1, LinkedList composed of one and one Node: [data][next]. [head] -> [data][next] -> [data][nex ...