-
# coding: utf-8
result=r'''这是一句中英文混合的Python字符串:
\Hello World\ '''
print(result)
查看全部 -
# Enter a code
template='lif is {a} , you need {b}'
c='short'
d='Python'
result = template.format(a=c,b=d)
print(result)
template2='lif is {0} , you need {1}'
result2=template2.format('long','happy')
print(result2)
查看全部 -
# Enter a code
a=r'\(~_~)/'
print(a)
b='''line1
line2\nline3'''
print(b)
c='''hello TOM
Hello JIMY\nHELLO 'lisa'''
print(c)
d=r'''"To be, or not to be": that is the question.
Whether it's nobler in the mind to suffer.'''
print(d)
print('\"To be, or not to be\": that is the question.\nWhether it\'s nobler in the mind to suffer.')
查看全部 -
print(r'''\"To be, or not to be\": that is the question.\nWhether it\'s nobler in the mind to suffer.''')
查看全部 -
print('special string: \', \", \\, \\\\, \\n, \\t')
查看全部 -
\n表示换行
\t 表示一个制表符
\\表示 \ 字符本身如果字符串既包含'又包含"怎么办?
这个时候,就需要对字符串中的某些特殊字符进行“转义”,Python字符串用\进行转义。
要表示字符串Bob said "I'm OK"
由于'和"会引起歧义,因此,我们在它前面插入一个\表示这是一个普通字符,不代表字符串的起始,因此,这个字符串又可以表示为'Bob said \"I\'m OK\".'
注意:转义字符 \不计入字符串的内容中。
查看全部 -
与运算
只有两个布尔值都为 True 时,计算结果才为 True。
True and True # ==> True
True and False # ==> False
False and True # ==> False
False and False # ==> False或运算
只要有一个布尔值为 True,计算结果就是 True。
True or True # ==> True
True or False # ==> True
False or True # ==> True
False or False # ==> False非运算
把True变为False,或者把False变为True:
not True # ==> False
not False # ==> True查看全部 -
print(imma)查看全部
-
理论上,所有的递归函数都可以写成循环的方式,但循环的逻辑不如递归清晰。查看全部
-
这里有几个需要注意的地方:
可以看到print('抱歉,考试不及格')这行代码明显比上一行代码缩进了,这是因为这行代码是if判断的一个子分支,因此需要缩进,在Python规范中,一般使用4个空格作为缩进
在if语句的最后,有一个冒号:,这是条件分支判断的格式,在最后加入冒号:,表示接下来是分支代码块查看全部 -
round函数(num,2)查看全部
-
print(type(3.1415926))#<class'float'>
print(pyte('Learn Python in important.'))#<class 'str'>
print(pyte(100))#<class 'int'>
print(pyte(0b1101))#<class 'int'>
print(0b1101)#输出 13,验证二进制转十进制查看全部 -
地板除
10//3 # ==> 3
查看全部 -
else if 可以用elif代替更简单方便。
写条件的时候要注意逻辑清晰。
查看全部 -
打印代码需要缩进
if语句后必须要有“:”
查看全部 -
s[0:4] s中的第一个字符到第五个字符,不包括第五个字符
s[2:6] s中的第三个字符到第七个字符,不包括第七个字符
查看全部 -
字符串索引
s[i]
字符串切片
s[i]左闭右开
查看全部 -
字符串索引 s[i]
字符串切片 s[i:j]左闭右开
查看全部 -
字符串索引
s[i]
字符串切片
s[0:i]左闭右开
查看全部 -
字符串索引
s[i]
字符串切片
s[i:j]左闭右开
查看全部 -
字符串索引
s{i}
字符串切片
s{i:j} 左闭右开
查看全部 -
s[i]左闭右开
从0开始切片,最后一个字符不取
查看全部 -
字符串的索引
s[i]
字符串的切片
s[i:j]左闭右开
查看全部 -
s = 'ABCDEFGHIJK'
abcd = s[0:4] # 取字符串s中的第一个字符到第五个字符,不包括第五个字符
print(abcd) # ==> ABCD
cdef = s[2:6] # 取字符串s中的第三个字符到第七个字符,不包括第七个字符
print(cdef) # ==> CDEF查看全部
举报