CISC 101 Lecture Notes - Lecture 16: For Loop

55 views3 pages

Document Summary

Lesson 16 for loop and range() function. Lookup funcion set() it will make life easier, super powerful. The for statement is a convenient way to iterate over sequences for statement. Example use for k in sequence: nums = [10, 20, 30, 40, 50, 60, 70] suite for k in nums: print(k) Variable k is referred to as a loop variable. Prining numbers in a sequence nums, using while loop. In the while loop version, loop variable k must be iniialized to 0 and incremented by 1 each ime through the loop. In the for loop version loop variable k automaically iterates over the provided sequence of values: Three things every loop must have: staring point. When you have to stop in the middle of the list. You cannot terminate the for loop in the middle whereas with a while loop you can add a boolean condiion. Someimes you iterate unil you ind something, not interested in all elements.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents