"Pro Tips" guide for Python beginners

138

    [TIP 1] Multiple conditions in one line

    To create "if" type conditions, you can do it in a much more elegant way and in one line as follows:

    • Instead of doing this:
    if a == 3:
        print(to)
    else:
        print(b)
    
    • You can do this:
    print(to) if a == 3 else print(b)
    

    [TIP 2] Comparing two lists in one line

    Provide explanation:

    • Instead of doing this to compare two lists:
    h = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list = [1, 2, 3, 4, 5, 6, 9, 10r = []
    for elem in h:
        if elem in list: r.append(elem) # r = [1, 2, 3, 4, 5, 6, 9, 10]
    
    • You can do this:
    h = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    list = [1, 2, 3, 4, 5, 6, 9, 10] r = [elem for elem in h if elem in list] # r = [1, 2, 3, 4, 5, 6, 9, 10]
    

    [TIP 3] Checking variables in a more "Pythonic" way.

    If I have two variables and I want it to return the one that is not None, instead of doing this:

    ````python a = None b = 2
    if a: print(a) else: print(b) ```` Print 2
    

    Do this instead:

    ````python a = None b = 2
    print(a or b) ```` Printea 2
    

    [TIP 4] Type objects queue in Python (Library collections).

    If we want to create a queue of objects, instead of creating a list and treating it as such, we can import the library collections and use the object queue that it offers us.

    from collections import about what
    

    These are the main differences between a list and a queue:

    • Access to elements:
      • In a list, you can access items by their index, and delete or insert items at any position.
      • In a queue, you can only access the first item, and you can only remove or insert items from the end of the queue.
    • Efficient operations:
      • Dequeues are optimized for adding and removing elements from both the beginning and the end. popleft() y pop() They are efficient, just like append() y appendleft().
      • In a list, adding or removing items at the beginning is less efficient, as it involves shifting all the remaining items.
    • Specific use:
      • Queues are useful when you need to follow the "first in, first out" (FIFO) principle. That is, the first item added is the first one removed.
      • Lists are more versatile and used for a variety of purposes, as they support operations on any position and are more suitable when the exact order of the elements is crucial. In short, while a queue (about what) shares some similarities with a list, it is optimized for certain scenarios, such as the efficient management of elements at the beginning and end of the sequence.
    from collections import about what
    
    queue = about what()
    
    queue.append(1)
    queue.append(2)
    queue.append(3) print(queue) # about what([1, 2, 3])
    
    queue.popleft() print(queue) # about what([2, 3])
    
    queue.pop()
    print(queue) # about what([2])
    
    ✉ SHALL WE TALK?

    Let's talk

    If we share interests in artificial intelligence, data, or software development, I'd be happy to exchange ideas. You can also contact me for professional matters.

    Read the blog