文章目录
输入输出
输入 input()
input(prompt=None, /)
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
输出 print()
-
print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
-
Prints the values to a stream, or to sys.stdout by default.
-
Optional keyword arguments:
- file: a file-like object (stream); defaults to the current sys.stdout.
- sep: string inserted between values, default a space.
- end: string appended after the last value, default a newline.
- flush: whether to forcibly flush the stream.
标准输入输出, 行读入
python3 中 raw_input() 已废弃
# 1.输入多行,a是一个多行字符串
import sys
a = sys.stdin.read()
print(a)
# 2.输入多行,a是一个列表,列表中每一个元素对应字符串的一行,行末的\n也还在
import sys
a = sys.stdin.readlines()
print(a)
# 或者
import sys
for line in sys.stdin.readlines():
print(line.strip())
# 3.输入单行,a直接就是那一行字符串
import sys
a = sys.stdin.readline()
print(a)
基础语法
六个标准数据类型
- 不可变数据:
- Number(数字)
- String(字符串)
- Tuple(元组)
- 可变数据:
- List(列表)
- Set(集合)
- Dictionary(字典)
Number ( 数字 )
Number 分为 int, float, complex, bool
>>> a, b, c, d = 20, 5.5, True, 4+3j
>>> print(type(a), type(b), type(c), type(d))
<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>
数学函数
函数 | 返回值 ( 描述 ) |
---|---|
abs(x) | 返回数字的绝对值,如abs(-10) 返回 10 |
ceil(x) | 返回数字的上入整数,如math.ceil(4.1) 返回 5 |
cmp(x, y) |
如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1。 Python 3 已废弃,使用 (x>y)-(x<y) 替换。 |
exp(x) | 返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045 |
fabs(x) | 返回数字的绝对值,如math.fabs(-10) 返回10.0 |
floor(x) | 返回数字的下舍整数,如math.floor(4.9)返回 4 |
log(x) | 如math.log(math.e)返回1.0,math.log(100,10)返回2.0 |
log10(x) | 返回以10为基数的x的对数,如math.log10(100)返回 2.0 |
max(x1, x2,...) | 返回给定参数的最大值,参数可以为序列。 |
min(x1, x2,...) | 返回给定参数的最小值,参数可以为序列。 |
modf(x) | 返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。 |
pow(x, y) | x**y 运算后的值。 |
round(x [,n]) | 返回浮点数 x 的四舍五入值,如给出 n 值,则代表舍入到小数点后的位数。 其实准确的说是保留值将保留到离上一位更近的一端。 |
sqrt(x) | 返回数字x的平方根。 |
随机数函数
函数 | 描述 |
---|---|
choice(seq) | 从序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中随机挑选一个整数。 |
randrange ([start,] stop [,step]) | 从指定范围内,按指定基数递增的集合中获取一个随机数,基数默认值为 1 |
random() | 随机生成下一个实数,它在[0,1)范围内。 |
seed([x]) | 改变随机数生成器的种子seed。如果你不了解其原理,你不必特别去设定seed,Python会帮你选择seed。 |
shuffle(lst) | 将序列的所有元素随机排序 |
uniform(x, y) | 随机生成下一个实数,它在[x,y]范围内。 |
三角函数
函数 | 描述 |
---|---|
acos(x) | 返回x的反余弦弧度值。 |
asin(x) | 返回x的反正弦弧度值。 |
atan(x) | 返回x的反正切弧度值。 |
atan2(y, x) | 返回给定的 X 及 Y 坐标值的反正切值。 |
cos(x) | 返回x的弧度的余弦值。 |
hypot(x, y) | 返回欧几里德范数 sqrt(x*x + y*y)。 |
sin(x) | 返回的x弧度的正弦值。 |
tan(x) | 返回x弧度的正切值。 |
degrees(x) | 将弧度转换为角度,如degrees(math.pi/2) , 返回90.0 |
radians(x) | 将角度转换为弧度 |
数学常量
常量 | 描述 |
---|---|
pi | 数学常量 pi(圆周率,一般以π来表示) |
e | 数学常量 e,e即自然常数(自然常数)。 |
String ( 字符串 )
# string 测试
s = "PythonStudy"
print( s )
print( s[0:-1] )
print( s[0] )
print( s[2:5] )
print( s*2 )
print( s[::2] ) # 第二个冒号之后是步长
print( s+"_Test" )
print( "\n__________________转义test\n" )
print( "Hello \nPython!" )
print( r"Hello \nPython" )
print( "\n__________________转义test\n" )
>>> # string 测试
... s = "PythonStudy"
>>> print( s )
PythonStudy
>>> print( s[0:-1] ) # 输出第一个到倒数第二个的所有字符
PythonStud
>>> print( s[0] )
P
>>> print( s[2:5] ) # 输出从第三个开始到第五个的字符
tho
>>> print( s*2 ) # 输出字符串两次
PythonStudyPythonStudy
>>> print( s[::2] )
PtoSuy
>>> print( s+"_Test" )
PythonStudy_Test
>>> print( "\n__________________转义test\n" )
__________________转义test
>>> print( "Hello \nPython!" ) # 使用反斜杠(\)+n转义特殊字符
Hello
Python!
>>> print( r"Hello \nPython" ) # 在字符串前面添加一个 r,表示原始字符串,不会发生转义
Hello \nPython