] for x in Marks: print( x ) Output Example In the above example, Marks list has elements; each element is indexed from to . The Python reads the for loop and print statements like English: “For (every) element (represented as x) in (the list of) Marks and print (the values of the) elements”. . .
Changing list elements In Python, the lists are mutable, which means they can be changed. A list element or range of elements can be changed or altered by using simple assignment operator =. Syntax: [index of an element] = Value to be changed [index from : index to] = Values to changed Where, index from is the beginning index of the range; index to is the upper limit of the range which is excluded in the range. For example, if you set the range [ : ] means, Python takes only to as element index.
Thus, if you want to update the range of elements from to , it should be specified as [ : ]. 12th Computer Chapter - - MyList = [ , , , , ] print ("MyList elements before update... ") for x in MyList: print (x) MyList[ ] = print ("MyList elements after updation... ") for y in MyList: print (y) Output: MyList elements before update...
MyList elements after updation... Example . : Python program to update/change single value MyList = [ , , , , ] print ("List Odd numbers... ") for x in MyList: print (x) MyList[ : ] = , , , , print ("List Even numbers...
") for y in MyList: print (y) Output List Odd numbers... List Even numbers... Example . : Python program to update/change range of values 12th Computer Chapter - - Lists, Tuples, Sets and Dictionary .
. Adding more elements in a list In Python, append() function is used to add a single element and extend() function is used to add more than one element