Python program to print list elements in different ways

Printing list components in Python: Here, we will figure out how to print list components in various manners?

In this program – we will figure out how might we print all rundown components, print explicit components, print a scope of the components, print list on various occasions (utilizing * administrator), print numerous rundowns by linking them, and so on.

Linguistic structure to print list in various manners:

    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 code to print list components:

Here, we have two records list1 and list2 with a portion of the components (integers and strings), we are printing components in the various manners.

# python program to demonstrate example of lists

# declaring & initializing two list
list1 = ["Kishan", "Durgesh", "Aakash", 19, 21, 20]
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:

['Kishan', 'Durgesh', 'Aakash', 19, 21, 20]
Kishan
Kishan Durgesh
['Aakash', 21, 22]
['Durgesh', 'Radib', 21, 22, 37]
[100, 200, 'Hello', 'World', 100, 200, 'Hello', 'World']
['Kishan', 'Durgesh', 'Aakash', 21, 22, 37, 100, 200, 'Hello', 'World']

Leave a Comment

error: Alert: Content is protected!!