更多课程 选择中心


Python培训

400-111-8989

Python开发中基本代码笔记

  • 发布:Python培训
  • 来源:网络
  • 时间:2018-07-05 16:03

这篇文章为大家整理的一些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 "<stdin>", 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 "<stdin>", line 1, in <module>

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'

>>>

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

Python开发中基本代码笔记

组的概念

列表 [ ]

>>> [[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))

<class 'int'> 元祖只有一个的元素 括号按数学运算

>>> type(('a'))

<class 'str'>

定义只有一个元素的元祖 (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 "<stdin>", line 1, in <module>

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())

Python开发中基本代码笔记

字典

很多个key 和 value

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

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

神冲刺'}

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

神冲刺'}[1]

'新月打击'

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

神冲刺'}['1']

'苍白之瀑'

>>>

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

value:几乎可以是所有类型

定义一个空字典

>>> {}

{}

>>> type({})

<class 'dict'>

预约申请免费试听课

填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!

上一篇:用Python开发拼图游戏教程
下一篇:7 月编程语言指数榜:Java落榜Python又是第一

Python从入门到项目实战训练营开课啦!

Python语言为什么这么受人欢迎?看完这个你就知道了!

学Python去达内教育怎么样?

达内Python培训免费训练营开班啦

  • 扫码领取资料

    回复关键字:视频资料

    免费领取 达内课程视频学习资料

Copyright © 2023 Tedu.cn All Rights Reserved 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有

选择城市和中心
黑龙江省

吉林省

河北省

湖南省

贵州省

云南省

广西省

海南省