python学习(五)语句
文章目录前言一、条件语句二、循环语句1、while循环前言本篇主要介绍python中的语句,即条件语句、循环语句等。一、条件语句用法如下:#!/usr/bin/python# -*- coding: UTF-8 -*-# 例2:elif用法num = -10if num == 3:# 判断num的值print ("boss")elif num == 2:print ("user")elif num
·
文章目录
前言
本篇主要介绍python中的语句,即条件语句、循环语句和pass语句等。
一、条件语句
1、单条件
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块,用法如下:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例1:if 基本用法
flag = False
name = 'luren'
if name == 'python': # 判断变量是否为 python
flag = True # 条件成立时设置标志为真
print 'welcome boss' # 并输出欢迎信息
else:
print name # 条件不成立时输出变量名称
输出结果为:
luren
2、多条件
如果判断需要多个条件时,可以使用and 或or。此时,可以使用括号来区分判断的先后顺序,括号中的判断优先执行,此外 and 和 or 的优先级低于>(大于)、<(小于)等判断符号,即大于和小于在没有括号的情况下会比与或要优先判断。
用法如下:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例3:if语句多个条件
num = 9
if num >= 0 and num <= 10: # 判断值是否在0~10之间
print 'hello'
# 输出结果: hello
num = 10
if num < 0 or num > 10: # 判断值是否在小于0或大于10
print 'hello'
else:
print 'undefine'
# 输出结果: undefine
num = 8
# 判断值是否在0~5或者10~15之间
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):
print 'hello'
else:
print 'undefine'
# 输出结果: undefine
3、简单语句组
#!/usr/bin/python
# -*- coding: UTF-8 -*-
var = 100
if ( var == 100 ) : print "变量 var 的值为100"
print "Good bye!"
结果如下:
变量 var 的值为100
Good bye!
二、循环语句
在python中,没有do…while循环。
1、while循环
(1)、基础使用
用法如下:
#!/usr/bin/python
i = 1
while i < 10:
i += 1
if i%2 > 0: # 非双数时跳过输出
continue
print i # 输出双数2、4、6、8、10
else:
print "My name is Li"
i = 1
while 1: # 循环条件为1必定成立
print i # 输出1~10
i += 1
if i > 10: # 当i大于10时跳出循环
break
注意:break用来退出循环,而continue用来跳过本次循环。
(2)、无限循环
#!/usr/bin/python
# -*- coding: UTF-8 -*-
var = 1
while var == 1 : # 该条件永远为true,循环将无限执行下去
num = raw_input("Enter a number :")
print "You entered: ", num
print "Good bye!"
(3)、简单语句组
类似 if 语句的语法,如果你的 while 循环体中只有一条语句,你可以将该语句与while写在同一行中, 如下所示:
#!/usr/bin/python
flag = 1
while (flag): print 'Given flag is really true!'
print "Good bye!"
(4)、循环嵌套
while expression:
while expression:
statement(s)
statement(s)
2、for循环
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
(1)、基础使用
#!/usr/bin/python
# -*- coding: UTF-8 -*-
for letter in 'Python': # 第一个实例
print("当前字母: %s" % letter)
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例
print ('当前水果: %s'% fruit)
print ("Good bye!")
(2)、通过序列索引迭代
#!/usr/bin/python
# -*- coding: UTF-8 -*-
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print ('当前水果 : %s' % fruits[index])
print ("Good bye!")
(3)、循环使用else语句
#!/usr/bin/python
# -*- coding: UTF-8 -*-
for num in range(10,20): # 迭代 10 到 20 之间的数字
for i in range(2,num): # 根据因子迭代
if num%i == 0: # 确定第一个因子
j=num/i # 计算第二个因子
print ('%d 等于 %d * %d' % (num,i,j))
break # 跳出当前循环
else: # 循环的 else 部分
print ('%d 是一个质数' % num)
(4)、循环嵌套
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
三、pass语句
Python pass 是空语句,是为了保持程序结构的完整性。pass 不做任何事情,一般用做占位语句。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 输出 Python 的每个字母
for letter in 'Python':
if letter == 'h':
pass
print '这是 pass 块'
print '当前字母 :', letter
print "Good bye!"
注意:
def function():
# 空函数在Python2.x版本中pass是必须的
pass
def function():
# 在Python3.x的时候pass可以写或不写
pass
更多推荐
已为社区贡献4条内容
所有评论(0)