python列表与元组

本文详细介绍了Python中的列表和元组,包括它们的定义、特点、创建方式、常用操作和区别。列表是可变的有序集合,可以进行增、删、改、查等操作,而元组是不可变的有序集合,强调其不可修改性。文章还深入讨论了列表的列表解析式、生成式以及深浅拷贝的概念,元组的创建和常见操作。
摘要由CSDN通过智能技术生成

python列表与元组

格式化

print()函数查看帮助文件

>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

以逗号隔开的元素一起输出会以元组形式输出

>>> a,b,c,*d = 1,2,3,4,5,6
>>> a
1
>>> b
2
>>> c
3
>>> d
[4, 5, 6]

语法格式:

print(value,...,sep=' ',end='\n',file=sys.stdout,flush=False)

等价于

print(value1,value2,value3,...,valuen,sep=' ',end='\n',file=sys.stdout,flush=False)
>>> a,b,c,*d = 1,2,3,4,5,6
>>> print(a,b,c,d)
1 2 3 [4, 5, 6]
>>> print(a,b,c,d,sep='$')
1$2$3$[4, 5, 6]

>>> x = 666
>>> import sys
>>> sys.stdout.write(str(x))
6663
>>> y = 66
>>> sys.stdout.write(str(y))
662

输出格式化:

name = "Kate"
age = 20
print("his name is %s, his age is %d." % (name, age))
print("his name is", name, ',his age is', age, '.')
print("his name is " + name + ' his age is ' + str(age) + '.')

格式化字符串

%号格式化和占位符
格式 描述
%d 有符号的整数(十进制)
%s 字符串
%c 字符及ASCII码
%o 无符号八进制整数
%x/%X 无符号十六进制整数
%e/%E 浮点数,科学记数法
%f 浮点数
%u 格式化无符号整型
%g/%G 根据值的大小决定使用%f或%e
%p 用十六进制数格式化变量的地址

%格式化字符串,用%匹配参数,注意个数一一对应

>>> "%d%%" % 100
'100%'

%格式化和format格式化,字符串对齐

format格式字符串

位置映射
print("name:{}, age:{}".format('Tom', 18))

#输出结果:name:Tom, age:18

print("name:{}, age:{}, {address}".format('Tom', 18, address='Hangzhou'))

#输出结果:name:Tom, age:18, Hangzhou
#address这类要放在关键字后面
print('第一个元素是:{0[0]},第二个元素是:{0[1]},第三个元素是:{0[2]}'.format(('www.', 'google.', 'com')))

#输出结果:第一个元素是:www.,第二个元素是:google.,第三个元素是:com

print('第一个元素是:{0[0]},第二个元素是:{0[1]},第三个元素是:{0[2]},第四个元素是:{1[0]},第五个元素是:{1[1]}'.format(('www.', 'google.', 'com'),('baidu.','com')))

#输出结果:第一个元素是:www.,第二个元素是:google.,第三个元素是:com,第四个元素是:baidu.,第五个元素是:com

字符串和数字的id都是不可更改的

列表

1、定义:
  • 列表是用方括号括起来的[ ],每个元素以逗号分隔;
  • 可以储存不同的数据类型数据
2、特点:
  • 是一个有序的
  • 可以重复的
  • 可变类型
3、列表的创建:
  • 创建空列表
li = []
print(type(li))

#输出结果:<class 'list'>
  • 创建多元列表
li = [1, 2, 3, "abcd", "city", "college", ['i', 'love', 'python']]
print(type(li))

#输出结果:<class 'list'>
4、列表的常用方法

(1)拼接

li1 = ['i']
li2 = ['love', 'python']
print(li1, li2)

#输出结果:['i'] ['love', 'python']

(2)重复

li2 = ['love', 'python']
print(li2 * 3)

#输出结果:['love', 'python', 'love', 'python', 'love', 'python']

(3)索引(偏移)

li = ['c', 'i', 't', 'y', 'city', 'college', 'zhejiang', ['city', 'college', 'zhejiang']]
print(len(li))
print(li[0], li[-7])
print(li[:5])
print(li[:-1])
print(li[4][1])
print(li[7][1])

'''
输出结果:
8
c i
['c', 'i', 't', 'y', 'city']
['c', 'i', 't', 'y', 'city', 'college', 'zhejiang']
i
college
'''

(4)强转

sr = 'abcd'
li = list(sr)
print(li, type(li))

#输出结果:['a', 'b', 'c', 'd'] <class 'list'>
5、列表的常见操作
    • append()
    li = ['city', 'college']
    print(id(li))
    li1 = ['a', 'b'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值