Learning to program using Python

Learning to program using Python is a great choice. Python is a friendly language where one can easily learn the building blocks of programming.

In learning to program using Python, I want to share some resources that I have found key to master learning to programming supported on this awesome language. The resources in here will be revisited from time to time to keep them updated or to add new ones.

Courses

Introduction to CS and programming using Python

This course was lectured in Fall 2022, by MIT’s professor, Ana Bell. I like the fact that it is a real course where MIT students shared their inputs.

I highly encouraged you to view the complete course here.

Videos on YouTube

Learn to Program: The Fundamentals

Course link.

Free Python e-books

Threading.Lock and its uses in Python

In Python you can spawn a new thread to perform a task, and then wait for that thread to finish with its task.

One of the reasons why programmers like to work with threads is that all of the threads share the same data. If you have a global variable “x”, and you start up a bunch of threads, all of those threads share the same value of “x”.  For example:

    import threading

    x = 12345
    y = 'hello'
    z = [10, 20, 30]

    class MyClass(threading.Thread):

    def run(self):
        tid = threading.get_ident()
        print("Thread {0} has x = {1}, y = {2}, z = {3}".format(tid, x,y,z))

    for i in range(5):
    t = MyClass()
    t.start()

In the above code, we create five instances of MyClass, each of which has a “run” method. We invoke that “run” method within a new thread with t.start(). Within each thread, we have access to all of our global data.

This might lead you to believe that you can modify data from within the threads. And that would be a very, very bad idea. This is because Python’s built-in data structures aren’t “thread safe.”  This means that certain operations require several low-level instructions, and that Python might start executing a different thread partway through those instructions. If A and B are both modifying the same data structure, then we’re in trouble.

Note: All text before this line is just a snippet of the article Threading.Lock and its uses in Python, published in the newsletter Better Developers. The author of the already mentioned article is Reuven M. Lerner. Subscribe today to Better Developers using this link.