您的位置:首页 > 编程语言 > Python开发

python 循环以及控制语句

2020-07-15 05:35 32 查看

if语句

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

基本判断语句

# 基本判断语句

age = 12
if age < 18:
print('18岁以下不宜观看')
  • if语句后面必须有 :
  • 自动缩进
  • if语句写完后,要退回原有缩进继续写
  • Python代码的缩进规则:具有相同缩进的代码被视为代码块

输入函数 input()

# 输入函数 input()

score = input('请输入成绩:')
print('该学生成绩为:' + score)
print(type(score))
# 注意:input()返回结果都为字符串,如果需要变为数字则用到int()/float()

=======================
请输入成绩:60
该学生成绩为:60
<class 'str'>

两种条件判断:if-else

flag = False
name = 'luren'
if name == 'python':          # 判断变量否为'python'
flag = True               # 条件成立时设置标志为真
print( 'welcome boss')    # 并输出欢迎信息
else:
print(name)               # 条件不成立时输出变量名称

=====================
luren

多种条件判断:if-elif-…-else

# 多种条件判断:if-elif-...-else

num = 2
if num == 3:            # 判断num的值
print('boss')
elif num == 2:
print('user')
elif num == 1:
print('worker')
elif num < 0:           # 值小于零时输出
print('error')
else:
print('roadman')    # 条件均不成立时输出

================
user

单语句多条件判断:or and

# 单语句多条件判断:or and

num = 5
if num >= 0 and num <= 10:
print( 'hello')
# 判断值是否在0~10之间
# 输出结果: hello

num = 10
if num < 0 or num > 10:
print( 'hello')
else:
print( 'undefine')
# 判断值是否在小于0或大于10
# 输出结果: undefine

num = 8
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):
print( 'hello')
else:
print( 'undefine')
# 判断值是否在0~5或者10~15之间
# 输出结果: undefine

===========================
hello
undefine
undefine

for循环

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

重复输出

# 想输出"hello world"5次怎么办?
for i in range(5):
print('hello world!')

===============
hello world!
hello world!
hello world!
hello world!
hello world!

遍历序列、映射

# 通过for遍历序列、映射

lst = list(range(10))
for i in lst[::2]:
print(i)
print('-----')
# 遍历list

age = {'Tom':18, 'Jack':19, 'Alex':17, 'Mary':20}
for name in age:
print(name + '年龄为:%s岁' % age[name])
# 遍历字典

================
0
2
4
6
8
-----
Mary年龄为:20岁
Tom年龄为:18岁
Alex年龄为:17岁
Jack年龄为:19岁

嵌套循环

# 嵌套循环

for i in range(3):
for j in range(2):
print(i,j)
# 循环套循环,注意:尽量不要多于3个嵌套

============
0 0
0 1
1 0
1 1
2 0
2 1

while循环

  • 执行语句可以是单个语句或语句块
  • 判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。
  • 当判断条件假false时,循环结束。

基本运行逻辑

# 基本运行逻辑

count = 0
while count < 9:
print( 'The count is:', count)
count = count + 1
print( "Good bye!")
# 这里count<9是一个判断语句,当判断为True时,则继续运行

=========================
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

关于无限循环

# 关于无限循环:如果条件判断语句永远为 true,循环将会无限的执行下去

var = 1
while var == 1 :
num = input("Enter a number  :")
print( "You entered: ", num)
print( "Good bye!")
# 该条件永远为true,循环将无限执行下去
# 一定要避免无限循环!!

while-else语句

# while-else语句

count = 0
while count < 5:
print(count, " is  less than 5")
count = count + 1
else:
print(count, " is not less than 5")
# 逻辑和if-else一样

================
0  is  less than 5
1  is  less than 5
2  is  less than 5
3  is  less than 5
4  is  less than 5
5  is not less than 5

循环控制语句

break语句

# break语句

s = 0
n = 1
while n > 0:
s = s + n
n = n + 1
if n == 20:
break
print(s)
# break语句用来终止循环语句,即便循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句。

s = 0
for i in range(10):
for j in range(5):
s = s + (i*j)
print('第%i次计算' %(i+j))
if s > 20:
break
print('结果为%i' % s)
# 如果使用嵌套循环,break语句将停止执行最深层的循环,并开始执行下一行代码。

=================
190
第0次计算
第1次计算
第2次计算
第3次计算
第4次计算
第1次计算
第2次计算
第3次计算
第4次计算
第5次计算
第2次计算
第3次计算
第4次计算
第5次计算
第6次计算
结果为30

continue语句

# continue语句

s = 0
for i in range(50):
if i%2 == 0:
s += i
else:
continue
print('第%i次计算'%(i/2))
print('结果为%i' % s)
# continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环。

==================
第0次计算
第1次计算
第2次计算
第3次计算
第4次计算
第5次计算
第6次计算
第7次计算
第8次计算
第9次计算
第10次计算
第11次计算
第12次计算
第13次计算
第14次计算
第15次计算
第16次计算
第17次计算
第18次计算
第19次计算
第20次计算
第21次计算
第22次计算
第23次计算
第24次计算
结果为600

pass语句

# pass语句

for letter in 'Python':
if letter == 'h':
pass
print( '当前字母 : h,但是我pass了')
print( '当前字母 :', letter)
print( "Good bye!")
# pass是空语句,是为了保持程序结构的完整性。(不中断也不跳过)

============================
当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h,但是我pass了
当前字母 : h
当前字母 : o
当前字母 : n
Good bye!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: