python 基础

if 判断

[root@es-3 ~]# cat test.py 
#-*-encoding:utf-8 -*-
'''
if 1 < 2 :
	print("hajjj")
if False :
	print(3456)
	print(787)
if 1 < 3 :
	print(123456)
else:
	print(56789)
num = input('请输入您猜的数字')

if  num == '1':
	print("看《活着》")
elif num == '2':
	print("看《摆渡人》")
elif num == '3':
	print("看《岛上书店》")
else:
	print("学python")
score = int(input("输入分数: "))
if score > 100:
	print("厉害,牛")
elif score >= 90:
	print("A")
elif score >= 70:
	print("B")
elif score >= 60:
	print("C")
else:
	print("笨了")
'''
name = input('请输入名字:')
age = input('请输入年龄')
if name == '小二':
	if age == '18':
		print('666')
	else:print(333)
测试
[root@es-3 ~]# python3 test.py 
请输入名字:小san
请输入年龄18
[root@es-3 ~]# python3 test.py 
请输入名字:小二
请输入年龄18
666
[root@es-3 ~]# cat test.py 
#-*-encoding:utf-8 -*-
'''
if 1 < 2 :
	print("hajjj")
if False :
	print(3456)
	print(787)
if 1 < 3 :
	print(123456)
else:
	print(56789)
num = input('请输入您猜的数字')

if  num == '1':
	print("看《活着》")
elif num == '2':
	print("看《摆渡人》")
elif num == '3':
	print("看《岛上书店》")
else:
	print("学python")
score = int(input("输入分数: "))
if score > 100:
	print("厉害,牛")
elif score >= 90:
	print("A")
elif score >= 70:
	print("B")
elif score >= 60:
	print("C")
else:
	print("笨了")
'''
name = input('请输入名字:')   
age = input('请输入年龄')
if name == '小二':   name等于小二是就进入下一个判断;否则直接退出
	if age == '18':  判断age是否等于18,如果等于18就打印666
		print('666')
	else:print(333)  否则打印333
测试
[root@es-3 ~]# python3 test.py 
请输入名字:小san
请输入年龄18
[root@es-3 ~]# python3 test.py 
请输入名字:小二
请输入年龄18
666
[root@es-3 ~]# python3 test.py 
请输入名字:小二  
请输入年龄12
333

循环

无限死循环

[root@es-3 ~]# vim test.py 
print('9090')
while True:
    print('chenxi')
    print('流浪')
    print('痒')
print('1010')
[root@es-3 ~]# python3 test.py
9090
chenxi
流浪
痒
chenxi
流浪
痒
chenxi
流浪
痒
chenxi
流浪
^Z
[1]+  已停止               python3 test.py

 循环语句终止,打印1到10

[root@es-3 ~]# cat test.py 
count = 1
flag = True
while flag:
	print(count)
	count = count + 1
	if count > 10 :
		flag = False
[root@es-3 ~]# python3 test.py 
1
2
3
4
5
6
7
8
9
10

  循环语句终止,另一种写法

[root@es-3 ~]# vim test.py 
count = 1
while count <= 10:
        print(count)
        count = count + 1
[root@es-3 ~]# python3 test.py 
1
2
3
4
5
6
7
8
9
10

  计算从1加到100

[root@es-3 ~]# vim test.py 
count = 1
sum = 0
while count <= 100:
        sum = sum + count
        count = count + 1
print(sum)
[root@es-3 ~]# python3 test.py 
5050

    循环语句终止之二break;跳本次循环

[root@es-3 ~]# cat test.py 
print('11')
while True:
	print('222')
	print('333')
	break
	print('444')
print('abc')
[root@es-3 ~]# python3 test.py   跳出循环没有444
11
222
333
abc
  •  continue和break有点类似,区别在于continue只是终止本次循环,接着还执行后面的循环,break则完全终止循环。
[root@es-3 ~]# cat test.py 
count = 0
while count <= 100:
	count += 1
	if count > 5 and count < 95:
		continue  本次循环结束
	print("loop",count)
print("----out----")
[root@es-3 ~]# python3 test.py 
loop 1
loop 2
loop 3
loop 4
loop 5
loop 95
loop 96
loop 97
loop 98
loop 99
loop 100
loop 101
----out----

  

  

  

  

草都可以从石头缝隙中长出来更可况你呢
原文地址:https://www.cnblogs.com/rdchenxi/p/10807153.html