当前位置: 首页 > news >正文

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)
http://www.lryc.cn/news/423965.html

相关文章:

  • Linux企业级应用(一)构建企业级Linux应用平台:全面指南
  • LeetCode112 路径总和
  • TI AWR1843 毫米波雷达实物展示
  • 前端JS总结(下)之事件操作
  • 如何妙用哈希表来优化遍历查找过程?刷题感悟总结,c++实现
  • 【设计模式】漫谈设计模式
  • 第N5周:Pytorch文本分类入门
  • SpringBoot 自定义 starter
  • TDengine Invalid data format 问题定位
  • Spring Boot 使用 MongoDB 教程
  • Python办公自动化:使用openpyxl 创建与保存 Excel 工作簿
  • 【张】#11 Union 共用体
  • Xcode 在原生集成flutter项目
  • ES6的promise
  • 轻松找回:如何在PostgreSQL 16中重置忘记的数据库密码
  • EVAL长度突破限制
  • 如何判断树上一个点是否在直径上
  • docker 部署 RabbitMQ
  • 设计模式 - 过滤器模式
  • 使用 Locust 进行本地压力测试
  • 【图形学】TA之路-矩阵应用平移-旋转-大小
  • Spring 循环依赖解决方案
  • 可视化大屏:如何get到领导心目中的“科技感”?
  • 基于Python的金融数据采集与分析的设计与实现
  • 使用Sanic和SSE实现实时股票行情推送
  • redis散列若干记录
  • Java面试八股之什么是STOMP协议
  • 【自用】Python爬虫学习(一):爬虫基础与四个简单案例
  • [python]uiautomation.WindowControl函数用法
  • 学习记录第二十七天