Tuples and more iterables

Tuples and more iterables

 

Print index number using Element

It's show the index number 9 of the list element 99.

 
    A = [2,3,4,5,6,7,8,9,0,99]
    print(A.index(99))
      
    # 9 is an index number.
 
    output: 9
 
 

 

It's show the index number 5 of the list element 7. 

 
 
    A = [2,3,4,5,6,7,8,9,0,99]
    print(A.index(7))
    
    # 5 is an index number.
    output: 5
 
 

 

 

It's show the index number 6 of the list element 8. 

 
    A = [2,3,4,5,6,7,8,9,0,99]
print(A.index(8))
     
    output: 6  
 
 
 

 

 

 

Print Element using index number


It print 543 using index number 4

 
    lst = ["Angela Yu","Google",77,000,543]
print(lst[4])
 
     output: 543
 
 

 

It print Google using index number 1 

 
    lst = ["Angela Yu","Google",77,000,543]
print(lst[1])
     
    output: Google
 
 

 

 

 

it will print index 6 to 8 list elements


    lst = ["Angela Yu","Google",77,000,543,True,"Code","Developer","Linux"]

    print(lst[6:8])
 
     output: ['Code', 'Developer'] 

 

 

it will print index 6 to last elements in the list

 
 lst = ["Angela Yu","Google",77,000,543,True,"Code","Developer","Linux"]
print(lst[6:])
 
     output: ['Code', 'Developer', 'Linux']
 

 

it will print index 1st to 4 elements in the list

 
 
  lst = ["Angela Yu","Google",77,000,543,True,"Code","Developer","Linux"]
print(lst[:4])
 
  output: ['Angela Yu', 'Google', 77, 0]
 

 

 

pop function remove index 6 elements from the list

 
 lst = ["Angela Yu","Google",77,000,543,True,"Code","Developer","Linux"]

lst.pop(6)
print(lst)
 
  output: ['Angela Yu', 'Google', 77, 0, 543, True, 'Developer', 'Linux']
  
 
 

 

 

If no index number given, pop function remove last elements by default


lst = ["Angela Yu","Google",77,000,543,True,"Code","Developer","Linux"]

lst.pop()
print(lst)

   output: ['Angela Yu', 'Google', 77, 0, 543, True, 'Code', 'Developer'] 


 



Append function Add elements in the list

 
 lst = ["Angela Yu","Google",77,000,543,True,"Code","Developer","Linux"]

lst.append("cyber secu")
print(lst)
 
 output: ['Angela Yu', 'Google', 77, 0, 543, True, 'Code', 
          'Developer', 'Linux', 'cyber secu']
 

 

 

insert function Add elements in the list by providing index no with elements. 

 
 lst = ["Angela Yu","Google",77,000,543,True,"Code","Developer","Linux"]
lst.insert(2,"Hi")
print(lst)
 
output: ['Angela Yu', 'Google', 'Hi', 77, 0, 543, True, 
            'Code', 'Developer', 'Linux']

 

Note: pop function remove elements.


join Function


var = ['s','a','ee','d','A','m','y']
s = "$".join(var)
print(s)
     
    output: s$a$ee$d$A$m$y
 


 
    var = ['s','a','ee','d','A','m','y']
s = "".join(var)
print(s)
 
    output: saeedAmy




Note:

list Comprehenship = list Comprehenship means making things small.



Post a Comment (0)
Previous Post Next Post