码迷,mamicode.com
首页 > 编程语言 > 详细

Python

时间:2017-02-26 00:02:42      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:第一个   port   存在   3.x   file   ack   efault   复制   bsp   

 

  1. Python

 

获取用户输入:

>>> input("x:")
x:3
‘3‘

模块导入

>>> import math
>>> math.floor(32.9)
32
>>>

 

字符串

 

- 单引号字符串和转义字符

 

>>> ‘hello world‘
‘hello world‘
>>> "hello world"
‘hello world‘
>>>

 

>>> "let‘s go"
"let‘s go"
>>> ‘"hello world". she said‘
‘"helo world". she said‘

>>> "\"hello world\" . she said"
‘"hello world" . she said‘
>>>

 

- 拼接字符

 

>>> "let‘s say" ‘"hello world"‘
‘let\‘s say"hello world"‘
>>> x="hello "
>>> y="world"
>>> x+y
‘hello world‘
>>>

 

- str repr

str 值转为合理的字符串

repr  所见即所得

 

- input raw_input

raw_input: 输入的数据作为原始数据放入字符串内 (python3.x系列不再有 raw_input 函数。3.x input 和从前的 raw_input 等效)

input: 直接使用输入格式

 

- 原始字符串

原始字符串的最后一位不能是反斜杠

>>> print ("c:\nothere")
c:
othere
>>> print(r"c:\nothere")
c:\nothere
>>>

 

  1. 序列 (列表和元组)

列表可以修改

元组: 不可以修改

 

  1. 通用序列操作

操作

  • 索引 (indexing)
  • 分片 (slicing)
  • (adding)
  • (multiplying)

 

 

  1. 索引

编号从0开始

正数表示从前往后,负数表示从后往前,最后一位是-1

 

可以函数调用返回序列,可以直接对结果进行索引操作

 

  1. 分片

[slicingStart : remainStart : step]

 

Step负值表示从右往前

 

>>> tag=‘1234567890‘

>>> tag[2:4]

‘34‘

>>> tag[0:3]

‘123‘

>>> tag[-3:-1]

‘89‘

>>> tag[-3:]

‘890‘

>>>

>>> tag[0:3]

‘123‘

>>>

>>> tag[0:5:2]

‘135‘

>>> tag[0::2]

‘13579‘

>>> tag[::2]

‘13579‘

>>> tag[6:1:-2]

‘753‘

>>>

 

  1. 运算

可以相加或相乘,不能与字符串相加

>>> [1,2,3] + [4,5,6]

[1, 2, 3, 4, 5, 6]

>>> [1,2,3]*3

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

 

  1. 函数

in

>>> x = ‘hh‘

>>> x in ‘hello‘

False

>>> x in ‘hhello‘

True

 

>>> user = ‘123456‘

>>> input("Enter your user name: ") in user

Enter your user name: 1

True

>>>

 

min – max - len

>>> number = [1,2,3]

>>> len(number)

3

>>> max(number)

3

>>> min(number)

1

 

  1. 列表

[x,x,x]

  1. 操作

根据字符串创建列表

>>> list(‘hello‘)

[‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘]

 

  1. 赋值

 

>>> x=[1,2,3]

>>> x[1]=7

>>> x

[1, 7, 3]

>>>

 

  1. 删除

 

>>> x

[1, 7, 3]

>>> del x[1]

>>> x

[1, 3]

>>>

 

  1. 分片赋值

>>> name = list(‘12345‘)

>>> name[2:4] = list(‘abc‘)

>>> name

[‘1‘, ‘2‘, ‘a‘, ‘b‘, ‘c‘, ‘5‘]

 

不等长赋值

>>> name = list("1234567890")

>>> name[5:]=list(‘abc‘)

>>> name

[‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘a‘, ‘b‘, ‘c‘]

>>>

插入

>>> name = list("1234567890")

>>> name[5:5] = list(‘abc‘)

>>> name

[‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘a‘, ‘b‘, ‘c‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘0‘]

删除

>>> name

[‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘a‘, ‘b‘, ‘c‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘0‘]

>>> name[5:7] = []

>>> name

[‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘c‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘0‘]

 

  1. 方法

    1. append

