CSE 1010 Lecture Notes - Lecture 8: For Loop
CSE 1010 verified notes
8/12View all
Document Summary
Cse1010 - lecture 8 - for loops & ranges. For loops: used for iterating over a sequence. With the for loop you can execute a set of statements, once for each item in the sequence. For loop format: for variable in collection: body. The for loop automatically increments, as opposed to while loops. Therefore, for loops don"t require an indexing variable to be set beforehand. Example: nums = [10, 20, 30, 40] #list for n in nums: print(n) # takes each number out of nums and puts it in n. For loops can also iterate over strings since they contain a sequence of characters. The range function: to loop through a set of code a specific number of times we can use the range function. The range returns a sequence of numbers, starting from 0 by default, and increments by 1 by default as well, until it ends at a specified number.