Python Programming: Lesson 62 - Iterators and the exec Function

Опубликовано: 01 Июль 2025
на канале: youmils03
66
0

PLEASE SUBSCRIBE!!!

In the previous lesson (   • Python Programming: Lesson 61 - Running a ...  ), we took a look at some more OOP fundamentals, reviewing the call and contains magic methods. We also introduced running a module as a script. We say a little more about that in this video, but then we bring up the exec function and iterators. The exec function allows you to execute Python code within a string, and iterators allow you to iterate (parse) through the elements in a particular data structure.

W = iter(D) accepts a data structure and returns an iterator
N = next(W) returns the next element in the data structure D

myTuple = 8 , 2 , "sup" , 9
b = iter(myTuple)
w,x,y,z = myTuple
c = 0
c += next(b)
c += next(b)
c += z
c = c + ( myTuple[1] // 2 )
print(c) prints 20 on the console

exec(x) accepts a string x and executes it as Python code

exec( "a=" + "10" )
exec( "b,c = a,a-1" )
exec( "print( \"a+b+c\"= + str(a+b+c))" )

prints "a+b+c"=29

7:15 NEW: exec(x) for some Python code string x executes the Python code within " "
10:40 NEW: Iterators

The Java connection to a topic on iterators is provided in this video:    • Java Programming: Lesson 51 - The Collecti...  

More on iterators in Python can be found in a video a little further down the road:    • Python Programming: Lesson 72 (OLD, SEE DE...  

Ready for the next video, which is on matrices (2D lists of lists or tuples with numbers in them)? They will be discussed in the next video [and the video after]:    • Python Programming: Lesson 63 - Techniques...  

Thanks for watching, and PLEASE SUBSCRIBE!!!