第一章:基础知识
>>> from __future__ import division #实现斜杠/为除,而不是整除 >>> 1 // 2 #实现整除 >>> 2 ** 3 #实现指数 >>> pow(2,3) #实现指数函数 >>> 0xAF #十六进制 >>> 010 #八进制 >>> x = input("x:") #输入的时候要带着格式,字符串要加引号 >>> x = raw_input("y:") #输入的时候不用加格式,直接转换为字符串 >>> import math #导入模块 >>> math.floor(342.34) 342.0 >>> from math import sqrt #下面不用再加math >>> sqrt(9) 3.0 >>> import cmath #复数 >>> cmath.sqrt(-1) 1j >>> print "Hello" #单引号和双引号可以交叉使用,都可以用 Hello >>> print 'Hello' Hello >>> print "Let's go!" #字符串有单,就用双 Let's go! >>> print '"Hello!"' #字符串有双,就用单 "Hello!" >>> print 'Let\'s go' #转义字符 Let's go >>> 1+2+3\ #反斜杠可以作为换行符 +4+5 15 >>> print 'Hello,\nworld' #\n作为换行符 Hello, world >>> print r'C:\nowhere' #前面加个r就实现原始字符,不再考虑转义字符了 C:\nowhere >>> print r'C:\Program Files\fnord' C:\Program Files\fnord
本章的新函数
第二章:列表和元组
参考学习内容:http://blog.sina.com.cn/s/blog_4b5039210100e9yd.html
>>> name = 'Alex' >>> name[0] #第一个 'A' >>> name[-1] #从最后一个倒回去 'x' >>> 'Hello'[1] #直接用 'e' >>> fourth = raw_input('Year:')[3] Year:2005 >>> fourth '5'
2-1
months = [ 'January', 'Feburuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] endings=['st','nd','rd'] + 17*['th']\ +['st','nd','rd'] + 7 * ['th']\ +['st'] year = raw_input('Year:') month = raw_input('Month(1-12):') day = raw_input('Day(1-31):') month_number = int(month) day_number = int(day) month_name = months[month_number-1] ordinal = day + endings[day_number-1] print month_name + ' ' + ordinal + ', ' + year
运行结果如下(不得不说今天是11.11.11)
Year:2011 Month(1-12):11 Day(1-31):11 November 11th, 2011
分片(格式关系很大)
>>> tag = '<a href="http://www.python.org">Python web site</a>' >>> tag[9:30] 'http://www.python.org' >>> tag[32:-4] 'Python web site'
>>> numbers = [1,2,3,4,5,6,7,8,9,10] >>> numbers[3:6] [4, 5, 6] >>> numbers[0:1] [1] >>> numbers[8:] [9, 10] >>> numbers[-3:] [8, 9, 10] >>> numbers[:] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> numbers[:3] [1, 2, 3]
2-2
# Split up a URL of the form http://www.something.com url = raw_input('Please enter the URL: ') domain = url[11:-4] print "Domain name: " + domain
Please enter the URL: http://www.baidu.com Domain name: baidu
更大的步长
>>> numbers = [1,2,3,4,5,6,7,8,9,10] >>> numbers[0:10:1] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> numbers[0:10:2] [1, 3, 5, 7, 9] >>> numbers[0:10:3] [1, 4, 7, 10] >>> numbers[0:10:-2] [] >>> numbers[::-2] [10, 8, 6, 4, 2] >>> numbers[5::-2] [6, 4, 2] >>> numbers[:5:-2] [10, 8]
序列相加
>>> [1,2,3]+[4,5,6] [1, 2, 3, 4, 5, 6] >>> a = [1,3,5] >>> b = [2,4,6 ] >>> c = a + b >>> c [1, 3, 5, 2, 4, 6]
乘法
>>> 'python'*5 'pythonpythonpythonpythonpython' >>> [42]*10 [42, 42, 42, 42, 42, 42, 42, 42, 42, 42]
初始化一个长度为10的列表
>>> sequence = [None] * 10 >>> sequence [None, None, None, None, None, None, None, None, None, None]
2-3 序列(字符串)乘法示例
sentence = raw_input("Sentence: ") screen_width = 80 text_width = len(sentence) box_width = text_width + 6 left_margin = (screen_width - box_width) // 2 print print ' ' * left_margin + '+' + '-' * (box_width-2) + '+' print ' ' * left_margin + '| ' + ' ' * text_width + ' |' #注意空格 print ' ' * left_margin + '| ' + sentence + ' |' print ' ' * left_margin + '| ' + ' ' * text_width + ' |' print ' ' * left_margin + '+' + '-' * (box_width-2) + '+' print
成员资格(in)
>>> permissions = 'rw' >>> 'w' in permissions True >>> 'x' in permissions False >>> users = ['mlh','foo','bar'] >>> raw_input('Enter your user name:') in users Enter your user name:mlh True >>> subject = '$$Get rich now!!!$$$' >>> '$$$' in subject True
2-4 序列成员资格示例
database = [ ['albert', '1234'], ['dilbert', '4242'], ['smith', '7524'], ['jones', '9843'] ] username = raw_input('User name: ') pin = raw_input('PIN code: ') if [username, pin] in database: print 'Access granted'
User name: smith PIN code: 7524 Access granted
内建函数
>>> numbers = [100,34,679] >>> len(numbers) 3 >>> max(numbers) 679 >>> min(numbers) 34 >>> max(2,3) 3 >>> min(8,3,4,2) 2
list函数
>>> list('hello') ['h', 'e', 'l', 'l', 'o'] >>> lists = list('hello') >>> lists ['h', 'e', 'l', 'l', 'o']
>>> x = [1,1,1] >>> x[1] 1 >>> x[2] = 2 >>> x [1, 1, 2]
删除元素
>>> names = ['Alice','Beth','Cecil','Dee-Dee','Earl'] >>> del names[2] >>> names ['Alice', 'Beth', 'Dee-Dee', 'Earl']
分片赋值、插入新的元素
>>> name = list('Perl') >>> name ['P', 'e', 'r', 'l'] >>> name[2:] = list('ar') >>> name ['P', 'e', 'a', 'r'] >>> name[1:] = list('ython') >>> name ['P', 'y', 't', 'h', 'o', 'n'] >>> numbers = [1,5] >>> numbers[1:1] = [2,3,4] >>> numbers [1, 2, 3, 4, 5] >>> numbers[1:4] = [] >>> numbers [1, 5]
列表方法
append
>>> lst = [1,2,3] >>> lst.append(4) >>> lst [1, 2, 3, 4]
count
>>> [2,4,3,2,4,5,6,2,6].count(2) 3 >>> x = [[2,3],[3,4],[2,5]] >>> x.count(1) 0 >>> x.count([2,3]) 1
extend
>>> a = [1,2,3] >>> b = [4,5,6] >>> a.extend(b) >>> a [1, 2, 3, 4, 5, 6]
index
>>> knights = ['We','are','the','knight','who','say','in'] >>> knights.index('who') 4
insert
>>> numbers = [1,2,3,4,5,6] >>> numbers.insert(3,'four') >>> numbers [1, 2, 3, 'four', 4, 5, 6] >>> numbers[4:4] = ['four'] >>> numbers [1, 2, 3, 'four', 'four', 4, 5, 6] >>>
pop(栈操作)
>>> x = [1,2,3] >>> x.append(x.pop()) >>> x [1, 2, 3]
remove
>>> x = ['to','be','or','not','to','be'] >>> x.remove('be') >>> x ['to', 'or', 'not', 'to', 'be']
reversed和list函数
>>> x = [1,2,3] >>> list(reversed(x)) [3, 2, 1]
sort
>>> x = [4,6,2,3,8,1] >>> x.sort() >>> x [1, 2, 3, 4, 6, 8]
存入副本
>>> x = [4,6,2,1,7,9] >>> y = x[:] >>> y.sort() >>> x [4, 6, 2, 1, 7, 9] >>> y [1, 2, 4, 6, 7, 9]
比较
>>> x = [3,2,5,4,6] >>> y = x >>> y.sort() >>> x [2, 3, 4, 5, 6] >>> y [2, 3, 4, 5, 6]
使用sorted函数
>>> x = [4,6,2,1,7,9] >>> y = sorted(x) >>> x [4, 6, 2, 1, 7, 9] >>> y [1, 2, 4, 6, 7, 9]
高级排序
>>> cmp(42,32) 1 >>> cmp(99,100) -1 >>> numbers = [5,3,9,7] >>> numbers.sort(cmp) >>> numbers [3, 5, 7, 9] >>> x = ['aadkfjl','dkfjl','eori','dkf'] >>> x.sort(key = len) >>> x ['dkf', 'eori', 'dkfjl', 'aadkfjl'] >>> x = [3,4,6,2,5] >>> x.sort(reverse = True) #大小写敏感的!!! >>> x [6, 5, 4, 3, 2]
元组:不可变序列
>>> 1,2,4 (1, 2, 4) >>> 43 43 >>> 34, (34,) >>> (1,2,5) (1, 2, 5) >>> (43,) (43,)
tuple函数
>>> tuple([1,2,4]) (1, 2, 4) >>> tuple('abc') ('a', 'b', 'c') >>> tuple((1,2,3)) (1, 2, 3)
基本元组操作
>>> x = 1,2,4 >>> x (1, 2, 4) >>> x[1] 2 >>> x[0:2] (1, 2)