2019独角兽企业重金招聘Python工程师标准>>>
打印
打印hello,word的两种方式
>>> print('hello,word')
'hello,word'
>>> print('hello,' 'word')
'hello,word'
拼接字符串
>>> print (('hello,') + ('word'))
>>> a = 'hello,'
>>> b = 'word'
>>> a + b
'hello,word'
打印长字符串
>>> print('''带释意词汇(如:"/"\*.\n等)''')
带释意词汇(如:"/"\*.
等)
#发现除了\n没有转义其它的都转义了,如果想要\n转移前边加\转义符即可
打印原始字符串
>>> print(r'带释意词汇(如:"/"\*.\n等)')
带释意词汇(如:"/"\*.\n等)
#直接显示原始字符串,注意不能在原始字符结尾输入\反斜线,如果有需求可把\当做单字符处理
批量打印中文字符文章
# -*- coding: unicode -*-
#编码文件头
>>> print('''第一行
第二行
第三行
... ...
最后一行''')
打印布尔型运算
>>> a = 'python'
>>> print(('hello,'),a or ('world'))
>>> b = ''
>>> print(('hello,'),b or ('world'))
#因为变量a 被赋值为字符串'python',非空值,即print a结果为True,打印出python
#b被赋为空值,即Flase,所以打印结果为空,结合后边or'word'非运算,输出结果'word'
字符串格式化
%格式化操作
>>> format = "Hello, %s. %s enough for ya?"
>>> values = ('world','Hot')
>>> print(format % values)
Hello,world.Hot enough for ya?
等差数列
求第n项的数值
公式:n项值=首项+(项数-1)*公差
x1 = 1
d = 3
n = 100
x100 = x1 + ( n - 1 ) * d
print(x100)
求和
公式:(首项+末项)*项数/2
x1 = 1
d = 3
n = 100
x100 = x1 + (n - 1) * d
s = (x1 + x100) * n / 2
print(s)
求项数
公式:项数=(末项-首项)/公差+1
x1 = 1
d = 3
x100 = 298
n = ( x100 - x1 ) / d + 1
print(n)
获取用户输入内容
显示变量值
input ('请输入内容:>>>')
请输入内容:a
print(a)
幂运算
运算符**表示
>>> 2 ** 3
8
函数表示
>>> pow(2,3)
8
绝对值
>>> abs(-10)
10
浮点数取整
四舍五入
>>> round(1.5/2.0)
1
模块实现
舍去小数点后留整
>>> import math
>>> math.floor(32.9)
32
不满整数入整
>>> import math
>>> math.ceil(32.9)
33
平方根计算
正数平方根
>>> import math
>>> math.sqrt(9)
3.0
负数平方根(虚数)
>>> import cmath
>>> cmath.sqrt(-1)
1j