>>> 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(value,value_2,...,value_n,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)
1 2 3 [4, 5, 6]
>>> print(a,b,c,d,sep = "-")
1-2-3-[4, 5, 6]
>>> print(a,b,c,d,sep = "-",end = " ")
1-2-3-[4, 5, 6] >>>
格式 | 描述 |
---|---|
%d | 有符号的整数 |
%s | 字符串 |
%c | 字符及ASCII码 |
%o | 无符号八进制整数 |
%x,%X | 无符号十六进制整数 |
%e,%E | 科学计数法,返回浮点数 |
%f | 浮点数 |
%格式化字符串 用%匹配参数,注意个数一一对应。
“%d%%” % 100 —>100%
print("%d" %668)
name = "Jerry"
age = 18
print("His name is %s,his age is %d" %(name,age))
print("%s%d" %("%",100))
print("Name:{},age:{}".format('Tom',18))
print("Name:{},age:{},{address}".format('Tom',18,address = "hangzhou"))
print("第一个元素是:{0[0]},第二个元素是:{0[1]},第三个元素是:{0[2]};\
第四个元素是:{1[0]},第五个元素是:{0[2]}".format(('www','google','com'),("baidu")))
列表是用方括号括起来的[],每个元素以逗号分隔;
可以储存不同的数据类型数据
数组
特点:
li = []
print(type(li))
创建多元素列表
li = [1,2,3,"abcd","City","College",["I","Love","Python"]]
强转
str = 'abcd'
li = list(str)
print(li,type(li))
(1)拼接
li1 = ["I"]
li2 = ["Love","Python"]
print(li1 + li2)
(2)重复
li2 = ["Love","Python"]
print(li2 * 3)
(3)索引(偏移)[] ,切片[:],[::]
li = ['C','i','t','y',"city",'college','zhengjiang']
print(len(li))
print((li[0],li[-7]))
print(li[:5])
(4)常见操作
增删改查
# 1.增
- append()
- extend() #取出元素再添加
- insert() #按照索引添加
li = ["City","College"]
print((id(li)))
li1 = ["a","b","c"]
li.append(li1)
print(li)
print(id(li))
li = ["City","College"]
li1 = ["a","b","c"]
li.extend(li1)
print(li)
li = ["City","College"]
li1 = ["a","b","c"]
li.insert(1,li1)
print(li)
# 2.改
li = ["City","College","a","b","c"]
li1 = ["a","b","c"]
li[4] = "6666"
li[2:4] = ["x","y","z"]
print(li)
#3.删除
- pop() #如果不指定索引,默认删除最后一个元素
li = ['a','b','c','d','e']
print(li.pop(2))
print(li)
- remove() #移除第一次遇到的指定元素
li = ['a','b','c','d','e','city','e']
print(li.remove("e"))
print(li)
- del()
li = ['a','b','c','d','e','city','e']
del li[5]
print(li)
- clear() #清空列表
li = ['a','b','c','d','e','city','e']
li.clear()
print(li)
# 4.查 --->索引,切片
遍历
(1)元素遍历
li = ['a','b','c','d','e','city','e']
for char in li:
print(char)
(2)索引遍历
li = ['a','b','c','d','e','city','e']
for i in range(len(li)):
print(li[i])
(3)枚举遍历
- enumerate() #对于一个可迭代的/可遍历的对象(如列表,字符串等),enumerate()将其组成一个索引序列,利用它我们可以获得索引和值
li = ['a','b','c','d','e','city','e']
for i in enumerate(li,2):
print(i)
堆和栈,用列表实现
list = ["apple", "banana", "grape"]
li = []
for value in list:
print("压入元素:",value)
li.append(value)
print(li)
for value in li:
print("弹出元素:", list.pop())
print(list)
压入元素: apple
['apple']
压入元素: banana
['apple', 'banana']
压入元素: grape
['apple', 'banana', 'grape']
弹出元素: grape
['apple', 'banana']
弹出元素: banana
['apple']
弹出元素: apple
[]
计数
- count()
li = ['a','b','c','d','e','city','e']
print(li.count('e'))
反转
- reverse()
li = ['a','b','c','d','e','city','e']
li.reverse()
print(li)
排序
- sort() #按照ASCII码值排序
li = ['a','b','c','d','e','city','e']
li.sort()
print(li)
- sort(reverse = True) #逆向排序
li = ['a','b','c','d','e','city','e']
li.sort(reverse=True)
print(li)
练习:
(1)input()函数实现往空列表中添加元素,当输入‘q’的时候,结束输入并返回列表
li = []
while True:
str = input("请输入要添加的元素:")
if str == "q":
break
else:
li.append(str)
print(li)
(2)随机生成30个0-10的随机整数,并统计每个数出现的次数,返回的结果形式为[[0, 3], [1, 4], … , [10, 5]]
import random
li = []
li1 = []
for i in range(30):
a = random.randint(0,10)
li.append(a)
for j in range(0,11):
count = li.count(j)
li1.append([j,count])
print(li1)
赋值
赋值是对对象的引用
la = [1,2,3,4,5,'a',['b1','b2']]
lb = la
la[5] = "aaa"
print(la)
print(lb)
print(lb is la)
深浅拷贝
深浅拷贝会创建新对象,内容是原对象的引用
三种实现方法:
切片操作[:](浅拷贝)
工厂函数list(li)
copy模块内的copy函数(浅拷贝)
la = [1,2,3,4,5,'a',['b1','b2']]
lb2 = la[:]
print(id(la),id(lb2))
16197112 16196312
import copy
lb_copy = copy.copy(la)
print(id(lb_copy),id(la))
44180104 44180904
la.append("test") #添加原子类型,不会影响lb_copy
print(la)
print(lb_copy)
[1, 2, 3, 4, 5, 'a', ['b1', 'b2'], 'test']
[1, 2, 3, 4, 5, 'a', ['b1', 'b2']]
import copy
la = [1,2,3,4,5,'a',['b1','b2']]
lb_copy = copy.copy(la)
la[6][0] = "bbb"
print(la)
print(lb_copy)
[1, 2, 3, 4, 5, 'a', ['bbb', 'b2']]
[1, 2, 3, 4, 5, 'a', ['bbb', 'b2']]
#la与lb_deepcopy均会发生变化
#浅拷贝相当于只拷贝一层,修改外部元素,会修改引用,让他们指向另一个位置;修改嵌套列表的元素,列表地址没有发生变化,指向的还是同一个位置
深拷贝
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))
#id会发生变化
[1, 2, 3, 4, 5, 'a', ['b1', 'b2']] 46474784
[1, 2, 3, 4, 5, 'a', ['b1', 'b2']] 46474024
la[6][1] = ["aaa"]
print(la)
print(lb_deepcopy)
[1, 2, 3, 4, 5, 'a', ['b1', ['aaa']]]
[1, 2, 3, 4, 5, 'a', ['b1', 'b2']]
#此时,lb_deepcopy中不发生变化
#深拷贝拷贝对象所有元素,包括多层嵌套的元素。因此,是开辟了一个新的内存存储区,和数据源没有关系了
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]
用来生成列表的特定的语法形式
格式:
[表达式 for 迭代元素 in 可迭代对象]
实现原理:
str = "城市学院"
print([ord(x) for x in str])
[22478, 24066, 23398, 38498]
y = 12
x = y+1 if y > 10 else y-1
print(x)
13
[exp for iter_val in iter if if_condtion]
等价于
li = []
for iter_val in iter:
if if_condtion:
li.append(iter_val)
print(li)
demo:
name = ["Tom","Jack","Lucy"]
subject = ["Python","Java","C","C++"]
print([[i,j] for i in name for j in subject])
[['Tom', 'Python'], ['Tom', 'Java'], ['Tom', 'C'], ['Tom', 'C++'], ['Jack', 'Python'], ['Jack', 'Java'], ['Jack', 'C'], ['Jack', 'C++'], ['Lucy', 'Python'], ['Lucy', 'Java'], ['Lucy', 'C'], ['Lucy', 'C++']]
特点
定义:
空元组的创建,不能添加元素
单元素元组的创建,需要在单元素后加逗号
tp = ("abc",)
tp = tuple()
tp = ("abc") #此时tp为字符串
多元素元组的创建,包含多种数据类型元组操作
(1)拼接
tp = ("abc","def","city","college")
tp1 = ('a','b','c')
print(tp + tp1)
('abc', 'def', 'city', 'college', 'a', 'b', 'c')
(2)重复
tp = ("abc","def","city","college")
tp1 = ('a','b','c')
print(tp * 3)
('abc', 'def', 'city', 'college', 'abc', 'def', 'city', 'college', 'abc', 'def', 'city', 'college')
(3)索引(偏移) 切片[:]
tp = ("abc","def","city","college")
tp1 = ('a','b','c')
print(tp[1])
print(tp[0:2])
print(tp[0:-1])
print(tp[::-1])
print(tp[-3:-1])
def
('abc', 'def')
('abc', 'def', 'city')
('college', 'city', 'def', 'abc')
('def', 'city')
(4)查找索引
tp = (1,2,3,'a','b',['aa','bb','cc','dd'])
print(tp.index('a'))
3
(5)删除
只能全部删除,不能删除某个元素
tp = (1,2,3,'a','b',['aa','bb','cc','dd'])
del tp
print(tp)
Traceback (most recent call last):
File "D:/python/PycharmProjects/chenhan/day03.py", line 155, in <module>
print(tp)
NameError: name 'tp' is not defined
>>> tp = (1,2,3,'a','b',['aa','bb','cc','dd'])
>>> id(tp)
41386872
>>> del tp
>>> tp = (1,2,3,'a','b',['aa','bb','cc','dd'])
>>> id(tp)
41386872
(6)最大值,最小值
tp = ('a','b','c')
print("a" in tp)
print(max(tp))
print(min(tp))
True
c
a
(7)遍历
元素遍历
tp = ('a','b','c')
for char in tp:
print(char)
索引遍历
tp = ('a','b','c')
for i in range(len(tp)):
print(tp[i])
枚举遍历
tp = ('a','b','c')
for char in enumerate(tp,1):
print(char)
(8)嵌套
tp = ((1,2,3),(4,5,6),(7,8,9))
print(tp[1][1])
5