Python3基础之基本数据类型

242 阅读6分钟

简介

Python之禅

Simple is better than complex

代码风格

Pythonic

简洁、优雅、易懂

能做什么

爬虫、大数据、测试、web、AI、脚本处理

缺点

运行速度不如java、c

相对的开发效率高

运行效率和开发效率,鱼和熊掌不可兼得

什么是代码

代码是现实世界事物在计算机世界中的映射

什么是写代码

写代码是将现实世界中的事物用计算机语言来描述

基本数据类型

Number、string、List、Tuple、Sets、Dictionary

数字 、 字符串 、 列表 、 元组、集合、字典

一、数字 Number

整数:int

浮点数:float

布尔值:bool

复数:complex

2/2float 2//2int

二进制 0b10 八进制 0o10 十六进制 0x10

>>> 0b10
2
>>> 0b11
3
>>> 0o10
8
>>> 0o11
9
>>> 0x10
16
>>> 0x1f
31

进制转换

->二进制 bin()

>>> bin(10)
'0b1010'
>>> bin(0o7)
'0b111'
>>> bin(0xe)
'0b1110'

->十进制 int()

>>> int(0b111)
7
>>> int(0o77)
63

->十六进制 hex()

>>> hex(888)
'0x378'
>>> hex(0o7666)
'0xfb6'

->八进制 oct()

>>> oct(111)
'0o157'
>>> oct(0b111)
'0o7'
>>> oct(0x111)
'0o421'

布尔

非零数字->布尔真(True)0->布尔假(Flase) 非空字符串、数组、对象->True 空->False

复数

字母j表示

>>> 36j
36j

二、字符串 str

单引号、双引号、三引号 三引号表示多行字符串

>>> '''
... hello blacker
... hello blacker
... hi blacker
... '''
'\nhello blacker\nhello blacker\nhi blacker\n'

字符串前加r表示原始字符串

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

字符串基本操作方法

字符串的运算

>>> 'hello' + 'blacker'
'helloblacker'
>>> 'hello'*3
'hellohellohello'
>>> 'hello'*'blacker'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'
>>> 'hello blacker'[0]
'h'
>>> 'hello blacker'[5]
' '
>>> 'hello blacker'[-1]
'r'
>>> 'hello blacker'[-3]
'k'
>>> 'hello blacker'[0:1]
'h'
>>> 'hello blacker'[0:-1]
'hello blacke'
>>> 'hello blacker'[5:]
' blacker'
>>> 'hello blacker'[-5:]
'acker'
>>> 'hello blacker'[:-1]
'hello blacke'

Python运算符优先级

以下表格列出了从最高到最低优先级的所有运算符:

运算符 描述
** 指数 (最高优先级)
~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % // 乘,除,取模和取整除
+ - 加法减法
>> << 右移,左移运算符
& 位 'AND'
^ 位运算符
<= < > >= 比较运算符
<> == != 等于运算符
= %= /= //= -= += *= **= 赋值运算符
is is not 身份运算符
in not in 成员运算符
not or and 逻辑运算符

三、列表 list

列表中的项目应该包括在方括号中,这样python就知道你是在指明一个列表。一旦你创建了一个列表,你就可以添加,删除,或者是搜索列表中的项目。由于你可以增加或删除项目,我们说列表是可变的数据类型,即这种类型是可以被改变的,并且列表是可以嵌套的。

1.获取

注意前者截取的是str 后者是list

>>> ['新月打击','苍白之瀑','月之降临','月神冲刺'][0]
'新月打击'
>>> type(['新月打击','苍白之瀑','月之降临','月神冲刺'][0])
<class 'str'>
>>> ['新月打击','苍白之瀑','月之降临','月神冲刺'][0:-1]
['新月打击', '苍白之瀑', '月之降临']
>>> type(['新月打击','苍白之瀑','月之降临','月神冲刺'][0:-1])
<class 'list'>

2.列表操作

列表间可以 +*

>>> ['新月打击','苍白之瀑','月之降临','月神冲刺']+[True,False]
['新月打击', '苍白之瀑', '月之降临', '月神冲刺', True, False]
>>> ['新月打击','苍白之瀑','月之降临','月神冲刺']*2
['新月打击', '苍白之瀑', '月之降临', '月神冲刺', '新月打击', '苍白之瀑', '月之降临', '月神冲刺']

四、元组 tuple

元祖和列表十分相似,不过元组是不可变的。即你不能修改元组。元组通过圆括号中用逗号分隔的项目定义。元组通常用在使语句或用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。 与list区别:元组不可变、列表可变

定义一个元素的元组需要加一个逗号,否则括号会被当作运算符号处理而有悖于预期的效果。

>>> type((1))
<class 'int'>
>>> type((1,))
<class 'tuple'>

定义一个空元组

>>> type(())
<class 'tuple'>

str list tuple 都可以看作「 序列 」

序列的共性

通过[num]的方式可以访问序列

>>> "hello blacker"[2]
'l'
>>> [1,2,3][2]
3

切片

>>> [1,2,3,4,5][2:3]
[3]
>>> [1,2,3,4,5][-2:]
[4, 5]

元素是否在列表中

>>> 3 in [1,2,3,4,5]
True
>>> 3 not in [1,2,3,4,5]
False

len()、max()、min()

>>> len([1,2,3,4,5])
5
>>> max([1,2,3,4,5])
5
>>> min([1,2,3,4,5])
1
>>> len('hello blacker')
13
>>> max('hello blacker')
'r'
>>> min('hello blacker')
' '
>>> min('helloblacker')
'a'

查看编码 (ASC||)

>>> ord('w')
119
>>> ord('1')
49
>>> ord(' ')
32

五、集合 set

特点:无序无法使用{1,2,3,4}[1]来访问第1个元素

差集 ‘ - ’ 、交集 ‘ & ’ 、和(并)集 ‘ | ’

>>> {1,2,3,4} - {3,4}
{1, 2}
>>> {1,2,3,4} & {3,4}
{3, 4}
>>> {1,2,3,4} | {3,4,7}
{1, 2, 3, 4, 7}

如何定义一个空集

set()

>>> type({})
<class 'dict'>
>>> type(set())
<class 'set'>
>>> set({1,2,3})
{1, 2, 3}

六、字典 dict

可以有很多个keyvalue

key:必须是不可变类型、不可重复

value:可以是任意类型

{key:value,key1:value1,...}

>>> type({1:1,2:2,3:3})
<class 'dict'>
>>> {1:1,'1':2,'我们':'3'}[1]
1
>>> {1:1,'1':2,'我们':'3'}['1']
2
>>> {'Q':'新月打击','W':'苍白之瀑','E':'月之降临','R':'月神冲刺'}['Q']
'新月打击'

小结