Python语法--Conditional Steps, repeated steps, loop

学习笔记:本文属于自用笔记,仅供参考。

Conditional Steps(条件语句) - if … elif .. else

Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。

1
2
3
4
5
6
if (expression):
(command)
elif (expression):
(command)
else:
(command)

Conditional Steps(条件语句) (Multi elif) - if … elif .. else

1
2
3
4
5
6
7
8
9
10
11
if x < 0:
x = 0
print 'negative changed to zero'
elif x == 0:
print 'zero'
elif x == 1:
print 'single'
elif x == 2:
print 'double'
else:
print 'more'

Repeated steps(循环语句) - while

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务

1
2
while i < 6:
print "At the top i is %d" % i

Repeated steps(循环语句) - another example

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务

Assign a value to i which is greater than 2

1
2
3
while i > 2:
print "Right now i is %d" % i
i=i-1

Loop(循环语句) - for

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串

1
2
for (y) in (x):
(command)

参考链接

Pythonlearn:resources-week01
Python 基础教程

文章目录
  1. 1. Conditional Steps(条件语句) - if … elif .. else
  2. 2. Conditional Steps(条件语句) (Multi elif) - if … elif .. else
  3. 3. Repeated steps(循环语句) - while
  4. 4. Repeated steps(循环语句) - another example
  5. 5. Loop(循环语句) - for
  6. 6. 参考链接
,