Python黑马学习笔记Day03

# 字符串

可以用单引号,双引号和三引号定义

name = 'wang'
print(type(name), name)  # <class 'str'> wang
name = "yi"
print(type(name), name)  # <class 'str'> yi
name = '''bo'''
print(type(name), name)  # <class 'str'> bo
name = """cute"""
print(type(name), name)  # <class 'str'> cute

# 字符串 * n 可以将字符串打印n遍

a = '*' * 5  # '*****'
print(a)
b = '3' * 6  # '333333'
print(b)

# 字符串的输入和输出

input()  # 输入,函数的到的内容就是字符串
print()  # 输出 %s 或者 f"{}"

# 下标

# 下标(索引,编号)可以是正整数也可以是负整数
# 正整数下标是从0开始的,表示第一个字符
# 负整数下标是从-1开始的,表示最后一个字符

my_str = 'hello'
print(my_str[0])  # h
print(my_str[1])  # e
print(my_str[-1])  # o
print(my_str[-4])  # e
# len可以得到字符串长度
print(len(my_str))  # 5
# 使用正数下标书写字符串中最后一个元素
print(my_str[len(my_str)-1])  # o

#切片

# 切片:会得到一个新的字符串
# 切片可以获取一段数据,下标只能获得一个数据
# 切片语法:变量[start:end:step] ,start 表示开始位置的下标 , end 表示结束位置的下标, step 表示步长,下标之间的间隔,默认是1

my_str1 = my_str[1:3]  # el 下标为1和2的字符
my_str2 = my_str[2:4]  # ll 下标为2和3的字符
my_str3 = my_str[1:4:2]  # el 下标在1到4之间,从下标1的字符开始,每隔两个字符取一个
# 当step为1时可省略不写,当end不写时可以取到最后一个元素(冒号不可省略),当start不写时表示从第第一个开始取(冒号不可省略)
my_str4 = my_str[1:]  # ello
my_str5 = my_str[:2]  # he 下标为0和1的,下标为2的取不到
my_str6 = my_str[:]  # hello 得到和原来一样的字符
my_str7 = my_str[3:1]  # 无数据
my_str8 = my_str[-3:-1]  # ll
# step可以为负数
my_str9 = my_str[3:1:-1]  # ll  位置换成1:3无法出结果
my_str10 = my_str[::-1]  # 字符串反转 olleh
my_str11 = my_str[::2]  # hlo 取下标为0,2,4的值

# 字符串常见操作

my_str = 'hello world itcast and itcastcpp'  # 定义一个字符串变量

# 查找类:find(),index(),count()

# find() 在字符串中查找是否存在某个字符串
# my_str.find(sub_str, start, end)
# sub_str:要在字符串中查找的内容
# start:开始位置,从哪里开始查找,默认是0
# end:结束位置,查找到哪里结束,默认是len()
# 返回值:即方法执行的结果是什么,如果找到sub_str,返回的sub_str在my_str中的位置的正数下标,没有找到则返回-1

my_str1 = my_str.find('hello')  # 0
my_str2 = my_str.find("hello", 3)  # 从下标为3的位置开始查找字符串'hello' ,未找到,返回值-1
my_str3 = my_str.find("itcast")  # 12 返回第一个字符的下标,空格也算一个字符
my_str4 = my_str.find("itcast", 15)  # 23
# rfind()  right find() 从右边(后边)开始查找
my_str5 = my_str.rfind("itcast")  # 23

# index() 在字符串中查找是否存在某个字符串
# my_str.index(sub_str, start, end)
# sub_str:要在字符串中查找的内容
# start:开始位置,从哪里开始查找,默认是0
# end:结束位置,查找到哪里结束,默认是len()
# 返回值:即方法执行的结果是什么,如果找到sub_str,返回的sub_str在my_str中的位置的正数下标,没有找到则会报错
# 与find()的区别就在没找到会报错,其他操作和参数都不变

my_str2 = my_str.index("hello", 3)  # 未找到,代码报错

# rindex()与rfind()一样

