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.
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
等价于
print(value1,value2, ...,valuen, sep=' ', end='\n', file=sys.stdout, flush=False)
a,b,c,*d=1,2,3,4,5,6
>>> a
1
>>> b
2
>>> c
3
>>> d
[4, 5, 6]
>>> print(a,b,c,d,sep="$")
1$2$3$[4, 5, 6]
>>>
>>> for i in range(5):
... print(i)
...
0
1
2
3
4
>>> for i in range(5):
... print(i,end=" ")
...
0 1 2 3 4 >>>
print("%d"% 6688) # 可以进行格式转化
name = "Tony"
age = 18
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 | 浮点数 |
%格式化字符串 用%匹配参数,注意个数一一对应。
“%d%%”%a---->‘100%’
“%o”%21---->‘25’
(1)位置映射
print("Name:{},age:{}".format('tom',18))
# Name:tom,age:18
(2) 关键字映射
print("{address},Name:{},age:{},".format('tom',18,address="hangzhou "))
# hangzhou ,Name:tom,age:18,
print("第一个元素是:{0[0]},第二个元素的值是{0[1]},第三个元素的值是{0[2]},第四个元素是{1[0]},第五个元素是{1[1]}".format(('I','love','charon'),("forever","love")))
# 第一个元素是:I,第二个元素的值是love,第三个元素的值是charon,第四个元素是forever,第五个元素是love
列表使用方括号括起来的[],每个元素以逗号分隔;
可以储存不同的数据类型数据的序列
#创建空列表
li = []
print(type(li))
# 创建多元素列表
li = [1,2,3,"abcd","city",["pluto","charon","love"]]
print((li,type(li)))
# 强转
sr="abcd"
li= list(sr)
print((li,type(li)))
#(['a', 'b', 'c', 'd'], )
li1 = ["I"]
li2 = ["love","python"]
print(li1+li2)
#['I', 'love', 'python']
li2 = ["love", "python"]
print(li2 * 3)
# ['love', 'python', 'love', 'python', 'love', 'python']
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[7][1])
# 8
# ('c', 'i')
# ['c', 'i', 't', 'y', 'city']
# [['city', 'college', 'zhejiang'], 'zhejiang', 'college', 'city', 'y', 't', # 'i', 'c']
# college
增删改查
sr = "冥王星和卡戎"
li = []
for i in sr :
li.append(ord(i))
print(li)
## [20901, 29579, 26143, 21644, 21345, 25102]
# entend()
li = ["City","college"]
li1 = ["a","b","c"]
li.extend(li1)
print(li)
# insert(),按照索引添加
li.insert(1,li1)
print(li)
# 改
li[2:6] = ["x","y","z"]
print(li)
# ['City', 'college', 'a', 'b', 'c']
# ['City', ['a', 'b', 'c'], 'college', 'a', 'b', 'c']
# ['City', ['a', 'b', 'c'], 'x', 'y', 'z']
# 删除
# pop()
li = ["a","b","c","d","e"]
print(li.pop(2))
print(li)
#c
#['a', 'b', 'd', 'e']
li = ["a","b","c","d","e","love","e"]
print(li.remove("e"))
print(li)
del li[5]
print(li)
li.clear()
print(li)
# None
# ['a', 'b', 'c', 'd', 'love', 'e']
# ['a', 'b', 'c', 'd', 'love']
# []
索引,切片
(1) 元素遍历
(2) 索引遍历
(3)枚举遍历
# 元素遍历
li = ["a","b","c"]
for i in li :
print(i)
# 索引遍历
li = ["a","b","c"]
for i in range(len(li)):
print(li[i],end=" ")
#枚举遍历
li = ["a","b","c"]
for i in enumerate(li[:2],2):
print(i)
# (2, 'a')
# (3, 'b')
赋值是对对象的引用
深浅拷贝
浅拷贝会创建新对象,内容是原对象的引用
三种实现方法:
la = [1,2,3,4,5,'a',['b1','b2']]
lb = la
lb2 = la[:]
print(id(la),id(lb),id(lb2))
la[5] = 'aaa'
print(la,lb,lb2)
#浅拷贝
import copy
lb_copy = copy.copy(la)
print(id(la),id(lb_copy))
la.append('test') # 添加原子类型,不会影响lb_copy
print(la)
print(lb_copy)
#2843089442888 2843089442888 2843090259144
# [1, 2, 3, 4, 5, 'aaa', ['b1', 'b2']] [1, 2, 3, 4, 5, 'aaa', ['b1', 'b2']] [1, 2, 3, 4, 5, 'a', ['b1', 'b2']]
# 2843089442888 2843091748936
# [1, 2, 3, 4, 5, 'aaa', ['b1', 'b2'], 'test']
# [1, 2, 3, 4, 5, 'aaa', ['b1', 'b2']]import copy
# 深拷贝
la = [1,2,3,4,5,'a',['b1','b2']]
lb_deepcopy = copy.deepcopy(la)
print(la,id(la))
print(lb_deepcopy,id(lb_deepcopy))
la[6][1] = "aaa"
print(la)
print(lb_deepcopy)
# [1, 2, 3, 4, 5, 'a', ['b1', 'b2']] 2068748454152
# [1, 2, 3, 4, 5, 'a', ['b1', 'b2']] 2068748455432
# [1, 2, 3, 4, 5, 'a', ['b1', 'aaa']]
# [1, 2, 3, 4, 5, 'a', ['b1', 'b2']]
对象的引用
浅拷贝相当于我们只拷贝了一层,修改外层元素,会修改引用,让他们指向另一个位置:
修改嵌套列表的元素,列表地址没有发生变化,还指向的同一个位置。
深拷贝是拷贝对象的所有元素,包括多层嵌套元素。因此,是开辟了一个新的内存空间,和数据源没有关系了。
用列表实现
其他操作
计数,count(),查找元素出现的次数
反转,reverse()
排序,sort(), 按照ascii码值排序
li = [["a","b","c"],["c","b","a"],["b","c","a"]]
print(li)
li.reverse()
print(li)
li.sort()
print(li)
li.sort(reverse=True)
print(li)
# [['a', 'b', 'c'], ['c', 'b', 'a'], ['b', 'c', 'a']]
# [['b', 'c', 'a'], ['c', 'b', 'a'], ['a', 'b', 'c']]
# [['a', 'b', 'c'], ['b', 'c', 'a'], ['c', 'b', 'a']]
# [['c', 'b', 'a'], ['b', 'c', 'a'], ['a', 'b', 'c']]
用来生成列表得到特定语法形式
格式:
[表达式 for 迭代元素 in 可迭代对象]
实现原理:
li = []
for x in range(10):
li.append(x)
print(li)
print([x for x in range(10) if x%2==0])
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# [0, 2, 4, 6, 8]
sr = "城市学院"
print([ord(x) for x in sr])
# [22478, 24066, 23398, 38498]
y =12
x = y + 1 if y>10 else y-1
print(x)
[exp for iter_val in iter if if_condtion]
li = []
for iter_val in iter:
if if_condtion:
li.append(iter_val)
print(li)
name = ["Tom","Jack","Lucy"]
subject = ["Python","jave","C","C++"]
li = []
for i in name:
for j in subject:
li.append([i,j])
print(li)
print([[i,j]for i in name for j in subject])# 列表生成式 嵌套式
有序
可重复
不可更改
符号使用()
定义:由常量元素组成的数组,也就是组成的元素不可以修改。其内部的元素可以是不同数据类型
元组的创建
tp1 = (1,2,"a","b",["aa","bb","cc","dd"])
tp2 = (3,4,"c","d")
print(tp1+tp2) # 拼接
print(tp1*2) # 重复
print(tp1[2],tp2[:4:1]) # 索引、切片
#(1, 2, 'a', 'b', ['aa', 'bb', 'cc', 'dd'], 3, 4, 'c', 'd')
# (1, 2, 'a', 'b', ['aa', 'bb', 'cc', 'dd'], 1, 2, 'a', 'b', ['aa', 'bb', 'cc', 'dd'])
# a (3, 4, 'c', 'd')
索引查
切片查
index()
tp = (1,2,"a","b",["aa","bb","cc","dd"])
print(tp.index("a"))
tp = (1,2,"a","b",["aa","bb","cc","dd"])
for i in tp :
print(i)
for i in range(len(tp)):
print(tp[i],end=" ")
for i in enumerate(tp[:3]):
print(i)