您的位置:首页 > 娱乐 > 明星 > Python学习笔记(五)

Python学习笔记(五)

2024/10/6 12:31:17 来源:https://blog.csdn.net/2303_76234920/article/details/141098278  浏览:    关键词:Python学习笔记(五)
"""
演示tuple元组的定义和操作
"""# 元组一旦定义完成,就不可修改
# 定义元组
# t1 = (1, "Hello", True)
# t2 = ()  # 定义空元组
# t3 = tuple()  #定义空元组
# print(f"t1的类型是:{type(t1)}, 内容是:{t1}")
# print(f"t2的类型是:{type(t2)}, 内容是:{t2}")
# print(f"t3的类型是:{type(t3)}, 内容是:{t3}")
#
# # 定义单个元素的元组
# # 如果元组里只有一个元素就必须在后面写逗号,如果不写就是字符串类型
# t4 = ("hello",)
# print(f"t4的类型是:{type(t4)}, t4的内容是:{t4}")
# # 元组的嵌套
# t5 = ( (1, 2, 3), (4, 5, 6) )
# print(f"t5的类型是:{type(t5)}, 内容是:{t5}")
#
# # 下标索引去取出内容
# num = t5[1][2]
# print(f"从嵌套元组中取出的数据是:{num}")
#
# # 元组的操作:index查找方法
# t6 = ("传智教育", "黑马程序员", "Python")
# index = t6.index("黑马程序员")
# print(f"在元组t6中查找黑马程序员,的下标是:{index}")
# # 元组的操作:count统计方法
# t7 = ("传智教育", "黑马程序员", "黑马程序员", "黑马程序员", "Python")
# num = t7.count("黑马程序员")
# print(f"在元组t7中统计黑马程序员的数量有:{num}个")
# # 元组的操作:len函数统计元组元素数量
# t8 = ("传智教育", "黑马程序员", "黑马程序员", "黑马程序员", "Python")
# num = len(t8)
# print(f"t8元组中的元素有:{num}个")
# # 元组的遍历:while
# index = 0
# while index < len(t8):
#     print(f"元组的元素有:{t8[index]}")
#     # 至关重要
#     index += 1
#
# # 元组的遍历:for
# for element in t8:
#     print(f"2元组的元素有:{element}")
#
# # 不能修改元组内容,元组定义后不能修改
# # t8[0] = "itcast"
# # print(t8[0])
#
# # 定义一个元组,元组内的第一层元素不能修改,但第二层元素如list内的元素可修改
# t9 = (1, 2, ["itheima", "itcast"])
# print(f"t9的内容是:{t9}")
# t9[2][0] = "黑马程序员"
# t9[2][1] = "传智教育"
# print(f"t9的内容是:{t9}")# 小练习
# t10=('周杰伦',11,['football','music'])
# print(t10.index(11))
# print(t10[0])
# t10[2].remove('football')
# print(t10)
# t10[2].append("coding")
# print(t10)"""
演示以数据容器的角色,学习字符串的相关操作
"""
# my_str = "itheima and itcast"
# # 通过下标索引取值
# value = my_str[2]
# value2 = my_str[-16]
# print(f"从字符串{my_str}取下标为2的元素,。值是:{value},取下标为-16的元素。值是:{value2}")
#
# # 不能修改字符串对应位置的值
# # my_str[2] = "H"
#
# # index方法
# value = my_str.index("and")
# print(f"在字符串{my_str}中查找and,其起始下标是:{value}")
#
# # replace方法,原字符串不变,得到一个替换后的新字符串
# new_my_str = my_str.replace("it", "程序")
# print(f"将字符串{my_str},进行替换后得到:{new_my_str}")
#
# # split方法
# my_str = "hello python itheima itcast"
# my_str_list = my_str.split(" ")
# print(f"将字符串{my_str}进行split切分后得到:{my_str_list}, 类型是:{type(my_str_list)}")
#
# # strip方法
# my_str = "  itheima and itcast  "
# new_my_str = my_str.strip() # 不传入参数,去除首尾空格
# print(f"字符串{my_str}被strip后,结果:{new_my_str}")
#
# my_str = "12itheima and itcast21"
# new_my_str = my_str.strip("12")
# print(f"字符串{my_str}被strip('12')后,结果:{new_my_str}")
#
# # 统计字符串中某字符串的出现次数, count
# my_str = "ithaha and itheihei"
# count = my_str.count("it")
# print(f"字符串{my_str}中it出现的次数是:{count}")
# # 统计字符串的长度, len()
# num = len(my_str)
# print(f"字符串{my_str}的长度是:{num}")# 小练习
str="ithaha itlala boxixi"
num=str.count("it")
print(f"有{num}个\"it\"字符")
newstr=str.replace(" ","|")
print(newstr)
newstr2=newstr.split("|")
print(newstr2)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com