Python 程序:将项目附加到列表

编写一个 Python 程序,将一个项目附加到列表中。在 Python 中,我们有多个选项可以向列表中追加或添加一个项目,这里,我们使用 append 方法。list append 方法在列表的末尾添加一个新元素。

【追加(项)

numbers = [10, 20, 30, 40, 50]

print("Numeric List Before Appending Item")
print(numbers)

numbers.append(65)

print("Numeric List After Appending First Item")
print(numbers)

value = int(input("Please enter the List Item = "))
numbers.append(value)

print("Numeric List After Appending Second Item")
print(numbers)

这个 python 程序使用索引函数将一个项目附加到列表中。list index 方法在指定的索引处添加一个 list 元素。

list.insert(索引,项目)

countries = ["India", "USA", "UK", "Italy"]

print("List Before Appending Item")
print(countries)

countries.insert(3, "Japan")
print("\nList After Appending Japan at 3rd Index Position")
print(countries)

countries.insert(0, "China")
print("\nList After Appending China at 0 Index Position")
print(countries)

countries.insert(6, "France")
print("\nList After Appending France at 6 Index Position")
print(countries)
List Before Appending Item
['India', 'USA', 'UK', 'Italy']

List After Appending Japan at 3rd Index Position
['India', 'USA', 'UK', 'Japan', 'Italy']

List After Appending China at 0 Index Position
['China', 'India', 'USA', 'UK', 'Japan', 'Italy']

List After Appending France at 6 Index Position
['China', 'India', 'USA', 'UK', 'Japan', 'Italy', 'France']

这个 Python 示例允许输入列表索引和列表值,并使用索引方法添加新项目。

numbers = [11, 22, 33, 44, 55, 66]

print("Numeric List Before Inserting Item")
print(numbers)

index = int(input("Please enter Index Position = "))
value = int(input("Please enter the List Value = "))

numbers.insert(index, value)

print("Numeric List After Appending Item")
print(numbers)
Numeric List Before Inserting Item
[11, 22, 33, 44, 55, 66]
Please enter Index Position = 4
Please enter the List Value = 111
Numeric List After Appending Item
[11, 22, 33, 44, 111, 55, 66]

list extend 方法将列表、元组、字符串等任何可条目添加到现有列表的末尾。

list.extend(迭代器)

countries = ["India", "USA", "UK", "Italy"]

print("List Before Appending Item")
print(countries)

tuple1 = ("Japan", "China", "France")
countries.extend(tuple1)
print("\nList After Appending Tuple using extend")
print(countries)

list1 = [19, 17, 39, 55]
countries.extend(list1)
print("\nList After Appending List sing extend")
print(countries)

countries.extend((11.5, 19.2))
print("\nList After Appending 11.5, 19.2 using extend")
print(countries)
List Before Appending Item
['India', 'USA', 'UK', 'Italy']

List After Appending Tuple using extend
['India', 'USA', 'UK', 'Italy', 'Japan', 'China', 'France']

List After Appending List sing extend
['India', 'USA', 'UK', 'Italy', 'Japan', 'China', 'France', 19, 17, 39, 55]

List After Appending 11.5, 19.2 using extend
['India', 'USA', 'UK', 'Italy', 'Japan', 'China', 'France', 19, 17, 39, 55, 11.5, 19.2]

原创文章,作者:BYZG7,如若转载,请注明出处:https://www.506064.com/n/126887.html

相关推荐

  • 用Python编写Web3.py库,轻松为您的DApp实现无缝连接

    一、什么是Web3.py库 Web3.py库是一个由Python编写的用于与以太坊通信的库。它允许开发者与以太坊节点交互,并能够实现广泛的功能,从简单的读取智能合约状态到发起交易或…

    编程 2024-12-27
  • Python神器:最大数计算

    在日常生活和工作中,需要使用计算机进行一些最大值的计算。比如要找到一个文件夹中最大的文件,或者计算一段序列中的最大值。这时就需要使用一些算法和工具来实现最大数计算。 一、内置函数m…

    编程 2024-12-22
  • 在js中调用python模块,有js基础学python

    本文目录一览: 1、js调用代码 js能调用python里的函数吗 2、如何在js上运行Python脚本?或者,js怎么调用.py文件? 3、Node.js运行Python脚本 4…

    编程 2024-12-07
  • Python类型元组的用法和示例

    一、元组的定义和访问 元组是Python中的一种序列类型,由若干个逗号分隔的值组成,可以包含任意类型的元素。与列表不同,元组在定义后不可修改,但可进行索引、切片、遍历等操作。下面是…

    编程 2025-01-11
  • Python acosd函数:计算反余弦值的快捷方式

    一、背景介绍 在数学中,反三角函数是一类用于计算与三角函数互为反函数的函数。其中,反余弦函数是常用的反三角函数之一,表示对于一个三角函数的值,求解其所对应的角度值。Python提供…

    编程 2024-12-29
  • 强化Python多线程编程的技巧

    Python是一种面向对象、解释型的高级编程语言。随着计算机发展,越来越多的应用需要采用多线程的方式进行,通过并行处理提高程序效率。Python在多线程方面也有着优秀的表现,这篇文…

    编程 2024-12-24
  • 详解VSCode函数列表

    VSCode是一个功能强大且广受欢迎的代码编辑器。其中一个重要的功能就是函数列表。VS Code 函数列表显示当前打开文件(或当前工作区中所有文件)中定义的所有函数。您可以从列表中…

    编程 2024-12-20
  • 进入和退出python的命令,命令行如何退出python

    本文目录一览: 1、python怎么运行 2、退出python环境应该输入什么 3、win10命令行怎么退出python python怎么运行 python运行的具体步骤: 工具:…

    编程 2024-12-01
  • 使用Python的if和else进行条件判断控制流程

    一、引言 在Python中,if和else语句是控制流程中最常用的条件判断结构。with语句用于处理文件、sockets、数据库等资源的管理。 在本文中,我们将详细介绍使用Pyth…

    编程 2024-12-12
  • 使用Python 3正则表达式进行文本匹配和替换

    正则表达式是一种用来匹配字符串的模式,Python 3提供了re模块来支持正则表达式操作。使用正则表达式可以在文本中快速定位和替换指定内容,提高效率。 一、正则表达式介绍 正则表达…

    编程 2024-12-22