# count(sub_str, start, end) 统计出现次数

my_str1 = my_str.count('aaaa')  # 0
my_str2 = my_str.count('hello')  # 1
my_str3 = my_str.count('itcast')  # 2
my_str4 = my_str.count('itcast', 20)  # 1

# 字符串替换操作
# replace(old_str, new_str, count)
# old_str:将要被替换的字符串
# new_str:替换成的新的字符串
# count:替换次数,默认是全部替换
# 返回值:得到一个新的字符串,不会改变原来的字符串

my_str1 = my_str.replace('itcast', 'itheima')  # hello world itheima and itheimacpp
my_str1 = my_str.replace('itcast', 'itheima', 1)  # hello world itheima and itcastcpp

# 字符串切割操作
# my_sub.split(sub_str, count) 将my_str字符串按照sub_str进行切割
# sub_str: 按照什么内容切割字符,默认是空白字符(空格键,Tab键)
# count: 切割几次,默认是全部切割
# 返回值: 列表 []

result = my_str.split()  # 按照空白字符全部切割  ['hello', 'world', 'itcast', 'and', 'itcastcpp']
result1 = my_str.split('itcast')  # 按照'itcast'字符全部切割  ['hello world', 'and', 'cpp']
result2 = my_str.split('itcast', 1)  # 按照'itcast'字符切割一次  ['hello world', 'and itcastcpp']
# rsplit() 从右边开始切割
result3 = my_str.rsplit('itcast',1)  # 按照'itcast'字符从右开始切割一次  ['hello world itcast and', 'cpp']

# 字符串连接操作
# join(可迭代对象)
# 可迭代对象:str, 列表(需要列表里的每个元素都是str类型)
# str.join():将str这个字符串添加到可迭代对象的两个元素之间
# 返回值:一个新的字符产,不会改变原字符串的值

my_str = '_'.join('hello')  # 会把'_'加入到'hello'每两个元素之间 即:h_e_l_l_o
my_str1 = '_*_'.join('hello')  # 会把'_*_'加入到'hello'每两个元素之间 即:h_*_e_*_l_*_l_*_o
my_list = ['hello', 'cpp', 'python']  # 定义一个列表
my_list1 = '_'.join(my_list)  # hello_cpp_python
my_list1 = '_*_'.join(my_list)  # hello_*_cpp_*_python
my_list1 = ' '.join(my_list)  # hello cpp python

# 其他操作

my_str = '  hello itcast and itcastapp  '
my_str.capitalize()  # 将第一个单词的首字母大写,即:  Hello itcast and itcastapp
my_str.title()  # 将每一个单词的首字母大写,即:  Hello Itcast And Itcastapp
my_str.upper()  # 将第所有字母大写,即:  HELLO ITCAST AND ITCASTCPP
my_str.lower()  # 将第所有字母小写,即:  Hello itcast and itcastapp
my_str.islower()  # 判断全部是不是小写
my_str.startswith('he')  # 判断是不是以he(括号里的字符)为开头的,如果是输出True,不是则输出False,此处为True
my_str.ljust(30)  # 返回一个原字符串左对齐,并使用空格填充至长度为30的新字符串即
my_str.rjust(30)  # 返回一个原字符串右对齐,并使用空格填充至长度为30的新字符串
my_str.center(30)  # 返回一个原字符串居中,并使用空格填充至长度为30的新字符串
my_str.lstrip()  # 删除my_str左边的空白字符  hello itcast and itcastapp
my_str.rstrip()  # 删除my_str右边的空白字符     hello itcast and itcastapp
my_str.strip()  # 删除my_str两边的空白字符hello itcast and itcastapp
my_str.partition('itcast')  # 把my_str分割成三部分,itcast前,itcast,itcast后,即:('     hello ', 'itcast', ' and itcastapp     ')
my_str.rpartition('itcast')  # 类似于partition,不过是从右边开始,即:('     hello itcast and ', 'itcast', 'app     ')
my_str.splitlines()  # 按照行分割,返回一个包含各行作为元素的列表
my_str.isalpha()  # 如果my_str所有字符都是字母,则返回True,否则返回False
my_str.isdigit()  # 如果my_str只包含数字则返回True,否则返回False
my_str.isalnum()  # 如果my_str所有字符都是字母或数字组成,则返回True,否则返回False
my_str.isspace()  # 如果my_str中只包含空格,则返回True,否则返回False

