Python
TUPLE
Fill in the blanks
1.a=(“PYTHON”).type(a)=   <class ‘tuple’>
2.b=(‘p’,’y’,’t’,’h’,’o’,’n’).print(b[3])=   h  
3. b=(‘p’,’y’,’t’,’h’,’o’,’n’).print(b[-5])=  y   
4.a=((1,2,3),(4,5,6),(7,8,9)).print(a[1][2]) = 6  
5.t=(‘p’,’y’,’t’,’h’,’o’,’n’).t[2:2]=   ()   
6.t1=(30,40,50).t2=(10,15,20).print(t1+t2)= (30,40,50,10,15,20)
7.We can change Tuple value.[T/F].  False
8.a=(‘H’,’E’,’L’,’L’,’O’,’W’,’O’,’R’,’L’,’D’).count(‘L’)=3
9. a=(‘H’,’E’,’L’,’L’,’O’,’W’,’O’,’R’,’L’,’D’).index(‘E’)=1
10.tup=(‘p’,’y’,’t’,’h’,’o’,’n’).print(‘o’ not in tup)=   False
11.s=(2,4,6,8).print(s.insert(0,1))  =   (0,2,4,6,8)
12.x=(2,5,9,1,4).print(x.remove(9))  =  (2,5,1,4)
13.a=(4,7,8,1,2,9). Print(a.pop())=  (4,7,8,1,2)
14. s=(1,2,3,4,5,6). S.reverse()   =  (6,5,4,3,2,1)
15.a=array(‘c’,[‘a’,’e’,’i’,’o’,’u’]).a.tostring()   = aeiou
16.deleting a tuple entirely is possible using the keyword del.
Answer the following Question
                                                                    (4 marks)
1.Write Tuple Built-in Function.
creation , Repetition,
concatenation
> > > t = (['xyz' 123],23,
-103.4)
> > > t
 (['xyz' 123],23, -103.4)
> > > t * 2
(['xyz' 123],23, -103.4 , ['xyz'
123],23, -103.4)    
> > > t = t + ('free','easy')
> > > t
(['xyz' 123],23, -103.4 ,
'free','easy')
Membership, slicing
> > > 23 in t
1
> > > 123 in t
0
> > > t[0][1]
123
> > > t[1:]
(23,-103.4,'free','easy')
Built - in Function
> > > str(t)
['xyz' 123],23, -103.4 'free','easy')
 > > > len(t)
5
> > > max(t)
'free'
> > > min(t)
-103.4
> > >cmp(t, (['xyz' 123],23,
-103.4, 'free','easy')
0
> > > list(t)
[['xyz' 123],23, -103.4, 'free','easy']
2.Write Tuple Slice, Negative index,Change or add element .
Negative index
>>>tup1=('P','Y','T','H','O','N')
>>>tup1
('P','Y','T','H','O','N')
>>>tup1[-1]
'N'
>>>tup1[-5]
'Y'
>>>tup1[-6]
'P'
>>> 
 
Slicing
> > > tup1 =
('P','Y','T','H','O','N')
> > > tup1[2:2]
()
> > > tup1[2:3]
('T')                    
 > > > tup1[3:5] 
     #only  3rd and 4th
('H', 'O')
> > > tup1[2:]   
       #from 2nd to end
('T', 'H', 'O', 'N')
> > > tup1[:3]   
        #Begining to 3rd element
('P','Y','T')
> > > tup1[:-2]   
     #last 2nd position to first element
('P','Y','T','H')
> > > tup1[-3:]   
     #from 3rd element to last element
('H', 'O', 'N')
> > > tup1[:]   
         #only : shows all elements
('P', 'Y', 'T', 'H', 'O', 'N')
 > > >
Changing a Tuple
Tuple cannot be
changed once it has been assigned.
#if you try to
change the values in tuple it comes error
> > > tup1 =
(10,'semester-1',(56,67,89))
> > > tup1[0] = 100
Trackback ( most recent call last)
         file
" <pyshell#221 >", line 1, in  <module>
          tup1[0]  =
100
TypeError: 'tuple' object dose no
support item assignment
#Tuple can be reassigned by again
variable declaration
> > > tup1 =
(100,'semester-1', (89,90,93))
#mutable element in the tuple can be
changed
 > > > tup1 =
(100,'semester-1', [89,90,93])
 > > >tup1[2][0] =99
> > > print(tup1)
(100,'semester-1',[99,90,93])
> > >
Tuple is similar to a list.The difference between the two is that we cannot change the element of a tuple once it is assigned.But List element can be changable.
Advantage of 'Tuple' over 'List':
* Tuple is hetrogeneous(different data type).But List homogeneous(similar data type).
*Tuple are immutable .Tuple is faster than the list.So there is a slight performance boost.
*If you have data that doesn't change, implementing it as tuple will guarantee that it remains write - protected.
Answer the following Question
                                                                   (10 marks)
1.Write a Tuple Method append(),extend(),insert(),remove(),pop(),clear(),index(),count(), sort(),reverse(),copy() function
Array - Efficient arrays of numeric
values
from array import *
my_array = array('i',[1,2,3,4,5])
for i in my_array:
    print(i)
OUTPUT
1
2
3
4
5
                                                                                     
> > > my_array[1]
2
> > > my_array[2]
3
> > > my_array[0]
1
> > >my_array.append(6)
> > > my_array
array('i', [1, 2, 3, 4, 5, 6])
 
> > > my_array.insert(0,0)
> > >
my_array                                                          
array('i', [0, 1, 2, 3, 4, 5, 6])
 
> > > my_extnd_array = array
('i', [7,8,9,10])
> > >my_array.extnd(my_extnd_array)
> > >my_array
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10])
 
> > > c = [11, 12, 13]
> > >my_array.fromlist(c)
> > >my_array
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13])
 
> > >my_array.remove(13)
> > >my_array
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12])
 
> > >my_array.pop()
12
> > >my_array
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11])
 
> > >my_array.index(5)
5
 
> > > my_array.reverse()
> > >my_array
array('i', [11, 10, 9, 8, 7, 6, 5, 4,
3, 2, 1, 0])
 
> > > my_array.buffer_info()
(33881712,12)
 
> > >my_array.count(11)
1
> > > my_char_array =
array('c',['a','e','i','o','u'])
> > > my_char_array
array('c','aeiou')
> > > my_char_array.tostring()
'aeiou'
 
> > > c = my_array.tolist()
 >>>c
[11,10,9,8,7,6,5,4,3,2,1,0]
No comments:
Post a Comment