Python学习基础知识概要

爬虫代理IP由芝麻HTTP服务供应商提供php

一、输入输出java

输出实例python

print 'hello','world'
hello world

输入实例数组

name = raw_input();
print "hello,",name

world
hello,world

输入时提示实例app

name = raw_input('please enter your name:');
print "hello,",name

please enter your name:world
hello,world

raw_input 函数读入的是字符串,若是想要转换成int类型,就要用到int函数。函数

birth = int(raw_input('birth: '))

二、字符表示编码

十进制正常表示,十六进制最前面加 0x,小数正常表示,科学计数法表示 1.23×109就是1.23e9,或者 12.3e8代理

转义符 \code

转义符实例:对象

>>> print '\\\n\\'
\
\

防止转义,能够在前面加入 r

>>> print '\\\t\\'
\       \
>>> print r'\\\t\\'
\\\t\\

多行内容表示,用三引号包括

>>> print '''line1
... line2
... line3'''
line1
line2
line3

布尔值的表示 True 和 False

>>> 3 > 2
True

空值 None,至关于Java,C 中的 null

>>>print None==None
True

Unicode表示的字符串用 u’…’ 表示,转化成 UTF-8 编码

>>> u'ABC'.encode('utf-8')
'ABC'
>>> u'中文'.encode('utf-8')
'\xe4\xb8\xad\xe6\x96\x87'>

文本文件编码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

三、格式化

格式化输出实例

>>> 'Hello, %s' % 'world'
'Hello, world'
>>> 'Hi, %s, you have $%d.' % ('Michael', 1000000)
'Hi, Michael, you have $1000000.'

格式化整数和小数

>>> '%2d-%02d' % (3, 1)
' 3-01'
>>> '%.2f' % 3.1415926
'3.14'

万能格式化 %s,能够代替全部格式化

对于Unicode字符串,用法彻底同样,但最好确保替换的字符串也是Unicode字符串:

>>> u'Hi, %s' % u'Michael'
u'Hi, Michael'

输出百分号 %,用双 % 便可

>>> 'growth rate: %d %%' % 7
'growth rate: 7 %'

四、列表LIST

列表 list ,可变的有序表

>>> classmates = ['Michael', 'Bob', 'Tracy']
>>> classmates
['Michael', 'Bob', 'Tracy']

len函数获取它的长度

>>> len(classmates)
3

取得某个元素,能够用中括号索引

>>> classmates[0]
'Michael'
>>> classmates[1]
'Bob'
>>> classmates[2]
'Tracy'
>>> classmates[3]
Traceback (most recent call last):

倒数索引

>>> classmates[-1]
'Tracy'
>>> classmates[-2]
'Bob'
>>> classmates[-3]
'Michael'
>>> classmates[-4]
Traceback (most recent call last):

append 追加元素到末尾

>>> classmates.append('Adam')
>>> classmates
['Michael', 'Bob', 'Tracy', 'Adam']

insert 插入到指定位置

>>>> classmates.insert(1, 'Jack')
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']

pop 删除末尾元素

>>> classmates.pop()
'Adam'
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy']

pop 加入参数删除指定元素

>>> classmates.pop(1)
'Jack'
>>> classmates
['Michael', 'Bob', 'Tracy']

元素改变,直接赋值便可

>>> classmates[1] = 'Sarah'
>>> classmates
['Michael', 'Sarah', 'Tracy']

list能够嵌套,可用二维索引

>>> s = ['python', 'java', ['asp', 'php'], 'scheme']
>>> s[2][1]
php

空列表

>>> L = []
>>> len(L)
0

五、元组tuple

不可变有序的数组

定义元组

>>> classmates = ('Michael', 'Bob', 'Tracy')
>>> classmates
('Michael', 'Bob', 'Tracy')

空的元组

>>> classmates = ()
>>> classmates 
()

一个元素的元组

>>> t = (1,)
>>> t
(1,)

注意不能用 t = (1) 来定义, 由于它定义的不是tuple,是 1 这个数,这是由于括号既能够表示tuple,又能够表示数学公式中的小括号,这就产生了歧义,所以,Python规定,这种状况下,按小括号进行计算,计算结果天然是1。

表面上可变的tuple

>>> t = ('a', 'b', ['A', 'B'])
>>> t[2][0] = 'X'
>>> t[2][1] = 'Y'
>>> t
('a', 'b', ['X', 'Y'])

表面上看,tuple的元素确实变了,但其实变的不是tuple的元素,而是list的元素。tuple一开始指向的list并无改为别的list,因此,tuple所谓的“不变”是说,tuple的每一个元素,指向永远不变。即指向 ‘a’,就不能改为指向 ‘b’ ,指向一个list,就不能改为指向其余对象,但指向的这个list自己是可变的!

六、字典dict

字典 dict 即键值对组,dict的key必须是不可变对象。

>>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
>>> d['Michael']
95

把数据放入dict的方法,除了初始化时指定外,还能够经过key放入,在这以前,d 必须被声明,不然会报错

>>> d['Adam'] = 67
>>> d['Adam']

判断key是否在字典中

1. in 判断

>>> 'Thomas' in d
False

2. 经过dict提供的get方法,若是key不存在,能够返回None,或者本身指定的value

>>> print d.get('Thomas')
None
>>> print d.get('Thomas', -1)
-1

要删除一个key,用 pop(key) 方法,对应的value也会从dict中删除

>>> d.pop('Bob')
75
>>> d
{'Michael': 95, 'Tracy': 85}

七、集合set

set和dict相似,也是一组key的集合,但不存储value。因为key不能重复,因此,在set中,没有重复的key。

要建立一个set,须要提供一个list做为输入集合:

>>> s = set([1, 2, 3])
>>> s
set([1, 2, 3])

重复元素在set中自动被过滤:

>>> s = set([1, 1, 2, 2, 3, 3])
>>> s
set([1, 2, 3])

经过 add(key) 方法能够添加元素到set中,能够重复添加,但不会有效果:

>>> s.add(4)
>>> s
set([1, 2, 3, 4])
>>> s.add(4)
>>> s
set([1, 2, 3, 4])

经过 remove(key) 方法能够删除元素:

>>> s.remove(4)
>>> s
set([1, 2, 3])

判断元素是否在set中

>>> 5 in s 
True

set能够当作数学意义上的无序和无重复元素的集合,所以,两个set能够作数学意义上的交集、并集等操做:

>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s1 & s2
set([2, 3])
>>> s1 | s2
set([1, 2, 3, 4])
相关文章
相关标签/搜索