python之循环
来源:互联网 发布:53端口 转发 编辑:程序博客网 时间:2024/05/22 15:53
一、for循环
注:python shell ,使用ctrl+n创建一个文件,编辑完成保存,按F5执行。
1、命名循环变量for year in range(1980,2020):
print 'in the {} ...'.format(year)
============= RESTART: C:/Users/vinsuan/Desktop/python笔记/1.py =============
in the 1980 ...
in the 1981 ...
in the 1982 ...
in the 1983 ...
in the 1984 ...
in the 1985 ...
in the 1986 ...
in the 1987 ...
in the 1988 ...
in the 1989 ...
in the 1990 ...
in the 1991 ...
in the 1992 ...
in the 1993 ...
in the 1994 ...
in the 1995 ...
in the 1996 ...
in the 1997 ...
in the 1998 ...
in the 1999 ...
in the 2000 ...
in the 2001 ...
in the 2002 ...
in the 2003 ...
in the 2004 ...
in the 2005 ...
in the 2006 ...
in the 2007 ...
in the 2008 ...
in the 2009 ...
in the 2010 ...
in the 2011 ...
in the 2012 ...
in the 2013 ...
in the 2014 ...
in the 2015 ...
in the 2016 ...
in the 2017 ...
in the 2018 ...
in the 2019 ...
二、遍历列表
cats = ['manx','tabby','calico']
for cat in cats:
print "that's a nice {} you...".format(cat)
============= RESTART: C:/Users/vinsuan/Desktop/python笔记/1.py =============
that's a nice manx you...
that's a nice tabby you...
that's a nice calico you...
三、跳到下一个列表项
numbers = [5,2,0,20,30]
for num in numbers:
if num == 0:
print "you give me a 0"
continue
new_num = 100.0/num
print "100/{} = {} ".format(num,new_num)
============= RESTART: C:/Users/vinsuan/Desktop/python笔记/2.py =============
100/5 = 20.0
100/2 = 50.0
you give me a 0
100/20 = 5.0
100/30 = 3.33333333333
四、跳出循环
4.1 不带else
cart = [50.25,20.0,120,88,99]
for item in cart:
print item
if item > 100:
print "item >100"
break
============= RESTART: C:/Users/vinsuan/Desktop/python笔记/3.py =============
50.25
20.0
120
item >100
4.2带else
4.2.1情况一
cart = [50.25,20.0,120,88,99]
for item in cart:
print item
if item > 100:
print "item >100"
break
else:
print "hello"
============= RESTART: C:/Users/vinsuan/Desktop/python笔记/3.py =============
50.25
20.0
120
item >100
4.2.2情况二
cart = [50.25,20.0,12,88,99]
for item in cart:
print item
if item > 100:
print "item >100"
break
else:
print "hello"
============= RESTART: C:/Users/vinsuan/Desktop/python笔记/3.py =============
50.25
20.0
12
88
99
hello
>>> item
99
注:1、可以在while和for循环中使用else语句,在循环中使用时,else子句只在循环完成后执行,也就是说break语句也会跳过else块。
2、for循环创建的变量在for循环完成后并不会消失。因此,这个变量会包含列表中的最后一个值
五、while循环
age = raw_input("please input age (eg.30):")
while not age.isdigit():
print "I'am sorry , but {} isn't valid.".format(age)
age = raw_input("please input age (eg.30):")
print "thanks! Your age is set to {}".format(age)
============= RESTART: C:/Users/vinsuan/Desktop/python笔记/4.py =============
please input age (eg.30):34 years
I'am sorry , but 34 years isn't valid.
please input age (eg.30):44
thanks! Your age is set to 44
0 0
- Python 之循环日志
- Python入门 之 循环
- 三.Python之循环
- Python之While循环
- python之循环语句
- python之循环
- Python学习之循环
- Python之for循环
- Python学习之循环语句
- Python系列之循环定时器
- Python之分支与循环
- 2016.3.17.Python之循环
- python之条件、循环语句
- python基础之循环语句
- Python入门之循环分支
- Python学习之While循环
- python之for循环详解
- Python学习之路---Python 循环语句
- Google关于BigTable技术论文中文版
- 简述java中两种实现多态的方法
- JAVA 基本类型的默认值和取值范围
- hihoCoder 1041 : 国庆出游 (dfs+bitset 很不错的题!)
- centos安装单机版storm
- python之循环
- 学习设计模式-解释器模式
- 拍一万次脑门不如看一眼2861决策云
- 学习设计模式-访问者模式
- 如何获得方法的default修饰符?JVM规范并没有java8的defalut修饰符
- Android音量调节
- express里的中间件
- 2470: Puya和她学弟们
- TCP/IP协议基础概念