栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 打印列表元素

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 打印列表元素

python 打印列表元素

In this program – we are going to learn how can we print all list elements, print specific elements, print a range of the elements, print list multiple times (using * operator), print multiple lists by concatenating them, etc.

在此程序中,我们将学习如何打印所有列表元素 ,打印特定元素,打印元素范围,多次打印列表(使用*运算符),通过将它们串联来打印多个列表,等等。

Syntax to print list in different ways:

以不同方式打印列表的语法:

    print (list)          	# printing complete list
    print (list[i])       	# printing ith element of list
    print (list[i], list[j])	# printing ith and jth elements
    print (list[i:j])     	# printing elements from ith index to jth index
    print (list[i:])      	# printing all elements from ith index
    print (list * 2)  		# printing list two times
    print (list1 + list2) 	# printing concatenated list1 & list2

Python代码打印列表元素 (Python code to print list elements )

Here, we have two lists list1 and list2 with some of the elements (integers and strings), we are printing elements in the different ways.

在这里,我们有两个列表list1和list2 ,其中包含一些元素(整数和字符串),我们以不同的方式打印元素。

# python program to demonstrate example of lists

# declaring & initializing two list
list1 = ["Amit", "Abhi", "Radib", 21, 22, 37]
list2 = [100, 200, "Hello", "World"]

print (list1)          		# printing complete list1
print (list1[0])       		# printing 0th (first) element of list1
print (list1[0], list1[1])	# printing first & second elements
print (list1[2:5])     		# printing elements from 2nd to 5th index
print (list1[1:])      		# printing all elements from 1st index
print (list2 * 2)  		# printing list2 two times
print (list1 + list2) 		# printing concatenated list1 & list2

Output

输出量

['Amit', 'Abhi', 'Radib', 21, 22, 37]
Amit
Amit Abhi
['Radib', 21, 22]
['Abhi', 'Radib', 21, 22, 37]
[100, 200, 'Hello', 'World', 100, 200, 'Hello', 'World']
['Amit', 'Abhi', 'Radib', 21, 22, 37, 100, 200, 'Hello', 'World']


翻译自: https://www.includehelp.com/python/print-list-elements-in-different-ways.aspx

python 打印列表元素

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号