学习python记录——第五天

前言

冲冲冲学习起来!


字符串

# 1、切分字符串
language = "Python and Java and C++ and Golang and Scala"
# split 切割字符串 生成一个列表: 暂时理解为一个容器 有序序列
result1 = language.split("and")
print(result1)

# 2、连接序列 生成字符串 跟split 是相反的操作
lang = ["English", "Chinese", "Jananese"]
# 通过 - 连接上面的语言  形成字符串
result2 = "-".join(lang)
print(result2, type(result2))

# 3、删除字符串两边的空格 strip
class_name = "  Big Data "
print(len(class_name))
# 删除两边空格
class_name_new = class_name.strip()
print(class_name_new, len(class_name_new))

# 4、判断一个字符串是否以指定子串开始
mystr = "hello world"
# mystr 以hello开始 则返回True
print(mystr.startswith("hello"))
# 不是以world开口 则返回False
print(mystr.startswith("world"))
# 以world结束 返回True
print(mystr.endswith("world"))
# 判断在指定范围内是否以hello开始
print(mystr.startswith("hello", 3, 8))
print(mystr.startswith("lo", 3, 8))




列表 :列表可以一次存储多个数据,格式如下:[数据1, 数据2, 数据3, 数据4......]

# 列表 [], 然后里面可以是任何类型的数据 12,23.6,“”, []

# 列表本质上是一个序列0     1        2        3     4
name_list = ["James", "蔡徐坤", "罗志祥", "格林", 2022]
# len表示列表长度
print(name_list, type(name_list), len(name_list))
# 1、列表索引查找
print(name_list[0])
print(name_list[1])
print(name_list[3])
print(name_list[2])
print(name_list[4])

# 使用index查找指定的数据 返回指定数据在列表 中的位置
print(name_list.index("格林"))
# 在指定的列表范围内 查找格林 没有找到 则报错
# print(name_list.index("格林", 0, 2))

# 2、统计一个元素在 列表中的个数 count
name_list2 = ["蒋卢", "吴苹雨", "李龙波", "蒋卢"]
result1 = name_list2.count("蒋卢")
result2 = name_list2.count("李龙波")
result3 = name_list2.count("饶鹏鹏")
print(result1, result2, result3)

# 3、计算列表长度
print(len(name_list))
print(len(name_list2))

# 4、判断指定元素是否存在
name_list3 = ["廖警官", "涛涛", "卢涛", "高宇"]
print("涛涛" in name_list3)
print("杨主峰" in name_list3)
print("覃喜文" not in name_list3)
print("卢涛" not in name_list3)

# 5、增加一个元素到列表中  加到列表的末尾
name_list3.append("杨主峰")
print(name_list3)

# 追加一个序列 将一个列表整体加入到列表中
name_list3.append(["孙涛", "张恩"])
print(name_list3)

# 追加一个序列 将序列中的值一个一个加入进去
name_list3.extend(["峰峰", "庆庆"])
print(name_list3)

# 在指定的位置上 插入一个数据
name_list3.insert(1, "良好")
print(name_list3)


# 1、删除列表
name_list1 = ["张飞", "关羽", "刘备"]
print("删除前:", name_list1)
del name_list1
# 删除之后 name_list1 不存在 报错
# print("删除后:", name_list1)

# 删除列表中的指定下标的元素
#               0        1      2      3
name_list2 = ["孙悟空", "唐僧", "八戒", "沙僧"]
# del 直接删除 没有返回值
del name_list2[1]
print(name_list2)

# 删除掉 指定下标的元素 然后返回该元素
result1 = name_list2.pop(1)
print(name_list2)
print(result1)

# pop里面没有参数  则默认删除列表中的最后一个元素 然后返回该元素
name_list3 = ["帅帅", "东东", "根根"]
result2 = name_list3.pop()
print(result2)
print(name_list3)

# remove删除指定元素 没有返回值
name_list4 = ["田田", "豪豪", "浩浩"]
name_list4.remove("豪豪")
print(name_list4)

# 清空列表  没有返回值
name_list4.clear()
print(name_list4)

# 2、修改列表    0       1       2
name_list5 = ["孝孝", "昊昊", "冯婕小仙女"]
name_list5[0] = "荣荣"
print(name_list5)

# 3、列表翻转  没有返回值
name_list5.reverse()
print(name_list5)

# 4、排序 默认是从小到大
score_list = [35, 89, 77, 0]
score_list.sort()
print(score_list)
# 从大到小进行排序
score_list.sort(reverse=True)
print(score_list)

# 5、复制列表
height_list = [183, 155, 185, 145]
height_list_new = height_list.copy()
print("新的复制列表:", height_list_new)
print("原来的列表:", height_list)


# while 循环列表   0        1        2        3
country_list = ["乌克兰", "俄罗斯", "漂亮国", "中国"]
i = 0
while i < len(country_list):
    print(i, country_list[i])
    i += 1

print("=========================================")
# for循环 循环列表
scenery_list = ["船舶大楼", "毛家屋场", "白鹿寺", "秀峰公园"]
# 通过j这个临时变量 挨个地取列表中取数据 从头取到尾 没有更多数据之后结束
for j in scenery_list:
    print(j)


# 列表嵌套          0               1            2
name_list = [["宏宏", "伟伟"], ["天天", "顺顺"], "廖警官"]
print(name_list[0])
# 单独把伟伟取出来
print(name_list[0][1])
name_list[0].append("亮亮")
print(name_list)

一点多慢慢消化! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值