-
按顺序访问list
L = ['Alice', 66, 'Bob', True, 'False', 100]
for item in L:
print(item)查看全部 -
list
列表(list)是一种有序的容器,放入list中的元素,将会按照一定顺序排列。使用中括号[]把需要放在容器里面的元素括起来,就定义了一个列表。
scores = [45, 60, 75, 86, 49, 100]
list可以同时放入任意类型的数据查看全部 -
Python之嵌套循环
s1 = 'ABC'
s2 = '123'
for x in s1:
for y in s2:
print(x + y)查看全部 -
Python之continue继续循环
我们可以控制循环继续下去,并跳过continue后面的逻辑
for ch in s:
if num < 10:
num = num + 1
continue # 当num < 10时,跳过后续循环代码,继续下一次循环
print(ch)
num = num + 1查看全部 -
Python之break跳出循环
num = 1
sum = 0
while True:
if num > 100:
break
sum = sum + num
num = num + 1
print(sum)查看全部 -
Python之while循环
while True:
print(1)查看全部 -
Python之for循环
s = 'ABCD'
for ch in s:
print(ch)查看全部 -
Python之if-elif-else语句
score = 59
if score < 60:
print('抱歉,考试不及格')
else:
if score >= 90:
print('恭喜你,拿到卓越的成绩')
else:
if score >= 80:
print('恭喜你,拿到优秀的成绩')
else:
print('恭喜你,考试及格')if score < 60:
print('抱歉,考试不及格')
elif score >= 90:
print('恭喜你,拿到卓越的成绩')
elif score >= 80:
print('恭喜你,拿到优秀的成绩')
else:
print('恭喜你,考试及格')查看全部 -
Python之if-else语句
if score < 60:
print('抱歉,考试不及格')
else:
print('恭喜你,考试及格')查看全部 -
Python之if语句
if score < 60:
print('抱歉,考试不及格')查看全部 -
Python的字符串切片
我们可以使用位置的方式取出字符串中特定位置的字符,按照位置取字符串的方式使用中括号[]访问
s = 'ABC'
a = s[0] # 第一个
b = s[1] # 第二个
c = s[2] # 第三个
我们会想获取字符串的一部分(子串),这个时候我们采取切片的方式获取,切片需要在中括号[]中填入两个数字,中间用冒号分开,表示子串的开始位置和结束位置,并且这是半闭半开区间,不包括最后的位置ab = s[0:2] # 取字符串s中的第一个字符到第三个字符,不包括第三个字符
查看全部 -
python3的编码
默认使用UTF-8 Unicode来进行编码,不用像python2一样处理
查看全部 -
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.查看全部 -
Python中raw字符串与多行字符串
如果一个字符串包含很多需要转义的字符,对每一个字符都进行转义会很麻烦。为了避免这种情况,我们可以在字符串前面加个前缀r,表示这是一个 raw 字符串,里面的字符就不需要转义了,例如:
r'\(~_~)/ \(~_~)/'
但是r'...'表示法不能表示多行字符串,也不能表示包含'和 "的字符串。
如果要表示多行字符串,可以用'''...'''表示:
'''Line 1
Line 2
Line 3'''上面这个字符串的表示方法和下面的是完全一样的:
'Line 1\nLine 2\nLine 3'
还可以在多行字符串前面添加r,把这个多行字符串也变成一个raw字符串:
r'''Python is created by "Guido".
It is free and easy to learn.
Let's start learn Python in imooc!'''查看全部 -
Python的字符串
字符串可以用' '或者" "括起来表示。
如果字符串本身包含'可以用" "括起来表示
如果字符串包含",我们就可以用' '括起来表示
如果字符串既包含'又包含",就需要对字符串中的某些特殊字符进行“转义”,Python字符串用\进行转义
常用的转义字符还有:
\n表示换行
\t 表示一个制表符
\\表示 \ 字符本身查看全部
举报