Python系列文章目录
第一章 python学习笔记(一) ——变量
第二章 python学习笔记(二) ——控制流工具
第三章 python学习笔记(三) ——数据结构
第四章 python学习笔记(四) ——模块
–
计算
1. 数字类型
- int
32位整数,具有无限精度。 - float
-32位浮点数,用C的double实现
-complex
复数包含实部和虚部,分别以一个浮点数表示。 - Decimal
decimal 模块旨在支持“无偏差,精确无舍入的十进制算术(有时称为定点数算术)和有舍入的浮点数算术”。 - Fraction
分数实例可以由一对整数,一个分数,或者一个字符串构建而成。
2. 数字运算±*/(可使用小括号)
note:除法
>>> 8 / 5 # division always returns a floating point number
1.6
>>>17 / 3 # classic division returns a float
5.666666666666667
>>>17 // 3 # floor division discards the fractional part
5
>>>17 % 3 # the % operator returns the remainder of the division
2
>>>5 * 3 + 2 # floored quotient * divisor + remainder
17
3.字符串
(1) 如果不希望前置 \ 的字符转义成特殊字符,可以使用 原始字符串,在引号前添加 r 即可:
>>> print('C:\some\name') # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name
(2) 索引的时候,左包括,右不包括。
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
(3) 序号索引不能溢出边界,切片索引可以溢出边界
>>> word[4:42]
'on'
>>> word[42:]''
(4) python字符串不能改,只能重新新建一个
>>> word[0]='J'
Traceback (most recent call last):
File"<stdin>", line1, in<module>TypeError:'str' object does not support item assignment
>>>'J'+word[1:]
'Jython'