python7

元祖类型 type(info_tuple) tuple(元祖的意识)

info_tuple[0] #取出数据

empty_tuple=() #空元祖
single_tuple=(5) #int类型,变量类型
single_tuple=(5,) #元祖类型,单元素

info.count() #统计
info.index() #索引
元祖只有两种类型

info_tuple=(‘zhangsan’,18,1.75)
print(info_tuple[0])

已经知道数据的内容,希望知道它的位置

print(info_tuple.index(‘zhangsan’))

#统计计数
print(info_tuple.count(‘zhangsan’))

print(len(info_tuple))

针对元祖遍历不是很多

函数的参数和返回值
格式字符串
保护数据,改成元祖

info_tuple=(‘小明’,21,1.85)
print(’%s 年龄是 %d 身高是%.2f’ % info_tuple)
inf_str=(’%s 年龄是 %d 身高是%.2f’ % info_tuple) #格式字符串生成新的字符串
print(inf_str)

元祖和列表之间的转换:
list(元祖) #把元祖转换成列表
tuple(列表) #把列表转成元祖

num_list=[1,2,3,4]
type(num_list)
print(type(num_list))
num_tuple=tuple(num_list)
print(type(num_tuple))


列表是有序的对象集合
字典是无序的对象集合

字典: key value 字典用 { } 大括号定义
键和值之间用 : 冒号分隔
键必须是唯一的,值可以取任何类型
但是键必须是 --------------字符串 数字 元祖

字典是除列表以外最灵活的数据

shift+F10运行脚本
xiaoming = {‘name’:‘小明’,
‘age’: 18}
print(xiaoming)

xiaoming = {‘name’: ‘小明’}
#1.取值
print(xiaoming[‘name’])
#2. 增加/修改
#如果key不存在,会新增键值对
#如果key存在,会修改已经存在的键值对
xiaoming[‘age’]=18
xiaoming[‘name’]=‘小小明’
#3. 删除
#在删除指定键值对的时候,如果指定的key不存在,程序会报错
xiaoming.pop(‘name’)
print(xiaoming)

字典的常用操作:
uptate 添加数据,需要被合并的字典中
包含已经存在的键值对,会覆盖原有的

xiaoming = {‘name’: ‘小明’,
‘age’: 18}
#1.统计键值对数量
print(len(xiaoming))

#2.合并字典
#会覆盖原有的
temp_dict={‘height’:1.75
‘age’: 20}
xiaoming.update(temp_dict)

#3.清空字典
xiaoming.clear()

print(xiaoming)

clear update len

xiaoming={‘name’: ‘小明’,
‘qq’: ‘123456’,
‘phone’: ‘10086’}
#变量k是每一次循环中,获取到的键值对的key
for k in xiaoming:
print(’%s - %s’ % (k,xiaoming[k]))

应用场景:
card_list=[
{‘name’: ‘张三’,
‘qq’: ‘12345’,
‘phone’: ‘110’},
{‘name’: ‘李四’,
‘qq’: ‘12335’,
‘phone’: ‘113430’}
]
for card_info in card_list:
print(card_info)

字符串:
str1=‘hello python’
str2=“我的外号是’大西瓜’”
print(str2)
print(str1[6])

for char in str2:
print(char)

index 字符串出现的位置

hello_str=‘hello hello’
#1.统计字符串长度
print(len(hello_str))

#2.统计某一个小字符串出现的次数
print(hello_str.count(‘llo’))
print(hello_str.count(‘abc’))

#3.某一个字符串出现的位置
print(hello_str.index(‘llo’))

字符串 .center #居中的意识
.endswith #以什么什么结尾
.is #is开头用来判断的

hello_str.capitalize(
hello_str.isdigit( #只包含数字,全角数字,(1),\u00b2
hello_str.rfind(
hello_str.casefold(
hello_str.isidentifier(
hello_str.rindex(
hello_str.center(
hello_str.islower( #
hello_str.rjust(
hello_str.count(
hello_str.isnumeric( #只包含数字,全角数字,汉字数字
hello_str.rpartition(
hello_str.encode(
hello_str.isprintable(
hello_str.rsplit(
hello_str.endswith(

hello_str.isspace( #如果字符串中,只包换空格,返回True /n /r 空白字符

hello_str.rstrip(
hello_str.expandtabs(
hello_str.istitle( #每个单词的首字母大写,True
hello_str.split(
hello_str.find(
hello_str.isupper(
hello_str.splitlines(
hello_str.format(
hello_str.join(
hello_str.startswith(
hello_str.format_map(
hello_str.ljust(
hello_str.strip(
hello_str.index(
hello_str.lower(
hello_str.swapcase(
hello_str.isalnum( #由字母或者数字组成,则返回True
hello_str.lstrip(
hello_str.title(
hello_str.isalpha( #由字母组成,则返回True
hello_str.maketrans(
hello_str.translate(
hello_str.isascii(
hello_str.partition(
hello_str.upper(
hello_str.isdecimal( #只包含数字,返回True, 全角数字
hello_str.replace(
hello_str.zfill(

6:拆分和连接
5: 去掉空白字符
4: 文本对齐
3: 大小写转换
2: 查找和替换
1:判断类型

space_str =" \t\n\r"
print(space_str.isspace())
True

num_str=“一千零一”
print(num_str)
print(num_str.isdecimal()) #纯数字
print(num_str.isdigit()) #unicode 字符串
print(num_str.isnumeric()) #中文数字 字符串

hello_str=‘hello world’
#1.判断是否以指定字符串开始
print(hello_str.startswith(‘hello’))
#2.判断是否以指定字符串结束
print(hello_str.endswith(‘world’))
#3.查找指定字符串
print(hello_str.find(‘llo’)) #索引位置

index 不存在则报错

find 不会报错,会返回-1

#4.替换字符串
print(hello_str.replace(‘world’,‘python’)) #会返回一个新的字符串,不会修改原来字符串的内容
print(hello_str)


width: 宽度
fillchar: 填充符号

poem = [‘登鹤雀楼’,
‘王之焕’,
‘白日依山尽’,
‘黄河入海流’,
‘欲穷千里目’
‘更上一层楼’]
for poem_str in poem:
# print("|%s|" % poem_str.center(10," “))
# print(”|%s|" % poem_str.ljust(10, " “))
print(”|%s|" % poem_str.rjust(10, " "))

去除空白字符:

poem = [’\t\n登鹤雀楼’,
‘王之焕’,
‘白日依山尽\t\n’,
‘黄河入海流’,
‘欲穷千里目’
‘更上一层楼’]
for poem_str in poem:
# print("|%s|" % poem_str.center(10," “))
# print(”|%s|" % poem_str.ljust(10, " “))
#先使用strip方法去除字符串中的空白字符
#再使用center方法居中显示文本
print(”|%s|" % poem_str.strip().center(10, " "))

拆分和连接字符串:

		poem_str='登鹤雀楼\t 王之涣\t 白日依山尽\t \n 黄河如海浪\t\t 欲穷千里目\n 更上一层楼'

print(poem_str)

#1.拆分字符串
poem_list=poem_str.split() # split 拆分
print(poem_list)

#2.合并字符串
result=" ".join(poem_list) #join 合并
print(result)

切片: 字符串 列表 元祖

【字符串:开始索引:结束 索引:步长】

顺序 012345
倒序 -1-2-3-4-5

num_str[:]
‘0123456789’

num_str[2:-1]
‘2345678’

num_str[-2:]
‘89’

num_str[::-1]
‘9876543210’

num_str[-1::-1]
‘9876543210’

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值