网络编程
位置:首页>> 网络编程>> Python编程>> 用代码帮你了解Python基础(2)

用代码帮你了解Python基础(2)

作者:FUXI_Willard  发布时间:2022-01-04 23:42:40 

标签:Python,代码,基础

1.列表:list

# 1.list:Python内置的一种数据类型,列表;
# 2.list是一种有序的集合,可以随时添加和删除其中的元素;
# Eg:列出班里所有同学的名字
studentNames = ['Willard','ChenJD','ChenXiaoBao']
print("班里所有同学的名字:",studentNames)
print("--------------------------------------------------------------------------")
# 3.获取list元素的个数,len()函数
studentNamesLen = len(studentNames)
print("studentNames的元素个数为:",studentNamesLen)
print("--------------------------------------------------------------------------")
# 4.使用索引访问list中每一个位置的元素,Python中索引从0开始
print("studentNames第一个元素为:",studentNames[0])
print("studentNames最后一个元素为:",studentNames[-1])
print("studentNames最后一个元素为:",studentNames[len(studentNames)-1])
print("--------------------------------------------------------------------------")
# Tips:
# 索引值不能超出范围
# 5.list是可变的有序列表,可以在list中追加元素和删除元素、替换元素
# a.在列表末尾添加元素
print("插入元素前的列表:",studentNames)
studentNames.append("ChenBao")
print("在studentNames末尾添加一个元素后的列表:",studentNames)
print("-------------------------------------------------------------------------")
# b.在指定的位置插入元素
print("插入元素前的列表:",studentNames)
studentNames.insert(3,"LinWenYu")
print("在索引号为3的位置插入元素:",studentNames)
print("-------------------------------------------------------------------------")
# c.删除list末尾的元素
print("删除元素前的列表:",studentNames)
studentNames.pop()
print("删除元素后的列表:",studentNames)
print("-------------------------------------------------------------------------")
# d.删除指定位置的元素
print("删除指定元素前的列表:",studentNames)
studentNames.pop(3)
print("删除指定元素后的列表:",studentNames)
print("-------------------------------------------------------------------------")
# e.把某元素替换成别的元素
print("替换元素前的列表:",studentNames)
studentNames[2] = "ChenBao"
print("替换元素后的列表:",studentNames)
print("-------------------------------------------------------------------------")
print("-------------------------------------------------------------------------")
# f.列表中的元素数据类型可以不同
informationList1 = ["willard",18,170]
informationList2 = [["willard",18,170],["ChenJD",18,168]]
print("informationList1的内容:",informationList1)
print("informationList2的内容:",informationList2)
print("-------------------------------------------------------------------------")
# g.空列表
emptyList = []
print("这是一个空列表:",emptyList)
print("-------------------------------------------------------------------------")
# h.列表类型
scoreList = [100,99,98]
print("列表类型为:",type(scoreList))

# 结果输出:
班里所有同学的名字: ['Willard', 'ChenJD', 'ChenXiaoBao']
--------------------------------------------------------------------------
studentNames的元素个数为: 3
--------------------------------------------------------------------------
studentNames第一个元素为: Willard
studentNames最后一个元素为: ChenXiaoBao
studentNames最后一个元素为: ChenXiaoBao
--------------------------------------------------------------------------
插入元素前的列表: ['Willard', 'ChenJD', 'ChenXiaoBao']
在studentNames末尾添加一个元素后的列表: ['Willard', 'ChenJD', 'ChenXiaoBao', 'ChenBao']
-------------------------------------------------------------------------
插入元素前的列表: ['Willard', 'ChenJD', 'ChenXiaoBao', 'ChenBao']
在索引号为3的位置插入元素: ['Willard', 'ChenJD', 'ChenXiaoBao', 'LinWenYu', 'ChenBao']
-------------------------------------------------------------------------
删除元素前的列表: ['Willard', 'ChenJD', 'ChenXiaoBao', 'LinWenYu', 'ChenBao']
删除元素后的列表: ['Willard', 'ChenJD', 'ChenXiaoBao', 'LinWenYu']
-------------------------------------------------------------------------
删除指定元素前的列表: ['Willard', 'ChenJD', 'ChenXiaoBao', 'LinWenYu']
删除指定元素后的列表: ['Willard', 'ChenJD', 'ChenXiaoBao']
-------------------------------------------------------------------------
替换元素前的列表: ['Willard', 'ChenJD', 'ChenXiaoBao']
替换元素后的列表: ['Willard', 'ChenJD', 'ChenBao']
-------------------------------------------------------------------------
-------------------------------------------------------------------------
informationList1的内容: ['willard', 18, 170]
informationList2的内容: [['willard', 18, 170], ['ChenJD', 18, 168]]
-------------------------------------------------------------------------
这是一个空列表: []
-------------------------------------------------------------------------
列表类型为: <class 'list'>
 

2.元组:tuple

