码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习笔记——条件和循环

时间:2016-08-08 12:21:30      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

1.条件表达式

>>> x = 3
>>> x = 1 if x<3 else 2
>>> x
2

2.for语句用于序列类型

  <1>通过序列项迭代

>>> List = [‘a‘,‘b‘,‘c‘,‘d‘]
>>> for eachList in List:
...     print eachList
... 
a
b
c
d

  <2>通过序列索引迭代

>>> for eachList in range(len(List)):
...     print List[eachList]
... 
a
b
c
d

  <3>使用项和索引迭代

>>> for i,eachList in enumerate(List):
...     print "%d %s" % (i,eachList)
... 
0 a
1 b
2 c
3 d

 

Python学习笔记——条件和循环

标签:

原文地址:http://www.cnblogs.com/tonglin0325/p/5748603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!