This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #can be executed as many times as we want | |
| print("Iterable:") | |
| for i in range(3): | |
| for chips in can_of_pringles: | |
| print(chips,end=" ") | |
| print("\n") | |
| #can be executed only once | |
| print("Iterator:") | |
| for j in range(3): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #will raise StopIteration exception | |
| print(next(iterator_COP)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| print(next(iterator_COP)) | |
| print(next(iterator_COP)) | |
| for chips in iterator_COP: | |
| print(f"Taking out chip {chips}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| print(dir(iterator_COP)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #creating an iterator | |
| iterator_COP=iter(can_of_pringles) | |
| print(f"\nType of 'can_of_pringles': {type(can_of_pringles)}") | |
| print(f"\nType of 'iterator_COP': {type(iterator_COP)}") | |
| print(f"\nIterator : {iterator_COP}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| print(next(can_of_pringles)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| print(dir(can_of_pringles)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| can_of_pringles=[1,2,3,4,5,6,7,8,9,10] | |
| for chips in can_of_pringles: | |
| print(f"Taking out chip {chips}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Generator Expression | |
| num_cube_lc=[n**3 for n in range(1,11) if n%2==0] #List Comprehension | |
| num_cube_generator=(num**3 for num in range(1,11) if num%2==0) #Generator Expression | |
| print(f"List Comprehension = {num_cube_lc}") | |
| print(f"Generator Expression = {num_cube_generator}") | |
| #sum(num_cube_generator) | |
| print(f"Sum = {sum(num_cube_generator)}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Dictionary Comprehension | |
| numbers=[num for num in range(1,11)] #creating a List of numbers from 1 to 10 | |
| num_cube=[n**3 for n in numbers] #creating a List of cubes of numbers from 1 to 10 | |
| cubes={} | |
| for (key,value) in zip(numbers,num_cube): | |
| cubes[key]=value | |
| print(f"cubes = {cubes}") |