What is the use of * operators in association with list in Python?

 What is the use of * operators in association with list in Python?

* is used as a multiplication operator in python.
for eg:-print(2*3)
            >>6

List in python are a set of elements of different data types.
for eg:- p=['a','d',1,2,2.4]
        
     print(p)
             >>
['a', 'd', 1, 2, 2.4]

How * operators in associated with the list ??
* operator repeats the entire list for n of times.


For eg:-The list p consist of elements different data types. To print the same list thrice we multiply the list with 3

Code:-

p=['a','d',1,2,2.4]
print("Repeat the list for 3 times", p*3)


Output:-


What is the output of print tiny_list * 2 if tiny_list = [123, 'john']?


Code:-

tiny_list = [123, 'john']
print(tiny_list * 2 )

Output:-

[123, 'john', 123, 'john']










Post a Comment

0 Comments