美文网首页
第2章 列表和元组

第2章 列表和元组

作者: 有斧头的IceBear | 来源:发表于2022-03-26 23:28 被阅读0次

代码清单2-1 索引操作示例

# Print out a date, given year, month, and day as numbers

months = [
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
]

# A list with one ending for each number from 1 to 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-31): ')

month_number = int(month)
day_number = int(day)

# Remember to subtract 1 from month and day to get a correct index
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]

print(month_name + ' ' + ordinal + ', ' + year)

运行结果如下:

Year: 2022
Month  (1-12): 10
Day (1-31): 21
October 21st, 2022

代码清单2-2 切片操作示例

# Split up a URL of the form http://www.something.com

url = input('Please enter the URL:')
domain = url[11:-4]

print("Domain name: " + domain)

运行结果如下:

Please enter the URL:http://www.python.org
Domain name: python

代码清单2-3 序列(字符串)乘法运算示例

# Prints a sentence in a centered "box" of correct width

sentence = 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()

运行结果如下:

Sentence: He's a very naughty boy!

                         +----------------------------+
                         |                            |
                         |  He's a very naughty boy!  |
                         |                            |
                         +----------------------------+

代码清单2-4 序列成员资格示例

# Check a user name and PIN code

database = [
    ['albert',  '1234'],
    ['dilbert', '4242'],
    ['smith',   '7524'],
    ['jones',   '9843']
]

username = input('User name: ')
pin = input('PIN code: ')

if [username, pin] in database: print('Access granted')

运行结果如下:

User name: smith
PIN code: 7524
Access granted

相关文章

  • 第三章

    列表,元组和字典 列表和元组 通过索引使用元素 通过索引获取子序列 列表和元组支持的运算 列表和元组的长度、最大值...

  • Python笔记re-day2 python编程基础(2)

    【本次学习主要参照B站孙兴华的视频教学整理】 re-day2 包含:第03课列表和元组 第03课列表和元组 一、列...

  • Python入门:元组

    六、元组 6.1 定义元组 元组和列表相似,列表是[---],元组是(---) 6.2 访问元组 6.3 修改元组...

  • 2018-01-13 python学习第一天

    第二章 列表和元组 列表和元组区别:列表可以修改,二元组则不能 python的6种內建的序列: 列表和元组,字符串...

  • Python元组

    元组 元组和列表相似,不同点元组定义后不能改变,列表可以做改变。 元组用小括号,列表用中括号。 列表常用操作 打印...

  • Python3 的数据类型3-元组

    元组和列表相似,但元组的元素值不能改变,而列表可以改变。元组用(),列表用[] 1. 元组的创建 元组直接用小括号...

  • Python基础教程Ch2-列表和元组

    第2章 列表和元组 ![](http://picture-repository-of-heamon7.qiniud...

  • 7元组、序列[python基础]

    元组(Tuple)的读写操作 元组就是不可变的列表*元组使用小括号,列表使用中括号 元组的读取方式和列表相同 创建...

  • python -------元组

    元组的定义 元组和列表相似,不过元组不可修改,元组和列表一样具有有序性,可以有多个元素组成。 如何创建元组 元组的...

  • Python程序员去面试,这几个问题一定要能回答!

    面试题以基础为主,去面试Python工程师,这几个基础问题不能答错 第1题:列表和元组有什么不同? 列表和元组是P...

网友评论

      本文标题:第2章 列表和元组

      本文链接:https://www.haomeiwen.com/subject/aedejrtx.html