进制之间相互转换
二进制:0b表示二进制
0b11 => 3
八进制:0o表示八进制
0o11 => 9
十六进制:0x表示十六进制
0x11 =>17
把其他进制的数转成二进制:bin(10)
把其他进制的数转成八进制:oct(0b1111)
把其他进制的数转成十进制:int(0x7)
把其他进制的数转成十六进制:hex(8888)
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> bin(10) ## 其他进制转二进制
'0b1010'
>>> bin(0x7)
'0b111'
>>> int(0x7) ## 其他进制转十进制
7
>>> int(0b1111)
15
>>> oct(0b1111) ## 其他进制转八进制
'0o17'
>>> oct(0x777)
'0o3567'
>>> hex(8888) ## 其他进制转十六进制
'0x22b8'
>>>
Number:数字
整数:int
浮点数:float
类比Java的数据类型
整型:byte、short、int、long
浮点型:float(单精度)、double(双精度)
Python针对于数字就只分为整数和浮点数(小数),而浮点数也不存在精度的区分都是float型
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> type(1)
<class 'int'>
>>> type(-1)
<class 'int'>
>>> type(1.1)
<class 'float'>
>>> type(1.111111111)
<class 'float'>
>>>
整型与浮点型相加、相乘、相除等运算;整型与浮点型相加、减结果都会自动转换成浮点型
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> type(1+1.0)
<class 'float'>
>>> type(1-1.0)
<class 'float'>
>>> type(1*1.0)
<class 'float'>
>>> type(1/1) ## /除运算符,返回浮点型
<class 'float'>
>>> type(1//1) ## //整除运算符,返回整型;只保留整数部分
<class 'int'>
>>> type(1//1.0)
<class 'float'>
>>>
布尔型:bool 表示真假(True、False),属于数字
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> True
True
>>> False
False
>>> FALSE
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
FALSE
NameError: name 'FALSE' is not defined
>>> true
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
true
NameError: name 'true' is not defined
>>> type(True)
<class 'bool'>
>>> int(True)
1
>>> int(False)
0
>>> bool(1)
True
>>> bool(0)
False
>>>
只要是非零的数字表示bool真(True),为零则为bool假(False),但是字符型呢?由下可见:零、空值和None都可以表示bool假(False)
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> bool(2.2)
True
>>> bool(0.0)
False
>>> bool('aaa')
True
>>> bool('')
False
>>> bool([1,2,3])
True
>>> bool([])
False
>>> bool({1,1,1})
True
>>> bool({})
False
>>> bool(None)
False
>>>
str:字符串,由单引号(‘ ’)、双引号(" ")和三引号(‘‘‘ ’’’ | """ """)表示的,通常是成对出现的;有时单引号和双引号会组合使用。
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> "let's go"
"let's go"
>>> 'left"s go'
'left"s go'
>>> '''
hello world
hello world
hello world
'''
'\nhello world\nhello world\nhello world\n'
>>> """
hello world
hello world
hello world
"""
'\nhello world\nhello world\nhello world\n'
>>>
字符串的基本操作
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 'hello' + 'world'
'helloworld'
>>> 'hello' * 3
'hellohellohello'
>>> 'hello' * 'hello' ## error
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
'hello' * 'hello'
TypeError: can't multiply sequence by non-int of type 'str'
>>>
字符串是由单个字符组成,如果想访问某个字符;则采用str[数字]。数字为正数则从字符串的开头依次走;数字为负数则从字符的末尾开始往回走
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 'hello'[0]
'h'
>>> 'hello'[2]
'l'
>>> 'hello'[-1]
'o'
>>> 'hello'[0:3] ## [0:3] 0表示起始的元素,3表示步长(step)
'hel'
>>> 'hello'[-2:2]
''
>>> 'hello'[-2:-1]
'l'
>>> 'hello world'[6:11] ## 截取 world 字符串的两种方式
'world'
>>> 'hello world'[6:] ## 冒号后不写表示到该字符串末尾
'world'
>>> 'hello world'[-5:]
'world'
>>> 'hello world'[:-5] ## ==> 'hello world'[0:-5]
'hello '
>>>
网友评论