python 打印列表元素_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 listprint (list[i]) # printing ith element of listprint (list[i], list[j]) # printing ith and jth elementsprint (list[i:j]) # printing elements from ith index to jth indexprint (list[i:]) # printing all elements from ith indexprint (list * 2) # printing list two timesprint (list1 + list2) # printing concatenated list1 & list2Python代碼打印列表元素 (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 & list2Output
輸出量
['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 打印列表元素
總結
以上是生活随笔為你收集整理的python 打印列表元素_Python程序以不同方式打印列表元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css div滚动_如何使用CSS创建可
- 下一篇: python字符串转浮点数_Python