# 列表

# 列表:是一种数据类型,可以存放多个数据,列表中的数据可以是任意类型的
# 列表list,用[]进行定义

my_list = []  # 定义空列表
my_list1 = list()  # 定义空列表
my_list2 = [1, 3.14, True, 'hello']
num = len(my_list2)  # 求列表中数据元素的个数,即列表的长度

# 列表支持下标和切片操作

my_list3 = my_list2[1]  # 3.14
my_list4 = my_list2[-1]  # hello
my_list5 = my_list2[1:3]  # [3.14, True]

# 下标操作和字符串不同的是,字符串不能使用下标修改其中的数据,但是列表可以使用下标修改列表中的数据

my_list2[0] = 18  # [18, 3.14, True, 'hello']
my_list2[0] = 'python'  # ['python', 3.14, True, 'hello']

# 列表遍历元素

my_list = ['王一博', '酷盖', '王甜甜', '王椰啵']
for i in my_list:
    print(i)  # i就是列表中的每个数据
# 列表遍历下标
num = len(my_list)
i = 0
while i < num:
    print(f"{my_list[i]}的下标是{i}")
    i += 1

结果:

 # 列表的相关操作

# 添加元素(append,extend,insert),都是在原数据上添加的,不会返回新列表
# my_list.append(数据)  向my_list中添加数据

my_list.append('谢允')  # ['王一博', '酷盖', '王甜甜', '王椰啵', '谢允']

# my_str.insert(下标,数据)  在指定的下标位置添加数据

my_list.insert(0, '季向空')  # 在列表下标为0的地方添加数据,即:[‘季向空’, '王一博', '酷盖', '王甜甜', '王椰啵', '谢允']

# my_list.extend(可迭代对象:str或列表) 会将可迭代对象中的数据逐个添加到列表的末尾

my_list.extend('蓝忘机')  # [‘季向空’, '王一博', '酷盖', '王甜甜', '王椰啵', '谢允', '蓝', '忘', '机']
my_list.extend(['蓝忘机', 85, 1997])  # ['季向空', '王一博', '酷盖', '王甜甜', '王椰啵', '谢允', '蓝', '忘', '机', '蓝忘机', 85, 1997]

# 数据查询(index(),count(),in/not in)
# index() 根据数据值,查找元素所在的下标,找到返回元素的下标,没有找到程序报错
# 列表中没有find()方法

my_list = [1, 3.14, True, 'hello']
num = my_list.index(3.14)  # 查找3.14所在下标,即:1
print(num)

# count() 统计出现的次数

num1 = my_list.count(3.14)  # 统计3.14出现的次数,即:1
print(num1)
num2 = my_list.count(1)  # 结果为2,因为列表中的True会被当成1,所以算出现两次

# in/not in 判断是否存在,存在是True,不存在是False

num3 = 3.14 in my_list  # True
num4 = 3.14 not in my_list  # False

# 删除操作

my_list = [1, 2, 4, 5, 6, 9]

# 1.根据元素数据值删除 remove(),直接删除原列表中的数据,删除的数据不存在时会报错

my_list.remove(4)  # [1, 2, 5, 6, 9]
print(my_list)

# 2.根据下标删除
# pop() 默认删除最后一个数据,返回删除内容,括号里有下标时按下标删除,当输入的下标在列表中不存在时程序报错

num = my_list.pop()  # 删除最后一个数据9
print(num)  # 9
print(my_list)  # [1, 2, 5, 6]
num = my_list.pop(2)  # 删除下标为2的数据5
print(num)  # 5
print(my_list)  # [1, 2, 6]

