20190804-Python基础 第二章 列表和元组
2024-09-05 05:25:08
容器,Python支持一种数据结构的基本概念(容器,基本上就是可包含其他对象的对象。)
两种主要的容器是:序列(如列表和元组)和映射(如字典)
Ps: 列表与元组区别,列表可修改,元组不能。
对序列的通用操作:索引、切片、相加、相乘、成员资格检查。其他,内置函数确定序列长度,以及找出序列中最大和最小元素。
1. 索引
# 将以指定年、月、日的日期打印出来
months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
] # 一个列表,其中包含1-31对应的结尾
endings = ['st','nd','rd'] + 17*['th']\
+ ['st','nd','rd'] + 7*['th']\
+ ['st'] year = input('Year:')
month = input('Month(1-12):')
day = input('Day(1-21):') month_number = int(month)
day_number = int(day) # 别忘了将表示月和日的数减1,这样才能得到正确的索引
month_name = months[month_number-1]
ordinal = day + endings[day_number-1] print(month_name + ' ' + ordinal + ', ' + year) # 测试endings列表打印出的内容
print(['st','nd','rd'] + 17*['th']\
+ ['st','nd','rd'] + 7*['th']\
+ ['st'])
2. 切片
# 从url中提取域名https://www.google.com
url = input('input your url:')
domain = url[12:-4]
print('Domain name:' + domain)
结果:
input your url:https://www.google.com
Domain name:google.
因输入url直接回车会跳转打开链接,所以空格回车输出结果,但引入了一个多余的空格,导致最终结果多了一个"."
number[3:6:1],默认步长为1,可不写。
第一个索引包含在内,第二个索引不包含在内。
步长为正数时,从起点移动到终点;为负数时,从终点到起点,且第一个索引必须比第二个索引大。
3. 序列相加
print([1,2,3] + [7,8,9,10])
结果:
[1, 2, 3, 7, 8, 9, 10] print([1,2,3,4] + 'qwrrt')
结果:
Traceback (most recent call last):
File "D:/Python/PycharmProjects/untitled1/venv/Robots_learning.py", line 818, in <module>
print([1,2,3,4] + 'qwrrt')
TypeError: can only concatenate list (not "str") to list
使用加法运算拼接序列,但是,一般不能拼接不同类型的序列。
4. 乘法
将序列与数x相乘时,将重复这个序列x次来创建一个新的序列:
print('Python '* 5)
结果:
Python Python Python Python Python
None在Python中代表什么都没有;初始化。
# 在屏幕中央且宽度合适的方框内打印一个句子
sentence = input('Plz enter your sentence:') screen_width = 80
text_width = len(sentence)
box_width = text_width + 12
left_margin = (screen_width - box_width) // 2 print()
print(' '* left_margin + '+' + '-'*(box_width-2) + '+')
print(' '* left_margin + '|' + ' '*(box_width-2) + '|')
print(' '* left_margin + '|' + ' '*(((box_width - text_width)//2)-1) + sentence +' '*(((box_width - text_width)//2)-1)+'|')
print(' '* left_margin + '|' + ' '*(box_width-2) + '|')
print(' '* left_margin + '+' + '-'*(box_width-2) + '+')
print() 结果:
Plz enter your sentence:You'er beautiful! +---------------------------+
| |
| You'er beautiful! |
| |
+---------------------------+
5. 成员资格检测
检查特定值是否包含在序列中,使用运算符in,返回结果True,False,为布尔运算。
# 检查用户名和PIN码
database = [
['albert', ''],
['dilbert', ''],
['smith', '']
] username = input('User Name:')
pin = input('PIN code:') # 检查用户名和PIN码
if [username, pin] in database:
print('Access granted!')
else:
print('You don\'t have access to the database!') 结果:
User Name:Elon
PIN code:1232345
You don't have access to the database!
最新文章
- [转]CocoaPods安装和使用教程
- MTK 常见的编译命令
- animation 的属性一共有 6 个值,详细介绍在此
- [转]Redis实现分析
- django 注册、登录及第三方接口程序(4):扩展邮箱注册,登录,微博登录
- Install gocode
- 表结构导出到excel中
- Centos6.4 设置开机自动以某个非root用户启动脚本
- 【游戏框架】Phaser
- js判断一个变量是否为数组的解决方案
- python2.7学习记录
- 2018-2019-2 网络对抗技术 20165323 Exp3 免杀原理与实践
- Linux系统命令行整理
- Python函数部分(1)
- 关于jqGrid组件表格无法自适应宽度问题
- 【Beta阶段】第六次Scrum Meeting!
- Public key for ambari-server-2.4.2.0-136.x86_64.rpm is not installed 安装ambari报错总结
- Java中sleep方法和wait的详细区别
- Python3 实现(wxpy)用微信自动定时给朋友定时推广
- 【转】maven profile实现多环境打包
热门文章
- scrapy框架之items项目
- 汇编语言学习-Dos下的调试工具debug的使用教程
- JAVA基础知识|HTTP协议-两个特性
- 浏览器环境下的microtaks和macrotasks
- C语言和Python语言在存储变量方面的不同
- eclipse CDT Error: Program ";g++"; not found in PATH
- Colab 实用教程
- vue实现购物清单列表添加删除
- c++ string操作
- osg(openscenegraph).chm帮助文档