Python第二天

if-elif-else判断

代码示例:

score = input('请输入您的成绩')
score = int(score)
if score >= 90 and score <= 100:
    print("你的考试等级为A")
elif score >= 80 and score < 90:
    print("你的考试等级为B")
elif score >= 70 and score < 80:
    print("你的考试等级为C")
elif score >= 60 and score < 70:
    print("你的考试等级为D")
else:
    print("你的成绩不及格!")

for循环

--用for循环遍历字符串。

代码示例:

name = "neusoft"
for i in name:
    print(i)
    if i =="s":
        print("哈哈")

其中i是临时变量,不用提前声明,Python自动创建。
--写一个有循环次数的for循环

代码示例:

for x in range(1,100,2):
    print("对不起我错了,这是我第", x+1 ,"次向您道歉")

range()可以写循环次数
range(起始位置,终止位置,步长)
起始位置省略默认为0,步长省略为1,范围值左闭右开


生成一个 [0,1,2,3,···20]的列表

可以使用循环列表来创建
创建一个空列表

list1 = []

使用循环不停的append

for i in range(21):
    list1.append(i)
    print(list1) # 每循环一次输出一次
print(list1) # 循环结束输出

遍历heroList

heroList = ['鲁班七号','安其拉','李白',"后羿",1]
for hero in heroList:
    print(hero)

等同于

for i in range(len(heroList)):
    print(heroList[i])

其中len()是可以检测对象的元素个数。

使用for循环遍历heroList列表,判断输出的如果是后羿则输出"恭喜你选中了隐藏英雄",如果是其他的则输出"不是隐藏英雄"

for i in range(len(heroList)):
    if heroList[i] == "后羿":
        print("恭喜你选中了隐藏英雄")
    else:
        print("不是隐藏英雄")

Python制作进度条

--安装tqdm库:pip install 库的名称

# 导入tqdm
from tqdm import tqdm

import time
mylist = []
for i in range(20):
    mylist.append(i)
# 遍历mylist
for x in tqdm(mylist):
    time.sleep(1)

数据类型:字符串

name = 'k"o"be'   # 创建字符串
print(name)  # 输出字符串
# 访问
print(name[2]) # 输出o
#修改  (不可修改,可以重新赋值)
name[1]="x"
name="kobe"
print(name)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.elif判断 代码示例 结果 2.Python中的for循环 格式: for临时变量 in 可迭代对象:循环体...
    lishuyu阅读 239评论 0 0
  • python中的循环 先介绍for循环格式for 临时变量 in 可迭代对象:循环体 这个X是什么 x是临时...
    abe9753fb114阅读 358评论 0 0
  • Python中的循环 for循环 格式: for 临时变量 in 可迭代对象:循环体举例代码块 问题: 其中X是什...
    小吴同学叫乌托阅读 348评论 0 0
  • Python用处: 运维,测试 Python全栈(前端、后端) Java+Python 人工智能 爬虫工程师 数据...
    矗鑫宛馨阅读 362评论 0 0
  • python中的循环 先介绍for循环格式for 临时变量 in 可迭代对象:循环体 这个X是什么 x是临时...
    瞎巴拉拉小魔仙阅读 306评论 0 0