append直接修改原来的表

>>> lst = [1,2,3]

>>> lst.append(‘4‘)

>>> lst

[1, 2, 3, ‘4‘]

>>>

  1. count

统计元素在表中出现的次数

 

  1. extend

列表的末尾追加另外一个序列,原序列不变

 

>>> a = [1,2,3]

>>> b = [‘a‘,‘b‘,‘c‘]

>>> c=a+b

>>> c

[1, 2, 3, ‘a‘, ‘b‘, ‘c‘]

>>> a

[1, 2, 3]

>>>

  1. index

从列表中找出第一个匹配项的索引位置

>>> a=[1,2,3,4,5]

>>> a.index(4)

3

  1. pop

移除列表中的一个元素,(默认最后一个),并返回这个元素的值

  1. remove

移除列表中某一个值的第一个匹配项,没有返回值

>>> x=[1,2,3]

>>> x=[1,2,3,11,22,33,1]

>>> x.remove(3)

>>> x

[1, 2, 11, 22, 33, 1]

>>>

  1. reverse

元素反向

  1. sort

>>> x=[1,4,3,2]

>>> x.sort()

>>> x

[1, 2, 3, 4]

>>>

如果需要x不变,返回另外一个排好序的列表

>>> x=[4,3,2,1]

>>> y=x.sort()

>>> x

[1, 2, 3, 4]

>>> print(y)

None

>>>

对y赋值用y=x[:]

>>> x=[4,3,2,1]

>>> y=x[:]

>>> y.sort()

>>> x

[4, 3, 2, 1]

>>> y

[1, 2, 3, 4]

y=x使x和y指向同一个列表

>>> x=[4,3,2,1]

>>> y=x

>>> y.sort()

>>> x

[1, 2, 3, 4]

>>> y

[1, 2, 3, 4]

用sorted函数

>>> x=[4,3,2,1]

>>> y=sorted(x)

>>> x

[4, 3, 2, 1]

>>> y

[1, 2, 3, 4]

>>>

sorted可以用于任何序列,却总是返回一个列表

>>> x=sorted("hello")

>>> x

[‘e‘, ‘h‘, ‘l‘, ‘l‘, ‘o‘]

>>>

 

  1. 元组:不可变序列

(x,x,x)

 

单个元素

(x,)

 

  1. 操作

    1. tuple

产生元组

>>> tuple(‘abc‘)

(‘a‘, ‘b‘, ‘c‘)

>>> tuple([‘a‘,‘b‘,‘c‘])

(‘a‘, ‘b‘, ‘c‘)

>>> tuple((‘1‘,‘2‘,‘3‘))

(‘1‘, ‘2‘, ‘3‘)

>>>

 

 

 

 

  1. 字符串

  2. 字典:当索引不好用时

{x:y:z, m:n:o}

 

空字典

{}

 

Dict函数创建

>>> items = ([1,"a"],[2,"b"])

>>> d=dict(items)

>>> d

{1: ‘a‘, 2: ‘b‘}

>>>

 

>>> d=dict(m=‘a‘,n=‘b‘)

>>> d

{‘m‘: ‘a‘, ‘n‘: ‘b‘}

>>>

 

>>> x=dict(o="c",p="d",**d)

>>> x

{‘p‘: ‘d‘, ‘m‘: ‘a‘, ‘o‘: ‘c‘, ‘n‘: ‘b‘}

 

  1. 基本字典操作

 

 

 

  1. 字典的格式化字符串

 

  1. 字典方法

    1. clear

无返回值

>>> d ={}

>>> d[‘name‘]=‘lucy‘

>>> d[‘id‘] = 2

>>> d

{‘name‘: ‘lucy‘, ‘id‘: 2}

>>> return_value = d.clear()

>>> d

{}

>>> print(return_value)

None

>>>

  1. copy

浅复制 shallow copy

副本中值的替换不会对源字典产生影响

>>> x={‘uername‘:‘lucy‘, ‘age‘:30}

>>> y=x.copy()

>>> y[‘age‘]=15

>>> y

{‘uername‘: ‘lucy‘, ‘age‘: 15}

>>> x

{‘uername‘: ‘lucy‘, ‘age‘: 30}

