Unit Iv
Unit Iv
ARIES
Lists:-
• A list is a sequence of values.
• They can be of any datatype.
• The values in a list are called elements or items.
• There are several ways to create a list
1.The simplest way is enclose the elements in square brackets([and ])
Example:-
Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)
2.A list that contains no elements is called empty list.
It can be created with empty brackets[].
Example:-
Create a List:
Empty=[]
3.A list contain elements of another list is called nested list.
Example:-
[“ Saran”,10,[15,25,35]]
Assigning list values to variables:-
• The list variables can be assigned to variables.
Example:-
>>>Icecreams=[‘vannila’’,strawberry’,’mango’]
>>>Age=[20,21,22,23,24,25]
>>>print(Icecreams,Age)
[‘vannila’’,strawberry’,’mango’] [20,21,22,23,24,25]
Accessing list elements:-
• The expression inside the brackets specifies the index.
• The index starts at 0
Example:-
>>>Icecreams=[‘vannila’,’strawberry’,’mango’]
>>> Icecreams[0]
‘vannila’
>>> Icecreams[1]
‘strawberry’
Negative indexing:-
• Python allows negative indexing for its sequences.
• The index -1 refers to the last item,-2 to next item and so on.
Example:-
>>>Icecreams=[‘vannila’,’strawberry’,’mango’]
>>> Icecreams[-1]
’mango’
List methods:-
append()
Adds a single item to the bottom of the list.
x = [1, 2]
x.append('h')
print(x)
Output:
[1, 2, 'h’]
Count()
The count method returns the number of times the items appear in the list.
x = [‘a’,’p’,’p’,’l’,’e’]
x.count(‘p')
print(x)
Output:
2
extend()
Adds another list to the end of a list.
x = [1, 2]
x.extend([3, 4])
print(x)
Output:
[1, 2, 3, 4]
insert()
Inserts a new element at a specific position in the list, this method receives the
position as a first argument, and the element to add as a second argument .
x = [1, 2]
x.insert(0, 'y')
print(x)
Output:
['y', 1, 2]
del()
• Deletes the element located in the specific index.
Program:-
x = [1, 2, 3]
del x([1])
print(x)
Output:
[1, 3]
len()
The len() returns number of elements in the list
Program:-
x = [1, 2, 3,4,5,6,7,8,9,10,21,22,23]
s=len(x)
print(s)
Output:
13
remove()
Removes the first match for the specified item.
x = [1, 2, 'h', 3, 'h']
x.remove('h')
print(x)
Output:
[1, 2, 3, 'h’]
reverse()
Reverses the order of the elements in the list, this places the final
elements at the beginning, and the initial elements at the end.
x = [1, 2, 'h', 3, 'h']
x.reverse()
print(x)
Output:
['h', 3, 'h', 2, 1]
Sort()
By default, this method sorts the elements of the list from smallest to largest,
this behavior can be modified using the parameter reverse = True.
Program1:
x = [3, 2, 1, 4]
x.sort()
print(x)
Output:
[1, 2, 3, 4]
Program2:
max()
The max() function returns the maximum value from the list.
Program:-
x = [1, 2, 3,4,5,6,7,8,9,10,21,22,23]
S=max(x)
print(S)
Output:
23
min()
The max() function returns the minimum value from the list.
Program:-
x = [1, 2, 3,4,5,6,7,8,9,10,21,22,23]
S=min(x)
print(S)
Output:
1
List Slices:-
• An individual element in list is called slice.
• Selecting a slice is similar to selecting an elements of a list.
Index between the elements:-
list=["a","b","c","d"]
temp=list
print(list)
print(temp)
temp.append("e")
print(list)
print(temp)
Output:-
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd', 'e']
Cloning list:-
• Cloning a list is to make a copy of the list itself.
• The easiest way to clone a list is to use the slice operator.
• The change made in the cloned list will not be reflected back in the original list.
Program:
list=["a","b","c","d"]
temp=list[:]
print(list)
print(temp)
temp.append("e")
print(list)
print(temp)
Output:
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd', 'e']
List parameters:-
• Passing a list as an arguments actually passes a reference to the list, not a copy
of the list.
• If a function modifies a list ,the caller see the changes.
Program:-
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
Output:-
apple
banana
cherry
Tuples:-
• A tuple is a sequence of values, which can be of any type and they are indexed
by integer.
• Tuples are an immutable sequence of elements. That is once a tuple is defined,
it cannot be altered.
• It is not necessary to enclose tuples in parenthesis.
• Example:
fruits = ("apple", "banana", "cherry“)
or
fruits = "apple", "banana", "cherry"
Creation of tuples:-
1.To create a tuple with a single element, include a final comma.
T1=‘banana’,
print(T1)
O/P
('banana',)
2.A tuple can be created using the built in function tuple. It can be an empty
tuple with no arguments.
T=tuple()
Print(T)
O/P
()
3.A tuple built in function can be used to create a tuple with sequence of arguments.
T=tuple('good students')
print(T)
O/P
('g', 'o', 'o', 'd', ' ', 's', 't', 'u', 'd', 'e', 'n', 't', 's’)
Operators on tuple:-
1.Bracket operator
The bracket operator indexes an element.
T=tuple('good students')
print(T[0])
print(T[1])
print(T[2])
print(T[3])
O/P:
g
o
o
Slice operator:-
The slice operator selects arrange of elements.
T=('good students')
print(T[5:10])
O/P:-
('s', 't', 'u', 'd', 'e’)
Concatenation operator:-
Tuples can be concatenated or joined them using + operator.
T1=('good')
T2=('students')
T3=T1+T2
print(T3)
O/P
goodstudents
Relational operators:-
The relational operators work with tuples and other sequences.
t1=(1,2,1,4)
t2=(1,2,2,4)
print(t1<t2)
O/P
True
t1=(1,2,3,4)
t2=(1,2,2,4)
print(t1<t2)
O/P
False
Looping through tuples:-
Tuple elements can be traversed using loop control statements.
The for loop is best to traverse tuple elements.
T1=('g','o','o','d')
for val in T1:
print(val)
O/P
g
o
o
d
Tuple Assignment
• It allows a tuple of variables on the left side of the assignment operator to be
assigned respective values from a tuple on right side.
• The number of variables on the left side should be same as the number of
elements in the tuple.
Program:-
T1=('g','o','o','d')
T2=('s','t','u','d','e','n','t','s')
print(T1,T2)
O/P
('g', 'o', 'o', 'd') ('s', 't', 'u', 'd', 'e', 'n', 't', 's')
Tuple as return values:-
Function can return more than one value.