-
\n表示换行
\t 表示一个制表符
\\表示 \ 字符本身查看全部 -
not计算的优先级是高于and和or的
Python把0、空字符串和None看成False,其他数值和非空字符串都看成True
查看全部 -
变量名由大小写英文字母、数字和下划线_组成
变量不能用数字开头
变量尽量不要和Python关键字重合(比如前面学习过的:and、or、not,否则可能导致Python原有关键字发挥不出作用)
查看全部 -
在index.py中,使用函数可以直接使用,不需要打出“=”
查看全部 -
".+代码“后面的代码必须是函数
"_+代码"其实就是给变量名
查看全部 -
所谓“set()没有顺序”,是指无论原本的set()顺序是什么,打出来后的顺序都是一样的。
查看全部 -
Python的字符串切片
s = 'ABC'
a = s[0] # 第一个
b = s[1] # 第二个
c = s[2] # 第三个
print(a) # ==> A
print(b) # ==> B
print(c) # ==> C
s = 'ABCDEFGHIJK'
abcd = s[0:4]
# 取字符串s中的第一个字符到第五个字符,不包括第五个字符
print(abcd) # ==> ABCD
cdef = s[2:6]
# 取字符串s中的第三个字符到第七个字符,不包括第七个字符
print(cdef) # ==> CDEF查看全部 -
Python的字符串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由两个部分组成,分别是字符串模板和模板数据内容组成,通过大括号{},可以把模板数据内容镶嵌到字符串模板的相应位置
# 字符串模板
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.
查看全部 -
a = 'python'
print('hello,', a or 'world')
b = ''
print('hello,', b or 'world')
and与运算
如果a是错误的(False)的,根据或运算法则,整个结果必定为False,所以返a(and法则,必须两个都为true,才会返回true,其中一个不为true,就为false)
如果a是正确的(true)的,根据或运算法则,整个结果必定取决于b,所以选b
or或运算
如果a是正确的,根据或运算法则,整个结果为a
如果a是错误的,根据或运算法则,整个结果为b
查看全部 -
word = 'Life is {b1}, you need {a1}.'
acc = 'short'
bcc = 'python'
result = word.format(b1 = acc, a1 = bcc)
print(result)
查看全部 -
r'...'表示法不能表示多行字符串,也不能表示包含'和 "的字符串
r'...'表示法不能表示多行字符串,也不能表示包含'和 "的字符串
查看全部 -
如果字符串既包含'又包含"怎么办?
这个时候,就需要对字符串中的某些特殊字符进行“转义”,Python字符串用\进行转义。
要表示字符串Bob said "I'm OK"
由于'和"会引起歧义,因此,我们在它前面插入一个\表示这是一个普通字符,不代表字符串的起始,因此,这个字符串又可以表示为'Bob said \"I\'m OK\".'
注意:转义字符 \不计入字符串的内容中。
查看全部 -
fotmat函数的形式
假设
Life is {0} , you need {1}
template.format('short','Python')
0 = short 1=Python
是下面一行内已经拥有了顺序,让上面那一行去输出,而不是上面那一行的数字决定下一行的顺序,是一种调用的关系,而不是一种定义的关系
查看全部 -
def func(P):
if isinstance(P, list):
sum = 0
for i in P:
sum+=i
if isinstance(P, tuple):
sum=1
for i in P:
sum=i*sum
return sum
L1=[1,2,3,4]
print(func(L1))
查看全部
举报