Join our Telegram Community Join Us Join Now!

TOP 10 PYTHON AUTOMATION PROJECTS ASPIRANTS SHOULD TRY

Python programming for aspirants, TOP 10 PYTHON AUTOMATION PROJECTS ASPIRANTS SHOULD TRY and get insights on what Python programming is really about
Please wait 0 seconds...
Scroll Down and click on link or Go to Link for destination
Congrats! Link is Generated

Python is a programming language known for its simplicity and easy-to-read syntax, as well as a diverse set of frameworks and a robust ecosystem on which Python developers rely heavily. Many times, programmers come across a piece of code on forums such as stack overflow or GitHub and wonder how it works. Python is, indeed, a universal language with an infinite number of possibilities. Even experienced programmers find it not enough to discover new tricks in Python programming. Here are ten hidden Python coding tips to know in 2022.

Python programming for aspirants

Flatten the lists

Converting a 2D list into a 1D list, called flattening the list usually requires nested loops, list comprehensions, recursion, built-in functions, or importing libraries in Python depending on the regularity and depth of the nested lists, the easiest of all being using imported libraries. Here is how it can be done.


    import itertools
    a = [[1, 2], [3, 4], [5, 6]]

    b = list(itertools.chain.from_iterable(a))

    print(b)

    Output:
    [1, 2, 3, 4, 5, 6]

    Reverse a list

    In this technique, a copy of the list is made and the list is not sorted in place. Creating a copy requires more space to hold all of the existing elements. This exhausts more memory. Here we are using the slicing technique to reverse our list in Python.

    a=[“10”,”9″,”8″,”7″]

    print(a[::-1])

    Output:

    10

    9

    8

    7

    Combining different lists

    It is easy to aggregate the contents of the container class using a normal zip function. There are times, though, when numerous lists and contained lists are required as index components, and you must merge them. Though this is an odd circumstance, the solution is straightforward.

    a=[‘a’,’b’,’c’,’d’]

    b=[‘e’,’f’,’g’,’h’]

    for x, y in zip(a, b):

    print(x,y)

    Output:

    a e

    b f

    c g

    d h

    Negative indexing lists

    Giving a negative number as a parameter inside a list function we can remove the last elements of that list and get a new list.


    a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    a[-3:-1]

    Output:

    [8, 9]

    Launch web server

    To launch a web server on any port, a simple command goes a long way in simplifying the code. All that you need to do is, set the port from range 0 to 65353.

    # Run Web Server

    python -m http.server 7000

    Easy value swapping

    Swapping of values of two variables is usually done using a temporary variable. There is a trick where you do not need a temp variable to achieve swapping.

    #using temp variable

    a = 5

    b = 6

    temp = a

    a = b

    b = temp

    #new way

    a, b = b, a

    Opening Website

    Do you need to open a website from your default browser? Then, the next tip will assist you with this.

    # Opening a Website

    import webbrowser

    webbrowser.open(“https://preps.com.ng/“)

    Detecting New Elements

    A nested loop is a common method to detect new elements in lists. However, with a set() data structure it is possible to detect the unique elements in any two lists.

    # Find New Elements

    list1 = [4, 5, 6, 8, 11, 13]

    list2 = [4, 5, 6]

    new = list(set(list1) – set(list2))

    print(new) # [8, 11, 13]

    Checking and analyzing the memory unit of an object

    In Python, everything is an object, from variables to lists and dictionaries everything is treated as an object. Here is one easy way to get its value.

    import sys

    a=10

    print(sys.getsizeof(a))

    Output:

    28

    Transposing a matrix

    In Python, transposing is usually implemented using a nested list (list inside a list) treating each element as a row of the matrix. However, with the zip function, it can be achieved in a few lines.

    mat = [[8, 9, 10], [11, 12, 13]]

    new_mat=zip(*mat)

    for row in new_mat:

    print(row)

    Output:

    (8, 11)

    (9, 12)

    (10, 13)

    Share This Post: If you believe this post will be useful to others, please share it using the buttons below!

    Post a Comment

    Cookie Consent
    We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
    Oops!
    It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
    AdBlock Detected!
    We have detected that you are using adblocking plugin in your browser.
    The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.