20190804-Python基础 第二章 列表和元组

容器,Python支持一种数据结构的基本概念(容器,基本上就是可包含其他对象的对象。)

两种主要的容器是:序列(如列表和元组)和映射(如字典)

Ps: 列表与元组区别,列表可修改,元组不能。

对序列的通用操作:索引、切片、相加、相乘、成员资格检查。其他,内置函数确定序列长度,以及找出序列中最大和最小元素。

1. 索引

 1 # 将以指定年、月、日的日期打印出来
 2 months = [
 3     'January',
 4     'February',
 5     'March',
 6     'April',
 7     'May',
 8     'June',
 9     'July',
10     'August',
11     'September',
12     'October',
13     'November',
14     'December'
15 ]
16 
17 # 一个列表,其中包含1-31对应的结尾
18 endings = ['st','nd','rd'] + 17*['th']\
19         + ['st','nd','rd'] + 7*['th']\
20         + ['st']
21 
22 year = input('Year:')
23 month = input('Month(1-12):')
24 day = input('Day(1-21):')
25 
26 month_number = int(month)
27 day_number = int(day)
28 
29 # 别忘了将表示月和日的数减1,这样才能得到正确的索引
30 month_name = months[month_number-1]
31 ordinal = day + endings[day_number-1]
32 
33 print(month_name + ' ' + ordinal + ', ' + year)
34 
35 # 测试endings列表打印出的内容
36 print(['st','nd','rd'] + 17*['th']\
37         + ['st','nd','rd'] + 7*['th']\
38         + ['st'])

2. 切片

1 # 从url中提取域名https://www.google.com
2 url = input('input your url:')
3 domain = url[12:-4]
4 print('Domain name:' + domain)
5 结果:
6 input your url:https://www.google.com 
7 Domain name:google.
8 因输入url直接回车会跳转打开链接,所以空格回车输出结果,但引入了一个多余的空格,导致最终结果多了一个"." 

number[3:6:1],默认步长为1,可不写。

第一个索引包含在内,第二个索引不包含在内。

步长为正数时,从起点移动到终点;为负数时,从终点到起点,且第一个索引必须比第二个索引大。

3. 序列相加

 1 print([1,2,3] + [7,8,9,10])
 2 结果:
 3 [1, 2, 3, 7, 8, 9, 10]
 4 
 5 print([1,2,3,4] + 'qwrrt')
 6 结果:
 7 Traceback (most recent call last):
 8   File "D:/Python/PycharmProjects/untitled1/venv/Robots_learning.py", line 818, in <module>
 9     print([1,2,3,4] + 'qwrrt')
10 TypeError: can only concatenate list (not "str") to list

使用加法运算拼接序列,但是,一般不能拼接不同类型的序列。

4. 乘法

将序列与数x相乘时,将重复这个序列x次来创建一个新的序列:

1 print('Python '* 5)
2 结果:
3 Python Python Python Python Python 

None在Python中代表什么都没有;初始化。

 1 # 在屏幕中央且宽度合适的方框内打印一个句子
 2 sentence = input('Plz enter your sentence:')
 3 
 4 screen_width = 80
 5 text_width = len(sentence)
 6 box_width = text_width + 12
 7 left_margin = (screen_width - box_width) // 2
 8 
 9 print()
10 print(' '* left_margin + '+' + '-'*(box_width-2) + '+')
11 print(' '* left_margin + '|' + ' '*(box_width-2) + '|')
12 print(' '* left_margin + '|' + ' '*(((box_width - text_width)//2)-1) + sentence +' '*(((box_width - text_width)//2)-1)+'|')
13 print(' '* left_margin + '|' + ' '*(box_width-2) + '|')
14 print(' '* left_margin + '+' + '-'*(box_width-2) + '+')
15 print()
16 
17 结果:
18 Plz enter your sentence:You'er beautiful!
19 
20                          +---------------------------+
21                          |                           |
22                          |     You'er beautiful!     |
23                          |                           |
24                          +---------------------------+

5. 成员资格检测

检查特定值是否包含在序列中,使用运算符in,返回结果True,False,为布尔运算。

 1 # 检查用户名和PIN码
 2 database = [
 3     ['albert', '12345'],
 4     ['dilbert', '32564'],
 5     ['smith', '43456']
 6 ]
 7 
 8 username = input('User Name:')
 9 pin = input('PIN code:')
10 
11 # 检查用户名和PIN码
12 if [username, pin] in database:
13     print('Access granted!')
14 else:
15     print('You don\'t have access to the database!')
16 
17 结果:
18 User Name:Elon
19 PIN code:1232345
20 You don't have access to the database!

猜你喜欢

转载自www.cnblogs.com/ElonJiang/p/11299293.html