List and Scope

List and scope

Whta is Data structure ?

ans: Providing structure to the data is called data structure.

 



print the whole list

 
    arr = [5, 12,"Tajmahal",45.77,True,"Tajmahal"]
    print(arr)
 
    output:
    [5, 12, 'Tajmahal', 45.77, True, 'Tajmahal'] 




print the length of index

 
    arr = [5, 12,"Tajmahal",45.77,True,"Tajmahal"]
    print(len(arr))
 
    output: 6




Access 2 and 5 index

 
    arr = [5, 12,"Tajmahal",45.77,True,"Tajmahal"]
    print(arr[2], arr[5])
 
    output: Tajmahal Tajmahal
 





Check the data type of "arr"

 
    arr = [5, 12,"Tajmahal",45.77,True,"Tajmahal"]
    print(type(arr))
     
    OUTPUT: <class 'list'> 

 
 



Create an empty list

 
    empty = []
    print(empty)
 


Another way to create an empty list.


## Another way to create an empty list.
list_name = list() # This is also a empty list.
 




Slicing

 
    slr = [2, 5, 7, 8,2,100,"Angela Yu","London",43.55]
    print(slr[2], slr[3])
 
    output: 7 8
 



Print whole list using range function

 
    slr = [2, 5, 7, 8,2,100,"Angela Yu","London",43.55]
 
    for i in slr:
    print(i,end = " ")
     
    output: 2 5 7 8 2 100 Angela Yu London 43.55
 
 


Print 2 to 9 elements from slr list

 
    slr = [2, 5, 7, 8,2,100,"Angela Yu","London",43.55]
     
    print(slr[2:9])
 
    OUTPUT: [7, 8, 2, 100, 'Angela Yu', 'London', 43.55]
 



HOW IT WORKING UNDERSTAND

 
    slr = [2, 5, 7, 8,2,100,"Angela Yu","London",43.55]
    print(slr[1:10:2])
 
    OUTPUT: [5, 8, 100, 'London']
 


print(slr[1:10:2])

1 is a starting number

10 is an ending number

2 is a step size 

 

 

print elements which are on even index.

     
    lst = [2,3,5,6,7,8,96,42,35,21,234,554,433]
 
    
for i in range(len(lst)):
if i % 2 == 0:
print(lst[i])
 
 
    output:
    2
    5
    7
    96
    35
    234
    433 

 
 


 

Another list example:

 
 names = ["Angela","Elon Musk","Jarvis","Linux","Telegram","python"]

for indx in range(5):
print("Hello", names[indx])
 
 
    OUTPUT: 
 
    Hello Angela Yu
    Hello Elon Musk
    Hello Jarvis
    Hello Linux
    Hello Telegram
 
 






MCQ 1

 
    lst = [1,2,3,4,5,6,7,8,9,10]
print(lst[0:len(lst):2])

# output: 1,3,5,7,9
 

0 is a starting number
len(lst) is 10
2 is a step size.
 or
print(lst[0:10:2])



MCQ: 2

 

 
    a = [1,2,3,4]
print(a[len(a)-1:0:-1 ])

## len(a)-1 = 3         (a[3:0:-1])
 
    output: [4, 3, 2]
 


It will print whole list

 
    a = [1,2,3,4]
print(a[0:len(a):1])
 
     OUTPUT: [1, 2, 3, 4]
 



MCQ:3


aa = [1,2,3,4,5]
aa.append(15)
print(aa[-1])

# output: 15
 



MCQ: 4

 
    i = [1,2,3,4]
i.insert(2,15)
print(i[2])

# output: 15
 

 

 



Append function

 
    box = [3,4,5,6,7,9]

# Adding an elements to the end using append function.
# append does not want index
box.append("Linux Debian")
    print(box)
 
    output: [3, 4, 5, 6, 7, 9, 'Linux Debian']
 




Insert function

 
    box = [3,4,5,6,7,9]
box.insert(4, "Dream not sleep")
print(box)
 
    output: [3, 4, 5, 6, 'Dream not sleep', 7, 9]
 



Negative indexing

 
    py = [1,2,3,4,5,6,78,9]
print(py[-1]) # output: 9
print(py[-3]) # output: 6
print(py[-2]) # output: 78
 




Others



py = [1,2,3,4,5,6,78,9]

# it will print from 0 to 6
print(py[:6]) # same as print(py[0:6])


# it will print from 2s and go till the end.
print(py[2:]) # same as print(py[2:8])


# it will print whole the elements (start from beginning and go till the end)
print(py[:]) # same as print(py[0:8])

# step size is 2
print(py[::2])  
 
    # output: [1, 3, 5, 78]
 

# it will start from beginning and go to -1
print(py[:-1]) 
     
     # output: [1, 2, 3, 4, 5, 6, 78]


# reversing the list
print(py[::-1])  
 
    # output: [9, 78, 6, 5, 4, 3, 2, 1]









Post a Comment (0)
Previous Post Next Post