-
见上一个代码,运行结果如下:
3
(3,)
ab
(1,)
(1, 2, 3, 4, 5)查看全部 -
T = ((1+2), ((1+2),), ('a'+'b'), (1, ), (1,2,3,4,5))
for i in T:
print(i)
查看全部 -
包含 0 个元素的 tuple,也就是空tuple,直接用()表示:
这个tuple里的第一个元素是(1+2)它有一个元素吗?就是3?
根据“()既可以表示tuple,又可以作为括号表示运算时的优先级,结果(1+2)被Python解释器计算出结果 3,导致我们得到的不是tuple,而是整数 3。所以我认为这里面的东东就不是元组了。
这个tuple里的第一个元素是(1+2),这个比第一个多了一个逗号,我感觉像是元组。
第三是字串符没有逗号,不是
第四个有逗号是
第五个肯定是
所以应该是三个吧
查看全部 -
这个题我审错了,元素很简单,就是,逗号分割开的就是一个,这个题问的是元素里有几个Tuple
查看全部 -
感觉自己没有理解“元素”的概念,我竟然搞了14个出来
查看全部 -
L = ['Alice', 'Bob', 'Candy', 'David', 'Ellena']
L.append('Zero')
L.insert(5, 'Phoebe')
L.insert(-2, 'Gen')
print(L)
我的答案是不是更好呢?
查看全部 -
注意,append()方法总是将元素添加到list的尾部。
insert()方法和append()方法不一样,insert()方法需要两个参数,分别是需要插入的位置,以及需要插入的元素。
查看全部 -
names = ['Alice', 'Bob', 'David', 'Ellena']
sub_names = names[0:2]
print(sub_names)#['Alice', 'Bob']
查看全部 -
字符串format由两个部分组成,字符串模板和模板数据内容组成,通过大括号{},就可以把模板数据内容嵌到字符串模板对应的位置。
# 字符串模板
template = 'Hello {}'
# 模板数据内容
world = 'World'
result = template.format(world)
print(result) # ==> Hello World
请使用两种format的方式打印字符串Life is short, you need Python。
1. print('Life is short, you need {}'.format('Python'))
2.print('Life is short, you need {launguage}'.format( launguage = 'Python'))
3.无参数
s = "Hello, {}!"
print(s.format("world"))# 输出:Hello, world!
1. 位置参数
s = "I am {} years old and my name is {}."
print(s.format(25, "John"))# 输出:I am 25 years old and my name is John.
4. 关键字参数
s = "My name is {name} and I am {age} years old."
print(s.format(name="John", age=25)) # 输出:My name is Johnand I am 25 years old.
5. 混合使用位置参数和关键字参数
s = "My name is {name} and I am {0} years old."
print(s.format(25, name="John")) # 输出:My name is John and I am 25 years old.如果模板中{}比较多,则容易错乱,那么在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的时候也可以指定模板数据内容的顺序。
除了使用顺序,还可以指定对应的名字,使得在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.
使用字典作为关键字参数
person = {"name": "John", "age": 25}
s = "My name is {name} and I am {age} years old."
print(s.format(**person))# 输出:My name is John and I am 25 years old.
使用索引
s = "The {2} of {0} is {1:.2f}"
print(s.format("pi", 3.14159, "circle"))# 输出:The circle of pi is 3.14
查看全部 -
while 循环的基本结构
在 Python 中,while 循环的结构简单明了。
它开始于关键字 while,紧接着是一个条件表达式,然后是一个冒号。在冒号后面,缩进的代码块就是循环体,只要条件为真,这段代码就会反复执行。
查看全部 -
按照位置取字符串的方式使用中括号访问,这个时候可以把字符串看作是一个列表(一种新的数据类型
访问,这个时候可以把字符串看作是一个列表(一种新的数据类型
查看全部 -
while 循环的核心有两个部分:条件和循环体。条件是一个布尔表达式(即结果为真或假的表达式),而循环体则是在条件为真时反复执行的代码块。
查看全部 -
age = 16
if age >= 18:
print('adult')
elif age <3:
print('baby')
elif 3<= age <=6:
print('kid')
else:
print('teenager')
查看全部 -
请运行如下代码,并解释打印的结果:
a = 'python'
print('hello,', a or 'world')#hello, python
b = ''
print('hello,', b or 'world')#hello,
是否正确呢,我的答案是根据短路计算的原理得来的。
我错了!
正确答案是:
('hello,', 'python')
('hello,', 'world')官方给出解释:解释:通过 or 运算,可以把空字符串"变成"默认字符串,而非空字符串保持不变。
WHY?
变量b被赋予了空值,因为Python把0、空字符串和None看成False,其他数值和非空字符串都看成True,所以or那段语句要选后面的非空字串符里的内容。
另外我的一个小错,是print输出内容到底是什么呢?于是我将()去掉
a = 'python'
print'hello,', a or 'world'
b = ''
print'hello,', b or 'world'
此时输出了如下内容:
hello, python
hello, world也就是说,print有括号和无括号都可以输出,但是输出的内容不一样
查看全部 -
list容器的概述
查看全部
举报