# del my_list[下标] 删除列表中下标位置的数据,当输入的下标在列表中不存在时程序报错

del my_list[1]  # 删除下标位置为1的数据2
print(my_list)  # [1, 6]

# 列表排序
# 想要对列表中的数据进行排序,前提是列表中的数据类型是一样的
# list.sort() 直接在原列表进行排序

my_list = [1, 5, 3, 7, 9, 6]
my_list.sort()  # 默认从小到大(升序),即 [1, 3, 5, 6, 7, 9]
print(my_list)
my_list.sort(reverse=True)  # 从大到小(降序),即 [9, 7, 6, 5, 3, 1]
print(my_list)

# sorted(list) 不会在原列表中进行排序,会生成一个新的列表

my_list = [1, 5, 3, 7, 9, 6]
my_list1 = sorted(my_list)
print(my_list)  # [1, 5, 3, 7, 9, 6]
print(my_list1)  # [1, 3, 5, 6, 7, 9]
my_list2 = sorted(my_list,reverse=True)
print(my_list)  # [1, 5, 3, 7, 9, 6]
print(my_list2)  # [9, 7, 6, 5, 3, 1]

# 列表逆置

my_list = ['a', 'b', 'c', 'd', 'e']
my_list3 = my_list[::-1]
print(my_list)  # ['a', 'b', 'c', 'd', 'e']
print(my_list3)  # ['e', 'd', 'c', 'b', 'a']

# 在原列表中直接逆置 reverse()

my_list.reverse()
print(my_list)  # ['e', 'd', 'c', 'b', 'a']

# 列表嵌套

school_names = [['北京大学', '清华大学'],
                ['浙江大学', '复旦大学'],
                ['四川大学', '武汉大学']]
print(school_names[1])  # ['浙江大学', '复旦大学']
print(school_names[1][1])  # 复旦大学
print(school_names[1][1][1])  # 旦
for schools in school_names:
    print(schools)
# ['北京大学', '清华大学']
# ['浙江大学', '复旦大学']
# ['四川大学', '武汉大学']
    for name in schools:
        print(name)
# ['北京大学', '清华大学']
# 北京大学
# 清华大学
# ['浙江大学', '复旦大学']
# 浙江大学
# 复旦大学
# ['四川大学', '武汉大学']
# 四川大学
# 武汉大学

# 案例:分配工作室
# 一个学校,有三个办公室,现在有8位老师等待工位的分配,请编写程序,完成随机的分配

import random
offices = [[], [], []]  # 定义一个列表存放3个办公室,对应下标0, 1, 2
teachers = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']  # 定义一个列表存放老师
for i in teachers:  # 让每个老师出来选择
    num = random.randint(0, 2)  # 每个老师抓阄去几号办公室,即让电脑产生随机数
    offices[num].append(i)  # 将老师分别添加进各个offices中
    print(f"老师{i}去了{num}号办公室")
print(offices)  # 打印出所有办公室列表
for count in range(0, 3):
    print(f"{count}号办公室有{len(offices[count])}人")  # count号的办公室里有多少人
    for name in offices[count]:
        print(f"老师的名字为{name}")  # 每个办公室里有那些老师

结果:

           

# 元组

# 元组 也是一种数据类型
# 元组与列表类似,可以存放多个数据,可以存放不同数据类型,但是区别在于元组不能修改里面的元素,也就是说没有增删改查的操作
# 元组用()定义

my_tuple = ()  # 定义空元组,无意义,因为无法修改
my_tuple1 = tuple()  # 定义空元组
my_tuple2 = (3)  # 定义一个数据元素是int类型
print(type(my_tuple2))  # <class 'int'>

# 要想定义一个只有一个数据元素的元组

my_tuple3 = (3,) # 数据元素后面一定要有一个逗号!!!
print(type(my_tuple3))  # <class 'tuple'>
my_tuple = (18, 3.14, True, 'hello')

# 元组支持下标和切片操作

my_tuple1 = my_tuple[1]
print(my_tuple1)  # 3.14

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值