# 1.tuple:元组,元组一旦初始化,将不能修改
studentNames = ("Willard","ChenJD","ChenBao")
print("studentNames元组:",studentNames)
print("-------------------------------------------------------------------------")
# 2.tuple没有append(),insert()方法;
# 3.tuple可以通过索引来获取元素;
# 4.定义空的tuple:
emptyTuple = ()
print("这是一个空元组:",emptyTuple)
print("-------------------------------------------------------------------------")
# 5.定义只有一个元素的tuple
oneElementTuple = (1,)    # 不能定义成:oneElementTuple = (1)
print("这是一个只有一个元素的元组:",oneElementTuple)
print("-------------------------------------------------------------------------")
# 6.元组类型
studentNames = ("Willard","ChenJD","ChenBao")
print("元组类型为:",type(studentNames))

# 结果输出:
studentNames元组: ('Willard', 'ChenJD', 'ChenBao')
-------------------------------------------------------------------------
这是一个空元组: ()
-------------------------------------------------------------------------
这是一个只有一个元素的元组: (1,)
-------------------------------------------------------------------------
元组类型为: <class 'tuple'>
 

3.条件判断

# if语法:如果if语句判断是True,则执行if后的语句块,否则,什么也不做;
# 1.实例:分数划分等级
score = int(input("请输入您的分数:"))
if score > 100 or score < 0:
   print("您的输入有误,请重新输入!")
if score >= 90 and score <= 100:
   print("成绩等级为A")
if score >= 80 and score < 90:
   print("成绩等级为B")
if score >= 70 and score < 80:
   print("成绩等级为C")
if score >= 60 and score < 70:
   print("成绩等级为D")
if score >= 0 and score < 60:
   print("成绩等级为E")

 # 结果输出:
请输入您的分数:100
成绩等级为A
------------------
请输入您的分数:-1
您的输入有误,请重新输入!

# if-else语法:如果if语句判断是True,则执行if后的语句块;
# 否则,执行else语句后的语句块;
# 2.实例:判断输入的账号密码是否正确
userName = input("请输入您的账号名称:")
password = input("请输入您的密码:")
if ((userName == "Willard") and (password == "JD584520")):
   print("账号密码输入正确,登录成功!")
else:
   print("账号或密码输入错误,登录失败!")

 # 结果输出:
请输入您的账号名称:Willard
请输入您的密码:JD584520
账号密码输入正确,登录成功!
---------------------------
请输入您的账号名称:Willard
请输入您的密码:jd584520
账号或密码输入错误,登录失败!

# if-elif-else语法:
if <条件判断1>:
   <执行1>
elif <条件判断2>:
   <执行2>
elif <条件判断3>:
   <执行3>
else:
   <执行4>
# 实例3:使用if-elif-else判断成绩等级
score = int(input("请输入您的分数:"))
if score > 100 or score < 0:
   print("您的输入有误,请重新输入!")
elif 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("成绩等级为E")

# 结果输出:
请输入您的分数:60
成绩等级为D
--------------------
请输入您的分数:-1
您的输入有误,请重新输入!

# 小实例:
# 综合实例
print("欢迎来到华中科技大学成绩查询网!")
print("请输入您的账号密码进行登录!")
print("---------------------------------")
userName = input("请输入您的账号:")
password = input("请输入您的密码:")
if ((userName == "Willard") and (password == "JD584520")):
   print("账号密码正确,登录成功!")
   print("请您输入您的分数,查询对应的分数等级!")
   score = int(input("请输入您的分数(0-100):"))
   if score > 100 or score < 0:
       print("您的输入有误,请重新输入!")
   elif 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("成绩等级为E")
else:
   print("账号或密码输入有误,登录失败!")
   print("请重新输入您的账号密码!")

# 结果输出:
# 输入1:
欢迎来到华中科技大学成绩查询网!
请输入您的账号密码进行登录!
---------------------------------
请输入您的账号:Willard
请输入您的密码:JD584520
账号密码正确,登录成功!
请您输入您的分数,查询对应的分数等级!
请输入您的分数(0-100):100
成绩等级为A

# 输入2:
欢迎来到华中科技大学成绩查询网!
请输入您的账号密码进行登录!
---------------------------------
请输入您的账号:Willard
请输入您的密码:jd584520
账号或密码输入有误,登录失败!
请重新输入您的账号密码!

# 输入3:
欢迎来到华中科技大学成绩查询网!
请输入您的账号密码进行登录!
---------------------------------
请输入您的账号:Willard
请输入您的密码:JD584520
账号密码正确,登录成功!
请您输入您的分数,查询对应的分数等级!
请输入您的分数(0-100):101
您的输入有误,请重新输入!
 

注:以上代码均经过验证,但并不是生产环境部署的代码,只是一些小Demo,以用来说明Python的相关知识,大神请跳过!

来源:https://fuxi-willard.blog.csdn.net/article/details/122591610

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com