-
迭代:通过for循环访问列表每一个元素的方式
查看全部 -
容器:列表list,元组tuple,字典dict,集合set
list:有序;使用中括号[]把需要放在容器里面的元素括起来
list可以同时放入任意类型的数据,内容可以直接打印
L = ['Alice', 66, 'Bob', True, 'False', 100]
查看全部 -
s = 'ABCD'
for ch in s:
print(ch)L = [75, 92, 59, 68, 99]
sum = 0.0
for x in L:
sum = sum + x
print(sum / 5查看全部 -
缩进表示分支
if语句后有一个冒号,表示进入分支;else,elif同理
查看全部 -
半开半闭区间
ab = s[0:2] # 取字符串s中的第一个字符到第三个字符,不包括第三个字
查看全部 -
字符串format
# 字符串模板
template = 'Hello {}'
# 模板数据内容
world = 'World'
result = template.format(world)
print(result) # ==> Hello World指定模板数据内容的顺序
# 指定顺序
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.指定对应的名字
# 指定{}的名字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.查看全部 -
转义字符:
\n表示换行
\t 表示一个制表符
\\表示 \ 字符本身Bob said "I'm OK"~'Bob said \"I\'m OK\".'
查看全部 -
Python把0、空字符串和None看成False,其他数值和非空字符串都看成True:
True and 0计算结果是0
继续计算0 or 99计算结果是 99not计算的优先级是高于and和or的
Python解释器在做布尔运算时,只要能提前确定计算结果,它就不会往后算了,直接返回结果
查看全部 -
整数和浮点数运算后 ,得到的结果不管小数点后是否有值,结果都变成浮点数
取模:%
地板除://(忽略小数部分)
round()函数保留小数点位数:
num = 10 / 3
print(num) # ==> 3.3333333333333335
# 使用round保留两位小数
round(num, 2) # ==> 3.33查看全部 -
合法变量:
1.变量名由大小写英文字母、数字和下划线组成
2.变量不能用数字开头
3.变量尽量不要和Python关键字重合
动态语言:
在Python里面,一个变量可以先后存储多种不同类型的数据
查看全部 -
浮点数:1.23x10^9~1.23e9, 0.00012~1.2e-5
整数运算永远是精确的,而浮点数运算则可能会有四舍五入的误差
布尔值:True False;可以用and、or和not运算
空值:None
查看全部 -
1.变量名由大小写英文字母、数字和下划线_组成
2.变量不能用数字开头
3.变量尽量不要和Python关键字重合(比如前面学习过的:and、or、not,否则可能导致Python原有关键字发挥不出作用)
查看全部 -
r'...'表示法不能表示多行字符串,也不能表示包含'和 "的字符串。
如果要表示多行字符串,可以用'''...'''表示:
查看全部 -
地板除//表示取整数
取模运算%表示取余数
round可以用来保留小数的数值和位数,用法如下:
# 使用round保留两位小数
round(num, 2) # ==> 3.33
查看全部 -
空值是Python里一个特殊的值,用None表示。
查看全部
举报