Resource 20250120143018 Python Intro(Datatype,Operator,Variable)
Resource 20250120143018 Python Intro(Datatype,Operator,Variable)
1) Using append()
method - append()
method only works for
addition of elements at
the end of the List.
ADDING ELEMENT TO A LIST
2) Using insert() method – Used for addition of element at the desired position.
• Unlike append()
which takes only
one argument,
insert() method
requires two
arguments
(position, value).
ADDING ELEMENT TO A LIST
3) Using extend() method- this method is used to add multiple elements at the
same time at the end of the list.
REMOVING ELEMENTS FROM A LIST
Elements from a list can removed using two methods :
1) remove() method -Elements can be removed from the List by using built-in
remove() function but an
Error arises if element
doesn't exist in the set.
• It only removes one
element at a time, to
remove range of
elements, iterator is
used.
• Remove method in List
will only remove the
first occurrence of the
searched element
REMOVING ELEMENTS FROM A LIST
2) Using pop() method - Pop() function can also be used to remove and return
an element from the set, but by default it removes only the last element of the
set.
• To remove an
element from a
specific position of
the list, index of the
element is passed as
an argument to the
pop() method.
SLICING OF A LIST
In Python List, there are multiple ways to print the whole List with all the
elements, but to print a specific range of elements from the list, we use Slice
operation. Slice operation is performed on Lists with the use of colon(:)
SLICING OF A LIST
• To print elements from beginning to a range use [:index]
• To print elements from end use [:-index],
• To print elements from specific index till the end use [index:],
• To print elements within a range, use [start index: end index]
• To print whole list with the use of slicing operation, use [:].
• To print whole list in reverse order, use [::-1].
• To print elements of list from rear end, use negative indexes.
# Creating a List Output
List= ['G','O','O','D','M','O', 'R','N','I','N','G'] Initial List: ['G','O','O','D','M', 'O',
print("Initial List: ") 'R','N','I','N','G']
print(List)
# using Slice operation Slicing elements in a range 3-8: ['D', 'M',
Sliced_List = List[3:8] 'O', 'R', 'N']
print("\nSlicing elements in a range 3-8: ")
print(Sliced_List)
SLICING OF A LIST
# Print elements from a Elements sliced from 5th element
# pre-defined point to end till the end: ['M', 'O', 'R', 'N', 'I', 'N', 'G']
Sliced_List = List[5:] print("Elements sliced from
5th element till the end: ")
print(Sliced_List)
# Printing elements from # beginning till end Printing all elements using slice
Sliced_List = List[:] print("\nPrinting all elements operation:
using slice operation: ") print(Sliced_List) ['G','O','O','D','M', 'O', 'R','N','I','N','G']
# Print elements from beginning # to a pre Elements sliced till 6th element from
defined point using Slice last: ['G','O','O','D','M', 'O']
Sliced_List = List[:-6]
print("\nElements sliced till 6th element from last:
")
print(Sliced_List)
SLICING OF A LIST
# Print elements of a range # using negative index Elements sliced from index -6 to -1
List slicing ['R', 'N', 'I', 'N', 'G']
Sliced_List = List[-6:-1]
print("\nElements sliced from index -6 to -1")
print(Sliced_List)
# Printing elements in reverse Printing List in reverse:
# using Slice operation ['G','N','I','N','R', 'O', 'M','D','O','O','G']
Sliced_List = List[::-1]
print("\nPrinting List in reverse: ")
print(Sliced_List)
RECAP
Write a python program to implement list operations (add, append, extend &delete)
Write a python program to implement list operations (Nested List, Length,
Concatenation, Membership, Iteration, Indexing and Slicing)?
my_list= ['p', 'r','b', 'e']
print (my_list[0])
print ("Length", my_list, ":",len (my_list))
print (my_list [2])
print (my_list [3])
n_list=[[1,3,5,7],[2,4,6,8,10]]
print (n_list[0][1])
print (n_list[1][3])
n_list2 = ["Happy", [2,0,1,5]]
print (n_list2 [0][1])
print (n_list2 [1][3])
concat1=[1,3,5]
concat2=[7,8,9]
print ("Concatenation: ", concat1,concat2, ":",
concat1+concat2)
repetion_string="Hello"
print ("Repetition: ", repetion_string * 4)