【python零基础入门学习】python基础篇之判断与for循环(二)

 本站以分享各种运维经验和运维所需要的技能为主

《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 的地址

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/91884.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#安装“Windows 窗体应用(.NET Framework)”

目录 背景: 第一步: 第二步: 第三步&#xff1a; 总结: 背景: 如下图所示:在Visual Studio Installer创建新项目的时候&#xff0c;想要添加windows窗体应用程序&#xff0c;发现里面并没有找到Windows窗体应用(.NET Framework)模板&#xff0c;快捷搜索也没有发现&#…

css实现文字翻转效果

csss实现文字翻转效果 主要实现核心属性 direction: rtl; unicode-bidi: bidi-override; direction: rtl; 这个属性用于指定文本的方向为从右到左&#xff08;Right-to-Left&#xff09;。它常用于处理阿拉伯语、希伯来语等从右向左书写的文字样式。当设置了 direction: rtl; …

在Windows操作系统上安装Neo4j数据库

在Windows操作系统上安装Neo4j数据库 一、在Windows操作系统上安装Neo4j数据库 一、在Windows操作系统上安装Neo4j数据库 点击 MySQL可跳转至MySQL的官方下载地址。 在VUE3项目的工程目录中&#xff0c;通过以下命令可生成node_modules文件夹。 npm install&#xff08;1&am…

什么是浏览器缓存(browser caching)?如何使用HTTP头来控制缓存?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 浏览器缓存和HTTP头控制缓存⭐ HTTP头控制缓存1. Cache-Control2. Expires3. Last-Modified 和 If-Modified-Since4. ETag 和 If-None-Match ⭐ 缓存策略⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 记得点击…

简单shell脚本的编写

文章目录 简单使用shell脚本参数判断整数的比较运算符字符串的比较运算shell脚本流程控制shell脚本循环for循环批量添加用户批量ping IP地址检测同一局域网&#xff0c;多台主机存活情况检测同一局域网&#xff0c;多台主机存活情况多线程检测主机存活情况 while循环case选择语…

贝锐蒲公英异地组网方案,如何阻断网络安全威胁?

随着混合云和移动办公的普及&#xff0c;企业网络面临着越来越复杂的安全威胁环境。 大型企业有足够的能力和预算&#xff0c;构建覆盖全部个性化需求的定制化网络安全方案。 但对于广大中小企业来说&#xff0c;由于实际业务发展情况&#xff0c;他们难以在部署周期、预算成本…

STL stack,queue,deque以及适配器

目录 stackstack的使用stack模拟实现 queuequeue的使用queue模拟实现 适配器deque stack stack的使用 下面是stack库中的接口函数&#xff0c;有了前面的基础&#xff0c;我们可以根据函数名得知函数的作用 函数说明stack()构造空栈empty()判断栈是否为空size()返回栈中元素…

1992-2022年全国31省市产业升级、产业结构高级化水平面板数据(含原始数据和计算过程)

1992-2022年全国31省市产业升级、产业结构高级化水平面板数据&#xff08;含原始数据和计算过程&#xff09; 1、时间&#xff1a;1992-2022年 2、指标&#xff1a;地区生产总值、第一产业增加值、第二产业增加值、第三产业增加值、第一产业占GDP比重、第二产业占GDP比重、第…

【分享】PDF如何拆分成2个或多个文件呢?

当我们需要把一个多页的PDF文件拆分成2个或多个独立的PDF文件&#xff0c;可以怎么操作呢&#xff1f;这种情况需要使用相关工具&#xff0c;下面小编就来分享两个常用的工具。 1. PDF编辑器 PDF编辑器不仅可以用来编辑PDF文件&#xff0c;还具备多种功能&#xff0c;拆分PDF文…

哪吒汽车“三头六臂”之「浩智电驱」

撰文 / 翟悦 编审 / 吴晰 8月21日&#xff0c;在哪吒汽车科技日上&#xff0c;哪吒汽车发布“浩智战略2025”以及浩智技术品牌2.0。根据公开信息&#xff0c;主编梳理了以下几点&#xff1a;◎浩智滑板底盘支持400V/800V双平台◎浩智电驱包括180kW 400V电驱系统和250kW 800…

基于Spring Boot的住院病人管理系统设计与实现(Java+spring boot+MySQL)

获取源码或者论文请私信博主 演示视频&#xff1a; 基于Spring Boot的住院病人管理系统设计与实现&#xff08;Javaspring bootMySQL&#xff09; 使用技术&#xff1a; 前端&#xff1a;html css javascript jQuery ajax thymeleaf 微信小程序 后端&#xff1a;Java spring…

Python钢筋混凝土结构计算.pdf-混凝土强度设计值

计算原理&#xff1a; 需要注意的是&#xff0c;根据不同的规范和设计要求&#xff0c;上述公式可能会有所差异。因此&#xff0c;在进行混凝土强度设计值的计算时&#xff0c;请参考相应的规范和设计手册&#xff0c;以确保计算结果的准确性和合规性。 代码实现&#xff1a; …