Python学习基础知识概要

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


1.输入输出

输出实例
print 'hello','world'
hello world


输出实例
name = raw_input();
print "hello,",name

world
hello,world


输入时提示实例
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: '))




2、字符表示




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

转义符 \

转义符实例:

>>> 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'>




文本文件编码

>>> '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 %'


4、列表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


5、元组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本身是可变的!

6、字典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}


7、集合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])

猜你喜欢

转载自zhimaruanjian.iteye.com/blog/2403741