While vs for while loop increment an index for

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: f integers i,i +1,i+2, ….,j-1 •  i and k are op\onal parameters 8 13 ­09 ­23 Range Func\on and For Loops •  The first argument i specifies the star\ng number of the sequence. •  If i is not specified, then the sequence will start from 0. •  The third argument k specifies the “step”, so each number in the sequence will increase by k. •  If k is not specified, then the step will be 1. The range() func9on • Range returns an iterable object, which means something we can iterate over like a list • It is not a list, but we can treat it like one in our for loop The range() func9on • We can skip elements in the sequence too • Example: skipping by 2 9 13 ­09 ­23 What is the output? for count in range(3): print(count) for count in range(6,5,1) print (count) for count in range(1,6,2): print(count) for count in range(4,1,-1) print(count) Range Func\on and List •  The range func\on can be used to fill out a list. while vs for •  while loop: increment an index •  For loop: iterate over the elements 10 13 ­09 ­23 To index or not to index? •  For searching either way is fine –  In this case, we’re searching for the...
View Full Document

Ask a homework question - tutors are online