Python Lists
List:
A list is a sequence of mutable python object like
float, string etc.
It defined using Square braces[].
my_list=[‘python’, 2.7,5]
Concatenation: Adding two or more lists by using + operator
#list
list1=['1','a']
list2=['b']
#concatenation
print(list1+list2)
Output:
['1', 'a', 'b']
Repetition: Using * operator, it repeats lists in given number times.
list follows * and specify the how many times repeat the given lists.
Example:
‘python’*2= python python
#list
list1=['1','a']
#repetition
print(list1*2)
Output:
['1', 'a', '1', 'a']
Slicing: using : colon operation, we can access the range of item in a lists.
#scling
list4=['a','b','c','d','e']
print(list4[1:2])
Output:
['b']
Indexing:
#indexing
list4=['a','b','c','d','e']
print(list4[1])
Output:
a
We already know concatenation, repetition ,slicing and Index. So, will learn other operations and functions in the list.
Example Program
Output:
['1', 'a', 'b']
['1', 'a', '1', 'a']
['b']
a
['a', 'b', 'c', 'd', 'e', 'f']
['a', 'b', 'c', 'd', 'e', 'f', '1']
['a', 'b', 'c', 'd', 'e', 'f', '1', 'h']
['a', 'b', 'c', 'd', 'e', 'f', '1']
In above example program,
Append () function:
#append
list3=['a','b','c','d','e']
list3.append('f')
print(list3)
Output: ['a', 'b', 'c', 'd', 'e', 'f']
Add/ Attach a value at last of the list. By using append() , adds ‘f’ in
the “list3” variables.
In list3 have 5 values, after using append function ‘f’ value add to
the list3.
I hope, you will understand it.
Extend ():
Extend is used for adding multiple variable in the
list.
Extend Operator, how it works. You are specifying whatever value
you want in square brackets [].
#Extend
list=[1,2]
list.extend([‘c’,’d’])
print(extend)
Insert (index, value):
Insert operator is used to add a value at specific
index.
Example:
List=[1,2,4] --> list.index(2,3)--> [1,2,3,4]
Pop()
Pop function used to delete a value from the list.
list=[‘a’,’b’,’c’] --> list.pop()-->[‘a’,’b’]
It removes one by one.
You can also remove specific value. Let’s see
list=[1,2,3] --> list.pop(2)-->[1,2]
If you have any quires, please leave a comment or Contact form
Thank you guys π☺π :)
#Python List