(一)数字整形和浮点型
- 整数:int (python没有数字范围short long)
- 浮点型:float(python没有单精度float和双精度double之分)
>>> type(1+1.0)
<class 'float'>
>>> type(4/2)
<class 'float'>
>>> type(24//2)
<class 'int'>
>>> 4/2
2.0
>>> 4//2
2
>>>
>>> 1//2
0
(二)计算机基础知识 10、2、8、16进制的表示和转换
>>> 0b10
2
>>> 0o10
8
>>> 0x10
16
>>> bin(0b100)
'0b100'
>>> oct(0o100)
'0o100'
>>> int(100)
100
>>> hex(0xf)
'0xf'
(三) 数字布尔类型和复数
- bool 布尔类型:表示真True和假False
- complex 复数
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> int(False)
0
>>> int(True)
1
>>> bool(0) # 0 表示false
False
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(2)
True
>>> bool(0x0)
False
>>> bool("")
False
>>> bool([])
False
>>> bool({})
False
>>> bool(())
False
>>> bool(None)
False
>>> 36j
36j
>>> type(36j)
<class 'complex'>
(四) 字符串单引号和双引号
- str 字符串
- 如何表示字符串:单引号、双引号、三引号
>>> type('2')
<class 'str'>
>>> type(1)
<class 'int'>
- 为什么需要这么多引号,如果我的it‘s 需要一个引号,外部则需要双引号
>>> "it's good"
"it's good"
>>> 'it\'s good'
"it's good"
(五) 多行字符串
>>> """
... ni hao
... hello
... hi"""
'\nni hao\nhello\nhi'
>>> """ nihao \n hi"""
' nihao \n hi'
>>> print(""" nihao \n hi""")
nihao
hi
>>> 'ni\
... hao'
'nihao'
(六) 转义字符
\n 换行,\t 横向制表符, \r 回车
\' 单引号
>>> print("hello \n world")
hello
world
>>> print("hello \\n world")
hello \n world
(七) 原始字符串
- 输出文件路径,可以使用\, 或者r(使用r值后,字符串变成了原始字符串)
>>> print("c:\north\no.py")
c:
orth
o.py
>>> print("c:\\north\\no.py")
c:\north\no.py
>>> print(r"c:\\north\\no.py")
c:\\north\\no.py
(八) 字符串运算
>>> "hello" + "world"
'helloworld'
>>> "hello"*2
'hellohello'
- 获取字符串中的字符,使用[],序号从0开始,右边从-1开始(表示从末尾开始查找的第一个)
>>> "hello"[0]
'h'
>>> "hello"[1]
'e'
>>> "hello"[-1]
'o'
>>> "hello world"[0:5]
'hello'
>>> "hello world"[0:-3]
'hello wo'