本站以分享各种运维经验和运维所需要的技能为主
《python》:python零基础入门学习
《shell》:shell学习
《terraform》持续更新中:terraform_Aws学习零基础入门到最佳实战
《k8》暂未更新
《docker学习》暂未更新
《ceph学习》ceph日常问题解决分享
《日志收集》ELK+各种中间件
《运维日常》持续更新中
判断语句以及for循环
判断语句:
if 条件:条件为真时执行的语句
else条件为假时执行的语句if 条件:cmd1
elif 条件:cmd2
else:cmd3练习:
if 3>0:print('ok')print('yes')if 10 in [10,20,30]:print('OK')if -0.0:print('yes')#任何值为0的数字都是Falseif 1:print('yes')if [1,2]:print('yes')#非空列表为Trueif {1,2}:print('yes')#非空对象都是Trueif (1,2):print('空元组为False')if ' ':print('空格也是一个字符,非空为True')if not None:print('None为False,取反为True')测试结果:
ok
yes
OK
yes
yes
yes
空元组为False
空格也是一个字符,非空为True
None为False,取反为True# a = 10
# b = 20
# if a < b :
# smaller = a
# else :
# smaller = b
# print(smaller)
#简化版:
a=10
b=20
smaller = a if a < b else b
print(smaller)测试结果
10
判断学习实例:
登录账号:(密码输入的时候不显示)
import getpass #导入名为getpass的模块uname = input('username: ')
upass = getpass.getpass('password: ')if uname == 'bob' and upass == '123456' :print('登录成功')
else :print('登录失败')
------要在终端下用 python 进行运行py文件 才会显示效果
判断成绩:
grade = int(input("请输入你的成绩: "))
if grade > 90 :print("优秀")
elif grade > 80 :print("好")
elif grade > 70 :print("良")
elif grade > 60 :print("及格")
else :print("不及格,你需要更努力了")try:-----输入不是整数的时候或者空的时候会报错g = int(input('g:'))if g > 90 :print('1111')elif g > 80 :print('111')elif g > 70:print('11')elif g > 60 :print('1')else:print('0')
except:print("输入整数")
------
score = int(input('分数: '))if score >= 60 and score < 70:print('及格')
elif 70 <= score < 80:print('良')
elif 80 <= score < 90:print('好')
elif score >= 90:print('优秀')
else:print('你要努力了')
猜拳:
>>> import random
>>> random.choices(['aa','bb'])
['bb']
>>> random.choice(['aa','bb'])
'bb'
>>> random.randint(1,100)
14import random
choices = ['拳头','剪刀','布']
#print(choices)
computer = random.choice(choices)
#print(computer)
while True:player = input('请出拳: ')print("mychoice:%s computer's choice:%s" % (player , computer))if player == computer:print('相同结果,请重新出拳: ')continueelif player == '拳头' and computer == '布' :print('你输了')breakelif player == '拳头' and computer == '剪刀' :print('你赢了')breakelif player == '布' and computer == '拳头' :print('你赢了')breakelif player == '布' and computer == '剪刀' :print('你输了')breakelif player == '剪刀' and computer == '布' :print('你赢了')breakelif player == '剪刀' and computer == '拳头' :print('你输了')breakimport random
choices = ['拳头','剪刀','布']
#print(choices)
computer = random.choice(choices)
#print(computer)
while True:player = input('请出拳: ')print("mychoice:%s computer's choice:%s" % (player , computer))if player == computer:print('相同结果,请重新出拳: ')continueelif player == '拳头' and computer == '剪刀' :print('你赢了')breakelif player == '布' and computer == '拳头' :print('你赢了')breakelif player == '剪刀' and computer == '布' :print('你赢了')breakelse:print('你输了')breakimport random
choices = ['拳头','剪刀','布']
#print(choices)
win_list = [['拳头','剪刀'],['剪刀','布'],['布','拳头']]
inn = """(0)拳头
(1)剪刀
(2)布
请选择(0/1/2): """
while True:computer = random.choice(choices)# print(computer)ind = int(input(inn))player = choices[ind]print("mychoice:%s computer's choice:%s" % (player , computer))if player == computer:print('\033[032;1m相同结果,请重新出拳:\033[0m')continueelif [player,computer] in win_list:print('\033[31;1m你赢了\033[0m')breakelse:print('\033[31;1m你输了\033[0m')break
循环:
break:
-
用于结束循环
#累加1+2+3+...+100
result = 0
counter = 1while counter < 101:result += countercounter += 1print(result)#三盘两胜局:
# import random
# choices = ['拳头','剪刀','布']
# #print(choices)
# win_list = [['拳头','剪刀'],['剪刀','布'],['布','拳头']]
# inn = """(0)拳头
# (1)剪刀
# (2)布
# 请选择(0/1/2): """
# count = 0
# count2 = 0
#
# while count < 2 and count2 < 2:
# computer = random.choice(choices)
# # print(computer)
# ind = int(input(inn))
# player = choices[ind]
# print("mychoice:%s computer's choice:%s" % (player , computer))
# if player == computer:
# print('\033[032;1m相同结果,请重新出拳:\033[0m')
# continue
# elif [player,computer] in win_list:
# #print('\033[31;1m你赢了\033[0m')
# count += 1
# print('赢了%s次' % count)
# else:
# #print('\033[31;1m你输了\033[0m')
# count2 += 1
# print('赢了%s次' % count2)
#
# if count == 2 :
# print("你赢了")
# else:
# print("你输了")# while count < 2 and count2 < 2:
# computer = random.choice(choices)
# # print(computer)
# ind = int(input(inn))
# player = choices[ind]
# print("mychoice:%s computer's choice:%s" % (player , computer))
# if player == computer:
# print('\033[032;1m相同结果,请重新出拳:\033[0m')
# continue
# elif [player,computer] in win_list:
# #print('\033[31;1m你赢了\033[0m')
# count += 1
# print('赢了%s次' % count)
# else:
# #print('\033[31;1m你输了\033[0m')
# count2 += 1
# print('赢了%s次' % count2)
#
# if count == 2 :
# print("你赢了")
# else:
# print("你输了")#方法二:
# while 1:
# computer = random.choice(choices)
# # print(computer)
# ind = int(input(inn))
# player = choices[ind]
# print("mychoice:%s computer's choice:%s" % (player , computer))
# if player == computer:
# print('\033[032;1m相同结果,请重新出拳:\033[0m')
# continue
# elif [player,computer] in win_list:
# #print('\033[31;1m你赢了\033[0m')
# count += 1
# print('赢了%s次' % count)
# else:
# #print('\033[31;1m你输了\033[0m')
# count2 += 1
# print('赢了%s次' % count2)
# if count == 2 or count2 == 2 :
# break
continue:
# #1-100的偶数累加
# result = 0
# counter = 0
#
# while counter < 100:
# counter += 1
# if counter % 2 == 1:
# if counter % 2 : 1为真 0为假
# continue
# else:(可有可无)
# result += counter
#
# print(result)
else:
import random
num2 = random.choice(range(1,101))
count = 0
while count<7:count += 1num = int(input("请输入一个数字: "))if num > num2:print('大了')elif num < num2:print('小了')elif num == num2:print("中")breakelse:print('不要输入除了1-100之外的数字或者符号哦')
else:print("正确答案是:%s 你猜了%s次,你个辣鸡,别猜了" % (num2,count))
for:
astr = 'hello'
alist = [10, 20, 30]
atuple = ('yyf', 'chao', 'yang')
adict = {'name':'yyf' , 'age':23}for ch in astr:print(ch)for i in alist:print(i)for name in atuple:print(name)for key in adict:print('%s:%s' % (key,adict[key]))测试结果:
/root/nsd1907/bin/python /root/nsd1907/py02/day02/01.py
h
e
l
l
o
10
20
30
yyf
chao
yang
name:yyf
age:23在终端用for循环的时候,也需要缩进
>>> a = 'wode'
>>> for i in a:
... print(i)
...
w
o
d
erange用法:
>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(6,10)
range(6, 10)
>>> list(range(6,10))
[6, 7, 8, 9]
>>> list(range(6,10,2))
[6, 8]
>>> list(range(1,10,2))
[1, 3, 5, 7, 9]
>>> list(range(10,1,-1))
[10, 9, 8, 7, 6, 5, 4, 3, 2]
>>> list(range(10,1))
[]
>>> list(range(10,1,-1))
[10, 9, 8, 7, 6, 5, 4, 3, 2]# sum100 =0
#
# for i in range(1,101):
# sum100 += i
#
# print(sum100)
列表实现斐波那契数列:
# num = int(input('输入一个数字: '))
#
# fib = [0, 1]
#
# for i in range(num):
# fib.append(fib[-1] + fib[-2])
#
# print(fib)
九九乘法:---for循环的嵌套使用
for i in range(1,4):#控制第几行的打印#每行之内再循环打印3个hellofor j in range(1,4): #行内重复打印3个helloprint('hello',end=' ') #print默认在结尾打印回车,改为空格print() #每行结尾打印回车for i in range(1,10):#控制第几行的打印#每行之内再循环打印3个hellofor j in range(1,i+1): #行内重复打印3个helloprint('*',end=' ') #print默认在结尾打印回车,改为空格print() #每行结尾打印回车n = int(input('number: '))for i in range(1, n + 1):for j in range(1, i + 1):print('%s*%s=%s' % (j, i, i * j), end=' ')print()
列表解析:
>>> [5]
[5]
>>> [5+5] ----将表达式的计算结果放到列表中
[10]
>>> [5+5 for i in range(10)] ---通过for循环控制表达式计算的次数
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
>>> [5+i for i in range(10)] ----再表达式中,使用for中的变量
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> [5+i for i in range(1,11)] ------
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> [5+i for i in range(1,11) if i % 2 ==1] ---通过if判断语句实现过滤,满足判断条件时,才计算表达式
[6, 8, 10, 12, 14]
>>>
>>> ['192.168.1.' + str(i) for i in range(1,255) ]
>>> ['192.168.1.%s' % i for i in range(1,255) ]
找出 192.168.1.1-254 的地址