# -*- coding:utf-8 -*- #最基本的数据结构:序列,索引从0开始 #python包括6种内建的序列 #最常用的序列包括:列表,元祖 #列表 edward = ['Edward Gumby',42] print edward #列表的元素可以是列表 john=['John Smith',50] database = [edward, john] print database #python之中还有一种名为容器的数据结构。 #序列(如列表和元组)和映射(如字典)是两类主要的容器。 #序列的每个元素都有自己的编号,而映射中的每一个元素则有一个名字(也称为键) #既不是序列也不是映射的容器类型:集合(set) #序列(list riple)的通用操作:索引、分片、加、乘、以及检查某个元素是否属于序列的成员、计算序列长度、找出最大元素、找出最小元素、迭代 #字符串使用索引 greeting = 'hello' print greeting[0] print greeting[-1] #字符串字面值直接使用索引 print greeting[1] #如果一个函数返回一个序列,那么可以直接对返回结果进行索引操作。 #fourth = raw_input('Year: ')[3] #print fourth #输入年月日的数字,打印日期 #为方便后续笔记调试,该段代码已被注释掉 ''' months = [ 'January', 'February', '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 ''' #分片,t[a:b]不包括t[b] tag = 'https://www.quora.com/#' tmpTag = tag[8:21] print tmpTag #www.quora.com print tag[8:-2] #www.quora.com #对列表分片 numbers = [1,2,3,4,5,6,7,8,9,10] print numbers[3:6] #[4, 5, 6] #访问后几个元素 print numbers[8:] print numbers[-3:-1] #[8, 9] print numbers[-3:0] #[] print numbers[-3:] #[8, 9, 10] print numbers[:3] #[1, 2, 3] print numbers[:] #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #对http://www.quora.com形式的URL进行分割 ''' url = raw_input("Please enter the url:") domain = url[11:-4] print "Domain name: " + domain ''' #更大的步长 print numbers[0:10:1] #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print numbers[0:10:2] #[1, 3, 5, 7, 9] print numbers[3:6:3] #[4] print numbers[::3] #[1, 4, 7, 10] #步长可以是负数 print numbers[8:3:-1] #[9, 8, 7, 6, 5] #范围和步长不一致,输出为空 print numbers[3:8:-1] #[] print numbers[::-2] #[10, 8, 6, 4, 2] #一端没有指定,步长为负的情况:起始点大于结束点 print numbers[5::-2] #[6, 4, 2] print numbers[:5:-2] #[10, 8] #序列相加 print [1,2,3]+[4,5,6] print 'Hello ' + 'world!' ''' print [1,2,3] + 'world!' #TypeError: can only concatenate list (not "str") to list ''' #乘法 print 'python '*5 print [42] * 10 #None、空列表 和 初始化 sequence = [None]*10 print len(sequence) #以正确的宽度在居中的“盒子”内打印一个句子 ''' sentence = raw_input("Sentence: ") screen_width = 80 text_width = len(sentence) box_width = text_width + 20 left_margin1 = (screen_width - box_width) // 2 left_margin2 = (screen_width - text_width - 4) // 2 print print ' ' * left_margin1 + '+' + '-' * (box_width -2) + '+' print ' ' * left_margin2 + '| ' + ' ' * text_width + ' |' print ' ' * left_margin2 + '| ' + sentence + ' |' print ' ' * left_margin2 + '| ' + ' ' * text_width + ' |' print ' ' * left_margin1 + '+' + '-' * (box_width -2) + '+' print ''' #成员资格 permissions = 'rw' print 'w' in permissions print 'x' in permissions users = ['mlh', 'foo', 'bar'] #print raw_input('Enter your user name: ') in users subject = 'Get rich now!!' print 'rich' in subject #True #检查用户名和输入码 ''' database = [ ['albert', '1234'], ['dilbert', '4242'], ['smith', '7524'], ['jones', '8765'] ] username = raw_input('User name: ') pin = raw_input('PIN code: ') if [username, pin] in database: print 'Access granted' ''' #长度、最小值、最大值 numbers = [100, 34, 765] print len(numbers) print max(numbers) print min(numbers)