for loops python' for-loop syntax is more convenient alternative to while loop when iterating through series of elements the for-loop syntax can be used on any type of iterable structuresuch as listtuple strsetdictor file (we will discuss iterators more formally in section its general syntax appears as follows for element in iterablebody body may refer to element as an identifier for readers familiar with javathe semantics of python' for loop is similar to the "for eachloop style introduced in java as an instructive example of such loopwe consider the task of computing the sum of list of numbers (admittedlypython has built-in functionsumfor this purpose we perform the calculation with for loop as followsassuming that data identifies the listtotal for val in datatotal +val note use of the loop variableval the loop body executes once for each element of the data sequencewith the identifiervalfrom the for-loop syntax assigned at the beginning of each pass to respective element it is worth noting that val is treated as standard identifier if the element of the original data happens to be mutablethe val identifier can be used to invoke its methods but reassignment of identifier val to new value has no affect on the original datanor on the next iteration of the loop as second classic examplewe consider the task of finding the maximum value in list of elements (againadmitting that python' built-in max function already provides this supportif we can assume that the listdatahas at least one elementwe could implement this task as followsbiggest data[ for val in dataif val biggestbiggest val as we assume nonempty list although we could accomplish both of the above tasks with while loopthe for-loop syntax had an advantage of simplicityas there is no need to manage an explicit index into the list nor to author boolean loop condition furthermorewe can use for loop in cases for which while loop does not applysuch as when iterating through collectionsuch as setthat does not support any direct form of indexing