>>>

副本中值的修改会产生影响

>>> x ={‘username‘:‘lucy‘, ‘books‘:[‘eng‘,‘math‘]}

>>> y=x.copy()

>>> y[‘books‘].remove(‘eng‘)

>>> y

{‘username‘: ‘lucy‘, ‘books‘: [‘math‘]}

>>> x

{‘username‘: ‘lucy‘, ‘books‘: [‘math‘]}

>>>

深复制 deep copy

>>> from copy import deepcopy

>>> d = {‘username‘:[‘zs‘,‘ls‘], ‘age‘:13}

>>> e=d.copy()

>>> f=deepcopy(d)

>>> d

{‘age‘: 13, ‘username‘: [‘zs‘, ‘ls‘]}

>>> d[‘username‘].append(‘wer‘)

>>> e

{‘age‘: 13, ‘username‘: [‘zs‘, ‘ls‘, ‘wer‘]}

>>> f

{‘age‘: 13, ‘username‘: [‘zs‘, ‘ls‘]}

>>>

 

  1. fromkeys

 

使用给定的key生成字典,默认值为None

 

>>> {}.fromkeys([‘name‘,‘age‘])

{‘age‘: None, ‘name‘: None}

 

>>> d={‘name‘:‘wang‘,‘age‘:12}

>>> d

{‘age‘: 12, ‘name‘: ‘wang‘}

>>> d.fromkeys(‘gender‘)

{‘g‘: None, ‘n‘: None, ‘e‘: None, ‘d‘: None, ‘r‘: None}

>>> d.fromkeys([‘gender‘])

{‘gender‘: None}

 

如果不想用默认的值None,也可以自己制定值

>>> {}.fromkeys([‘username‘,‘age‘],‘unk‘)

{‘age‘: ‘unk‘, ‘username‘: ‘unk‘}

>>>

 

  1. get

提供更宽松的方法访问字典,可以检测key不存在的场景

 

>>> d={}

>>> print(d[‘name‘])

Traceback (most recent call last):

File "<pyshell#100>", line 1, in <module>

print(d[‘name‘])

KeyError: ‘name‘

>>> print(d.get(‘name‘))

None

 

>>> d=dict(m=‘a‘,n=‘b‘)

>>> print d.get("m")

a

>>> print d.get("x")

None

>>> print d.get("x","not found")

not found

 

  1. has_key

(python3.3不支持)

  1. items & iteritems

items 返回列表,无特殊顺序

iteritems返回迭代器 (python3.3不支持)

>>> d={‘username‘:‘zs‘, ‘age‘:12}

>>> d.items()

dict_items([(‘username‘, ‘zs‘), (‘age‘, 12)])

  1. keys & iterkeys

 

  1. pop

>>> d

{‘username‘: ‘zs‘, ‘age‘: 12}

>>> d.pop(‘age‘)

12

>>> d

{‘username‘: ‘zs‘}

>>>

  1. popitem

popitem弹出随机项

  1. setdefault

类似于get,如果键值不存在,会将键值设定,值默认为None,也可根据参数设定

>>> d={}

>>> d.setdefault(‘name‘)

>>> d

{‘name‘: None}

>>> d={}

>>> d.setdefault(‘name‘,‘unkonwn‘)

‘unkonwn‘

>>> d

{‘name‘: ‘unkonwn‘}

>>>

  1. update

利用一个字典更新另外一个字典

 

>>> d={"name":"haha", "age":32}

>>> x={"name":"haha", "age":23}

>>> d.update(x)

>>> d

{‘age‘: 23, ‘name‘: ‘haha‘}

>>>

  1. values & itervalues

返回字典的值

>>> d= {}

>>> d[1]=1

>>> d[2]=2

>>> d[3]=3

>>> d[4]=1

>>> d.values()

dict_values([1, 2, 3, 1])

>>>

 

 

  1.  
  1. 条件,循环和其他语句

 

__init__.py

this file is required to tell Python that the folder is to be treated as a package.

 

  1. 异常

装饰器

Python

标签:第一个   port   存在   3.x   file   ack   efault   复制   bsp   

原文地址:http://www.cnblogs.com/2dogslife/p/6443216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!