LIST OPERATIONS =============== Traversing on List data: ======================== moving on the list element by element ==> traversing ex: [1,2,3,4,5,6] ==> all odd elements with list ==> [1,3,5] for traversing: 1) using while loop 2) using for loop 1) using while loop: ==================== Syntax: initialization (index for start traversing) while index < length-list/number: loop body operation update len(): ===== It is an inbuilt method, which we can use to find the size or length or total number of elements of the string. Syntax: len(str-data) # Traversing on List using while loop # WAP TO PRINT ALL THE LIST ELEMENTS WITH CORRESPONDING POSITIVE AND NEGATIVE INDEX VALUE. """ input: "python" Output: the character at positive '0' = 'p' the character at negative index '-1' = 'n' the character at positive index '0' and at negative index '-6' = 'p' """ # traversing with positive index values strData = "Python" length = len(strData) # print("The Length of the string = ",length) index = 0 while index < length: # print("The Character at positive index {} is = {}".format(index,strData[index])) print("The Character at {} and at {} is = {}".format(index,index-length,strData[index])) index = index + 1 print() # traversing with negative index values index = -1 while index >= -length: # print("The Character at negative index {} is = {}".format(index,strData[index])) print("The Character at {} and at {} is = {}".format(index,index + length,strData[index])) index = index - 1 ========================================= # List Traversing using while loop listData = eval(input("Enter a list:")) length = len(listData) index = 0 while index < length: print("Element of list at {} and at {} is = {}".format(index, index-length,listData[index])) index += 1 print() index = -1 while index >= -length: print("The Element aof list at {} and at {} is = {}".format(index,index+length,listData[index])) index -= 1 ==================================================== 2) using for loop: ================== Syntax: for iter_var in range(): block operation for iter-var in list-data: block of operation # List Traversing using for loop listData = [1,3,5,7,9] index = 0 length = len(listData) # in forward access for ele in listData: print("The Element at {} and at {} is = {}".format(index,index-length,ele)) # print(ele,end = "\t") index += 1 print() # in reverse access index = -1 for ele in listData[::-1]: print("The Element at {} and at {} is = {}".format(index,index + length,ele)) index -= 1 ================================================= Math Operations on List: ======================== 1) LIST CONCATENATION ====================== Joining of two or more list data's into single list. ==> symbol: + Syntax: list1 + list2 + list3 + ..... 2) LIST REPETITION =================== To repeat the content of the list for several number of times, we can use "list Repetition". Symbol: * Syntax: list-data * n # List Math Operations # List Concatenation l1 = [1,3,5,7,9] l2 = [0,2,4,6,8] res_list = l1 + l2 # List Repetition list_repeat = l1 * 5 print("The Concatenated list = ",res_list) print("Repeated List = ",list_repeat) ======================================== Membership Check on List: ========================= we can use: membership operators in and not in # Membership check l1 = [1,3,5,7,9,'python','object','oriented', 'language'] print('python' in l1) # True print('object' not in l1) # False print(7 in l1) # True print(9 not in l1) # False ======================================== List Comparison: ================ we can use: Relational Operators list1 == list2 or list1 != list2 ================================= 1) Check lengths of the list: if lengths of both lists are same, it will consider for individual element check if lengths of both lists are not same, equality ==> False # Equality Check on list l1 = [1,2,3,4,5] l2 = [1,2,3,4,5] l3 = [1,3,5,7,9] l4 = [1,2,3,4] print(l1 == l2) print(l1 == l3) print(l1 == l4) print(l1 != l2) print(l1 != l3) print(l1 != l4) l1 < l2 S2 | l1 <= l2 | l1 >= l2 ======================================== 1) return: False/True (based on the lengths) when lists are with different lengths 2) the comparison is defined on the list element by element, when the lengths are same l1 = [1,2,3,4,5] l2 = [1,2,3,4,5] l3 = [1,3,5,7,9] l4 = [1,2,3,4] print(l1 < l2);print(l1 > l2) print(l1 <= l2);print(l1 >= l2) print(l2 < l4);print(l2 > l4)