达内python笔记_Python开发中基本代码笔记

这篇文章为大家整理的一些python  数据基本类型的笔记,如同小字典一样,如果感觉有用,收藏下来,以备日后用到的时候不至于手忙脚乱!

// 表示取整

/ 表示除法 得 float

0b 2进制

0o 8进制

0x 16进制

转换:

bin(10) 转2进制

int(0b111) 转10进制

hex(888) 转16进制

oct(0b1111) 转8进制

bool 布尔类型:表示真、假

int(Ture) => 1

int(False) => 0

bool(1) => Ture

bool(0) =>False

非零 即为 True

字符串 非空 即为True

列表 元祖 等 非空 即为True

bool(None) => False

复数 36j

---------------------

str 字符串

单引号 双引号 三引号

'let\'s go'

"let's go"

'''

let's

go

'''

idle 换行

'hello\

world'

'''hello world

hello world'''

----------------

转义字符

无法 看见 的字符

特殊的字符

与语言本身语法有冲突的字符

\n 换行

\r 回车

\' 单引号

\t 横向制表符

print('hello \\n world') 不换行

print(r'C:\\Python36\\work')

字符串前面加小写r,表示原始字符串

>>> print(r'let's go')

File "", line 1

print(r'let's go')

^

SyntaxError: invalid syntax

这个不能通过 加 r 来解决

字符串基本操作方法 字符串的运算

>>> a='hello'

>>> b=' world'

>>> c=a+b

>>> c

'hello world'

+为字符连接

>>> 'hello '*3

'hello hello hello '

>>> 'hello'[0]

'h'

>>> ' hello'[0]

' '

>>> ' hello'[-1]

'o'

>>> ' hello'[-3]

'l'

>>> ' hello'[-30]

Traceback (most recent call last):

File "", line 1, in

IndexError: string index out of range

- 表示从末尾数

>>> 'hello world'[0:5]

'hello'

范围截取 必须截取到指定字符的下一位

>>> 'hello world'[6:11]

'world'

>>> 'hello world'[6:20]

'world'

>>> 'hello world'[6:]

'world'

>>> 'hello world'[6:-1]

'worl'

>>> 'hello world'[-5:]

'world'

>>>

--------------------------------------

组的概念

列表 [ ]

>>> [[1,2],[3,4],[True,False]][0] 第一个

[1, 2]

>>> [[1,2],[3,4],[True,False]][2]第三个

[True, False]

>>> [[1,2],[3,4],[True,False]][0:2] 0-1

[[1, 2], [3, 4]]

>>> [[1,2],[3,4],[True,False]][-1:] 最后一个

[[True, False]]

>>> [1]+[2]

[1, 2]

>>> [1]*3

[1, 1, 1]

元祖 ()

>>> (1,2,3,4)[0]

1

>>> (1,2,3,4)[0:2]

(1, 2)

>>> (1,2,3,4)+(5,6)

(1, 2, 3, 4, 5, 6)

>>> (1,2)*4

(1, 2, 1, 2, 1, 2, 1, 2)

int,list[],str,tuple()

>>> type((1))

元祖只有一个的元素 括号按数学运算

>>> type(('a'))

定义只有一个元素的元祖 (1,)

定义空元祖 type(())

列表 和 元祖的区别

>>> '12345678'[0:7:2]

'1357'

>>> '12345678'[0:7:3]

'147'

>>>

判断 是否在

>>> 1 in [1,2,3]

True

>>> 4 in [1,2,3]

False

>>> 1 not in [1,2,3]

False

>>> 4 not in [1,2,3]

True

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

3

>>> min(1,2,3)

1

>>> min('bac')

'a'

>>> min('bacc')

'a'

>>> min('bacc ')

' '

ord函数获取 asscil

>>> ord('a')

97

集合 set {}

无序

不重复

>>> {1,1,2,2,}

{1, 2}

>>> len({1,2,3})

3

>>> 1 in {1,2}

True

>>> 1 not in {1,2}

False

>>> {1,2,3,4,5,6}+{3,4}

Traceback (most recent call last):

File "", line 1, in

TypeError: unsupported operand type(s) for +: 'set'

and 'set'

>>> {1,2,3,4,5,6} - {3,4} 差集

{1, 2, 5, 6}

>>> {1,2,3,4,5,6} & {3,4} 交集

{3, 4}

>>> {1,2,3,4,5,6} | {3,4,7} 并集

{1, 2, 3, 4, 5, 6, 7}

定义一个空的集合

type(set())

字典

很多个key 和 value

不能有相同的key 如有取最后一个

{'Q':'新月打击','W':'苍白之瀑','E':'月至降临','R':'月

神冲刺'}

>>> {1:'新月打击','1':'苍白之瀑','E':'月至降临','R':'月

神冲刺'}[1]

'新月打击'

>>> {1:'新月打击','1':'苍白之瀑','E':'月至降临','R':'月

神冲刺'}['1']

'苍白之瀑'

>>>

key : 必须是不可变的类型 int 'str' 元组可以

value:几乎可以是所有类型

定义一个空字典

>>> {}

{}

>>> type({})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值