Change language

Amazing Python Hacks

Python is truly one of the smartest and most popular languages ​​out there. Here are some cool hacks that make Python superior to all other languages.

  1. Understanding Lists: Understanding Lists — the best and most efficient method to get rid of unnecessary lines of code. 

    # Geek list declaration

    geek = [ ’Geeks’ , ’ Programming’ , ’Algorithm’ , ’ Article’ ]

     
    # Direct print list

    print ( "Simple List: " , geek)

      
    # Print the list using join method

    print ( ’ List by using join method:% s’ % ’,’ . join (geek))

      
    # Direct use of join method

    print ( ’Direct apply the join method: ’ , ( ", " . join (geek)) )

      Output:  Simple List : [’Geeks’,’ Programming’, ’Algorithm’,’ Article’] List by using join method: Geeks, Programming, Algorithm, Article Direct apply the join method: Geeks, Programming, Algorithm, Article 
  2. Cool tricks n and lightning

  3. Transpose the matrix: You can read here about it.
  4. Split the list into N groups: we used iter () as a sequence iterator.

     
    # Geek list declaration

    geek = [ ’Sun’ , ’Flowers’ , ’ Peoples’ , ’Animals’ , ’ Day’ , ’Night’ ]

     
    # In python 2.7 just remove the list keyword

    partition = list ( zip ( * [ iter (geek)] * 2 ))

    print (partition)

      Output:  [(’Sun’,’ Flowers’), (’Peoples’,’ Animals’), (’Day’,’ Night’)] 

    Explanation : [iter (geek)] * 2 creates a list containing 2 items from the geek [] list, ie a list of length 2. * arg unpacks a sequence into arguments for a function call. Therefore, we pass the same iterator 2 times to zip ().

  5. Print multiple list items at the same time

    list1 = [ 1 , 3 , 5 , 7 ]

    list2 = [ 2 , 4 , 6 , 8 ]

     
    # Here the zip () function takes two s the same length and concatenates them
    # together in pairs

    for a, b in zip ( list1, list2):

    print (a, b)

      Output:  1 2 3 4 5 6 7 8 
  6. Take a string as input and convert it to a list:

    # In Python 2.7 replace input () with raw_input ()
    # Reads a string from input and casing it into int
    # after splitting into spaces

     

    formatted_list  = list ( map ( int , input (). split ()))

    print (formatted_list)

      Input:  2 4 5 6  Output:  [2, 4, 5, 6] 
  7. Convert list into one list

    # import itertools

    import itertools

     
    # Declaring geek list

    geek = [[  1 , 2 ], [ 3 , 4 ], [ 5 , 6 ]]

     
    # chain. from_iterable () function returns nested list items
    # and repeat from first iterable list to last
    # end of list

     

    lst = list (itertools.chain.from_iterable (geek))

    print (lst)

      Outpu t:  [1, 2, 3, 4, 5, 6] 
  8. Printing repeated characters: The task is to print the template, like this Geeeeekkkkss. This way we can easily print this template without using a for loop.

    # + used to concatenate lines
    # To repeat a character n times, just multiply n
    # with this character

    print ( "G" + " e " * 5 + "k" * 4 + "s" * 2 )

      Output:  Geeeeekkkkss 

Details her:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method