-
L=['Alice','Bob','Candy','David', 'Ellena']
L.pop(2)
print(L)
L.pop(2)
print(L)
M=['Alice','Bob','Candy','David', 'Ellena']
del M[2:4]
print(M)
C=['Alice','Bob','Candy','David', 'Ellena']
C.remove('Candy')
C.remove('David')
print(C)
查看全部 -
# coding: utf-8
s='"这是一句中英文混合的Python字符串:\nHello World!"'
print(s)
查看全部 -
>>> python
查看全部 -
网络爬虫
数据分析
人工智能
查看全部 -
Python3
查看全部 -
# 因为包含在r'........'这个里面的都会视为字符类型输出,转义符号等一些连接符都会失效。
查看全部 -
Life is Short , You need Python.
查看全部 -
# \n 表示换行
\t 表示一个制表符
\\ 表示 \ 字符本身查看全部 -
# 0,空字符串,None在Python中是False # 短路计算,非真即假,非假即真,NOT优先级最高
查看全部 -
学习吧抓紧时间了123456查看全部
-
若set元素中每个元素都需进行大小写切换时,可通过使用for循环将set元素中每个元素进行赋值,后再通过切换大小写的方法将元素进行修改后才可输出切换大小写后的结果。
在例子中,可通过lower()方法将元素切换至小写后进行输出,公式如下:
names=['Alice', 'Bob', 'Candy', 'David', 'Ellena']#list列表 name_set=set(names)#切换成set元素 print(name_set)#输出set(['Bob', 'Ellena', 'Alice', 'Candy', 'David']) new_names=[ ] #新建一个空的list for i in name_set: #将set元素一次赋值给i n=i.lower( ) #通过lower()方法将i的元素切换成小写模式,n即表示小写的i的元素 new_name.append(n)#使用append()方法将n表示的元素追加到空list m=set(new_names)#后再通过set()方法将list元素切换成set元素 print(m) #输出set元素为小写set(['bob', 'ellena', 'alice', 'candy', 'david'])
查看全部 -
Python的字符串format
字符串是Python程序重要的数据类型,到目前为止,我们输出的字符串的内容都是固定的,但有时候通过字符串输出的内容不是固定的,这个时候需要使用format来处理字符串,输出不固定的内容。
字符串format由两个部分组成,字符串模板和模板数据内容组成,通过大括号{},就可以把模板数据内容嵌到字符串模板对应的位置。# 字符串模板 template = 'Hello {}' # 模板数据内容 world = 'World' result = template.format(world) print(result) # ==> Hello World
如果模板中{}比较多,则容易错乱,那么在format的时候也可以指定模板数据内容的顺序。
# 指定顺序 template = 'Hello {0}, Hello {1}, Hello {2}, Hello {3}.' result = template.format('World', 'China', 'Beijing', 'imooc') print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc. # 调整顺序 template = 'Hello {3}, Hello {2}, Hello {1}, Hello {0}.' result = template.format('World', 'China', 'Beijing', 'imooc') print(result) # ==> Hello imooc, Hello Beijing, Hello China, Hello World.
除了使用顺序,还可以指定对应的名字,使得在format过程更加清晰。
# 指定{}的名字w,c,b,i template = 'Hello {w}, Hello {c}, Hello {b}, Hello {i}.' world = 'World' china = 'China' beijing = 'Beijing' imooc = 'imooc' # 指定名字对应的模板数据内容 result = template.format(w = world, c = china, b = beijing, i = imooc) print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.
任务
请使用两种format的方式打印字符串Life is short, you need Python。
# Enter a code
# coding=utf-8
# 记忆方法:模具,内容,合并了的模具
template = '林 {}'
# 模具
a = '书豪'
# 实体
result = template.format(a)
print(result)
# result由两个组成,字符串模板,模板数据内容(合并了的模板)
# format括号里边需要引号,除非上面已经定义
print('指定顺序')
template = 'hello {0},hello {1},hello {2},hello {3}'
result = template.format('world', 'china', 'beijing', 'imooc')
print(result)
# template='林[0],林[1],林[2],林[3].'
# result=template.format('书豪','国豪','俊杰','妙可')
# print(result)
print('指定顺序中变量设置为中文运行不了,什么原因呢')
# 发现原因了,原来是笔记本没有{}符号,换其他键盘可以打出
template = '林 {1},林 {2},林 {3},林 {0}.'
result = template.format('书豪', '国豪', '俊杰', '妙可')
print(result)
print('调整顺序')
template = 'hello {0},hello {1},hello {2},hello {3}.'
# template后面加不加点都可以
# 元素之间逗号后有没有空格都可以,一般建议有空格
result = template.format('beijing', 'shanghai', 'guangdong', 'hunan')
print(result)
# 1.等号两边需要空格
# 2.hello和数字之间需要空格
# 3.beijing的逗号后面需要空格
# 4.result变量前面需要空格
# 5.代码结束需要下一空行
# 6.#的后面需要空格
print('指定对应的名字')
template = 'hello {m}, hello {w},hello {h}, hello {y}.'
meixi = 'meixi'
wujiayu = 'wujiayu'
huangshang = 'huangshang'
yazi = 'yazi'
result = template.format(m=meixi, w=wujiayu, h=huangshang, y=yazi)
# 这行的等号两边不需要空格
print(result)
print('指定对应名字 测试')
template = '3 {w}, 0 {c}, 2 {b}, 1 {i}.'
c = 'www'
w = 'ccc'
b = 'bbb'
i = 'iii'
result = template.format(b=c, c=w, w=i, i=w)
print(result)
#template规定了3021顺序,{c}对应c=w再对应w = 'ccc',也就是跳了两次
# result后面是合体版本
# 模板数据内容有三条:
# 变量
# (如果有指定名字,则这行需要指定)
# result
# print
print('任务')
print('第一种方式,普通方式')
template = 'life is short,{}.'
a = 'you nide python'
result = template.format(a)
print(result)
print('第二种方式,指定顺序')
template = 'life {0} short,{1} nide {2}.'
result = template.format('is', 'you', 'python')
print(result)
print('第三种方式,调整顺序')
template = 'life {2} short,{0} nide {1}.'
result = template.format('you', 'python', 'is')
print(result)
print('第四种方法,指定名字')
template = 'life {i} short,{y} nide {p}.'
i = 'is'
y = 'you'
p = 'python'
result = template.format(i=i, y=y, p=p)
print(result)
print('第五种方法,自创最简')
template = 'life {i} short,{y} nide {p}.'
result = template.format(i='is', y='you', p='python')
print(result)
查看全部 -
L=[[1, 2, 3], [5, 3, 2], [7, 3, 2]]
for a in L:
s=(a[0]*a[1]+a[1]*a[2]+a[0]*a[2])*2
print(s)
查看全部 -
合法变量名: num查看全部
-
L = [[1, 2, 3], [5, 3, 2], [7, 3, 2]]
for i in range(0,3):
s = 2*(L[i][0] * L[i][1] + L[i][0]*L[i][2] + L[i][1]*L[i][2])
print(s)查看全部
举报