python基本数据类型

基本数据类型

Number 数字

  • 整数 int
  • 浮点数 float
>>> print('hello world')
hello world
>>> type(1)
<class 'int'>
>>> type(-1)
<class 'int'>
>>> type(1.1)
<class 'float'>
>>> type(1+1.0)
<class 'float'>
>>> type(1+1)
<class 'int'>
>>> type(1*1)
<class 'int'>
>>> type(1*1.0)
<class 'float'>
>>> type(2/2)
<class 'float'>
>>> type(2//2)
<class 'int'>
>>> 2/2
1.0
>>> 2//2
1
>>> 1//2
0
>>> 
  • 10 2 8 16 进制
>>> 0b10
2
>>> 0b11
3
>>> 0o10
8
>>> 0o11
9
>>> 0x10
16
>>> 0x11
17

>>> bin(10)
'0b1010'
>>> bin(0o10)
'0b1000'
>>> bin(0x10)
'0b10000'
>>> int(0b111)
7
>>> int(0o11)
9
>>> hex(16)
'0x10'
>>> hex(0o10)
'0x8'
>>> oct(0b10)
'0o2'
>>> oct(9)
'0o11'
>>> 

bin向二进制转换
int向十进制转换
hex16进制转换
oct向八进制转换
  • bool布尔类型
>>> True
True
>>> False
False
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> int(True)
1
>>> int(False)
0
>>> bool(1)
True
>>> bool(0)
False
>>> bool(2)
True
>>> bool(2.2)
True
>>> bool(-2)
True
>>> bool(0)
False
>>> bool(0b01)
True
>>> bool(0b0)
False
>>> 

>>> bool('abc')
True
>>> bool('')
False
>>> bool([1,2,3])
True
>>> bool([])
False
>>> bool({1,1,1})
True
>>> bool({})
False
>>> bool(None)
False
>>> 
  • complex复数
>>> 36j
36j
>>> 

str字符串

不可变

  • 单引号与双引号
>>> 'hello world'
'hello world'
>>> "hello world"
'hello world'
>>> type('1')
<class 'str'>
>>> "let's go"
"let's go"
>>> 'let"s go'
'let"s go'
>>> 'let\'s go'
"let'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'
>>> 
>>> print('\nhello world\nhello world\nhello world\n')

hello world
hello world
hello world

>>> 
>>> 'hello\
world'
'helloworld'
>>> 
  • 转义字符
特殊的字符
\n 换行
\r回车
\'单引号
\t横向制表符
思考
>>> print('hello\nworld')
hello
world
>>> 

>>> print('hello \\n world')
hello \n world
>>> 

>>> print('c:\\test\\test')
c:\test\test
>>> print(r'c:\test\test') 加入r是原始字符串/R
c:\test\test
  • 字符串运算
>>> 'hello'+'world'
'helloworld'
>>> 'hello'*3
'hellohellohello'
>>> 'hello world'[0]
'h'
>>> 'hello world'[-1]
'd'
>>> 'hello world'[0:5]
'hello'
>>> 'hello world'[0:-1]
'hello worl'
>>> 'hello world'[6:11]
'world'
>>> 'hello world'[-11:-6]
'hello'
>>> 'hello world'[6:]
'world'
>>> 'hello world'[:-6]
'hello'
>>> 'hello world'[-5:]
'world'
>>> 

列表list

可变

>>> type[1,2,3,4,5]
type[1, 2, 3, 4, 5]
>>> type([1,2,3,4,5])
<class 'list'>
>>> type([1,2,3,4,'hello'])
<class 'list'>
>>> type([1,2,3,4,'hello',True,False])
<class 'list'>
>>> type([[1,2],[3,4],[True,False]])  //嵌套列表
<class 'list'>
  • 操作列表
>>> ['a','b','c','d'][0]
'a'
>>> ['a','b','c','d'][0:2]
['a', 'b']
>>> ['a','b','c','d'][-1:]
['d']
>>> ['a','b','c','d'][0:-1]
['a', 'b', 'c']
>>> ['a','b','c','d']+['e','f']
['a', 'b', 'c', 'd', 'e', 'f']
>>> ['e','f']*3
['e', 'f', 'e', 'f', 'e', 'f']
>>> [['a','b'],['c','d'],['e','f'],['h','i'],['j','k']]

元组tuple

元组tuple的元素只能容纳不可变对象 且其中的元素可以是列表list

>>> (1,2,3)
(1, 2, 3)
>>> (1,'-1',True)
(1, '-1', True)
>>> (1,2,3)[0]
1
>>> (1,2,3)[0:2]
(1, 2)
>>> (1,2,3)+(4,5)
(1, 2, 3, 4, 5)
>>> (1,2,3)*3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> (1,2,3)[0:-1]
(1, 2)
>>> type((1,2,3))
<class 'tuple'>
>>> type((1))   //为什么这里不是元组类型 这里一个元素转换成运算
<class 'int'>
>>> type(('a'))  //为什么这里不是元组类型
<class 'str'>  

如何定义只有一个元素的元组
>>> type((1,))
<class 'tuple'>
>>> type(())
<class 'tuple'>
>>> type([1])  //单个列表不会是运算
<class 'list'>
>>> 

序列

str,list和tuple都可被认为是序列

有序 可用下标索引访问 切片操作

  • 序列共有的特性
>>> 'hello'[0]
'h'
>>> [1,2,3][0]
1
>>> (1,2,3)[0]
1
>>> 

>>> 'hello'[0:3]
'hel'
>>> [1,2,3][0:2]
[1, 2]
>>> (1,2,3)[0:2]
(1, 2)
>>> 
  • 序列中是否包含某元素
>>> 2 in [1,2,3]
True
>>> 2 not in [1,2,3]
False
>>> 4 in [1,2,3]
False

>>> 2 in (1,2,3)
True
>>> '2' in '123'
True

>>> len((1,2,3))
3
>>> len([1,2,3])
3
>>> len('hello')
5
>>> 

>>> max([1,2,3,4])
4
>>> min([1,2,3,4])
1

//转换成asc进行运算
>>> max('hello world')
'w'
>>> min('hello world')
' '
>>> min('helloworld')
'd'

>>> ord('w')
119
>>> ord('d')
100
>>> ord(' ')
32
彩蛋
(1, 2)
>>> 'hello world'[0:8:2]
'hlow'
>>> 

集合set

  • 无序

  • 没有索引 不能切片

  • 不重复

>>> type({1,2,3})
<class 'set'>
>>> {1,1,2,2,3,3}
{1, 2, 3}
>>> 
>>> len({1,2,3})
3
>>> 1 in {1,2,3}
True
>>> 1 not in {1,2,3}
False
>>> {1,2,3,4}-{1,2}  //求集合差集
{3, 4}
>>> {1,2,3,4}&{1,2}  //求集合交集
{1, 2}
>>> {1,2,3,4}|{1,2,5,6} //求集合并集
{1, 2, 3, 4, 5, 6}

//如何定义一个空的集合
>>> type({})
<class 'dict'>
>>> type(set())  //定义一个空的集合
<class 'set'>
>>> len(set())
0

字典dict

无序 集合

很多个key value

字典中不能有重复的key,并且key是不可变的类型

value没有什么限制

>>> {1:1,2:2,3:3}
{1: 1, 2: 2, 3: 3}
>>> type({1:1,2:2,3:3})
<class 'dict'>
>>> {'q':'一技能','w':'二技能','e':'三技能','r':'大招'}
{'q': '一技能', 'w': '二技能', 'e': '三技能', 'r': '大招'}

>>> {'q': '一技能', 'w': '二技能', 'e': '三技能', 'r': '大招'}['q']
'一技能'

>>> {1:'一技能','1':'二技能','e':'三技能','r':'大招'}
{1: '一技能', '1': '二技能', 'e': '三技能', 'r': '大招'}
>>> 

//空的字典
>>> type({})
<class 'dict'>
>>> 

注意:元组可以作为字典的key

>>> {[1,2]:'一技能','1':'二技能','e':'三技能','r':'大招'}
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    {[1,2]:'一技能','1':'二技能','e':'三技能','r':'大招'}
TypeError: unhashable type: 'list'
        
>>> {(1,2):'一技能','1':'二技能','e':'三技能','r':'大招'}
{(1, 2): '一技能', '1': '二技能', 'e': '三技能', 'r': '大招'}
>>> {(1):'一技能','1':'二技能','e':'三技能','r':'大招'}
{1: '一技能', '1': '二技能', 'e': '三技能', 'r': '大招'}
>>> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值