INFO5002-OCT10(10).pdf
Document Details
Uploaded by Deleted User
Full Transcript
10/17/24, 9:29 AM INFO5002-OCT10(10) In [ ]: For loops - (Iterable means an object that can be looped over, or iterated over, to return 1) iterate over a collection of items -> execute a block of...
10/17/24, 9:29 AM INFO5002-OCT10(10) In [ ]: For loops - (Iterable means an object that can be looped over, or iterated over, to return 1) iterate over a collection of items -> execute a block of code once for each 2) A general syntax : for in : block of code ** Request the next members of the iterable. ** if the iterable is empty, exit the for-loop WITHOUT running its body. ** if the iterable did produce a member, assign that member to ( if was not previously defined, it becomes defined) ** Execute the enclosed body of code ** Go back to the first step In : x = 0 for x in []: print("Hello!") print(x) 0 In : # Loop through a list primes = [2,3,5,7,9,11] for x in primes: print(x, end =' ') 2 3 5 7 9 11 In : # Loop through a str for x in "banana": print(x) b a n a n a In : # Use Loop to measure some strings: words = ['cat','window','defenestrate'] for w in words: print(w,len(w)) cat 3 window 6 defenestrate 12 In [ ]: The range () for for-loop The range() function - return a sequence of numbers "OBJECT not a LIST" -> it s (a) start from 0 by default (b) increments by 1 (by default) (c) end at a specified number Syntax : localhost:8888/nbconvert/html/INFO5002-OCT10(10).ipynb?download=false 1/8 10/17/24, 9:29 AM INFO5002-OCT10(10) range(stop) -> range object range(start,stop[,step]) -> range object In : range? In : for x in range(4): print(x) 0 1 2 3 In : for x in range(2,6): print(x)#2,3,4,5 2 3 4 5 In : for x in range(2,20,4): print(x) #2,6,10,14,18 2 6 10 14 18 In [ ]: The for loop: - len(a) returns the length of the list a, which is 5 in this case. - range(len(a)) creates a sequence of numbers from 0 to 4 (since len(a) is - The loop iterates over each index in this range (i takes the values 0, 1, - Inside the loop, a[i] accesses the element at index i in the list a. - print(i, a[i]) prints both the index and the corresponding element of the In : #Exercise : iterate over the indexs of a sequence: a = ["Emily","Amy","Sarah","Mya","."] for i in range(len(a)): print(i,a[i]) 0 Emily 1 Amy 2 Sarah 3 Mya 4. In : list(range(0,10,3)) #parenthesis () -> "list" [0, 3, 6, 9] Out: localhost:8888/nbconvert/html/INFO5002-OCT10(10).ipynb?download=false 2/8 10/17/24, 9:29 AM INFO5002-OCT10(10) In : list(range(-10,-100,-30)) [-10, -40, -70] Out: In : sum(range(4))#1+2+3 6 Out: While Loops In [ ]: "While-Loop" - repeat a block of code until a condition is no longer true Syntax : while : ##Where is an expression that returns T black of code 1) If this returns True, bool() called -> block of code is executed 2) While loop is "exited" -> skip the indented code 3) If the indented block code is executed -> go to the first step The range for "for loop" is going to be assiged by "for loop" but not "while" l [correct version] for x in [1,2,3]: ~ [incorrect version] -> errors while x > 3 : While Loop In : i = 1 while i < 3 : print(i) i+=1 print(i) 1 2 3 In : i = 1 while i < 3 : print(i) i+=1 1 2 In [ ]: Loop Control Statements and Clauses The "break" statement - breaks out of the innermost enclosing for-loop or while The "continue" statement - stops the current iteration and continues with the n The "else" clause - can be run once the condition is not true The break and continue in loop localhost:8888/nbconvert/html/INFO5002-OCT10(10).ipynb?download=false 3/8 10/17/24, 9:29 AM INFO5002-OCT10(10) In : i = 1 while i 4 : break print(i) i +=1 continue In [ ]: In [ ]: 4. Use while-loop without break to create a list of all the even numbers betwee In [ ]: a = 1 b = [] while x < 11 : if x % 2 == 1: b.append(a) x += 1 b In : even=[] x =1 while x 10: break In [ ]: d=[] for x in range(1,11): while x % 2 ==0: d.append(x) break print(d) In [ ]: searches for prime numbers within 10 and prints out that a non-prime number consists of a prime number multiplied by another number. In [ ]: for n in range(2,10): for x in range (2,n): if n % x =! 0: In [ ]: for n in range(2,10): for x in range(2,n): if n % x == 0: print(n, "equals",x) break else: print(n, "is a prime number") In [ ]: localhost:8888/nbconvert/html/INFO5002-OCT10(10).ipynb?download=false 8/8