10 Interesting Python Cool Tricks

In python we can return multiple values –

  1. It’s very unique feature of Python that returns multiple value at time.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    def GFG():
        g = 1 
        f = 2
        return g, f 
      
    x, y = GFG()
    print(x, y)

    chevron_right

    
    

    Output:

    (1, 2)
    
  2. Allows Negative Indexing: Python allows negative indexing for its sequences. Index -1 refer to the last element, -2 second last element and so on.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    my_list = ['geeks', 'practice', 'contribute']
    print(my_list[-1])

    chevron_right

    
    

    Output:

    contribute
    
  3. Combining Multiple Strings. We can easily concatenate all the tokens available in the list.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    my_list = ['geeks', 'for', 'geeks']
    print(''.join(my_list))

    chevron_right

    
    

    Output:

    geeksforgeeks
    
  4. Swapping is as easy as none.

    See, How we could swap two object in Python.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    x = 1
    y = 2
      
    print('Before Swapping')
    print(x, y)
      
    x, y = y, x
    print('After Swapping')
    print(x, y)

    chevron_right

    
    

    Output:

    Before Swapping
    (1, 2)
    After Swapping
    (2, 1)
    
  5. Want to create file server in Python
    We can easily do this just by using below code of line.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    python -m SimpleHTTPServer # default port 8080

    chevron_right

    
    

    You can access your file server from the connected device in same network.

  6. Want to know about Python version you are using(Just by doing some coding). Use below lines of Code –
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import sys
    print("My Python version Number: {}".format(sys.version))  

    chevron_right

    
    

    Output:

    My Python version Number: 2.7.12 (default, Nov 12 2018, 14:36:49) 
    [GCC 5.4.0 20160609]
    

    It prints version you are using.

  7. Store all values of List in new separate variables.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    a = [1, 2, 3]
    x, y, z =
    print(x)
    print(y)
    print(z) 

    chevron_right

    
    

    Output:

    1
    2
    3
    
  8. Convert nested list into one list, just by using Itertools one line of code. Example – [[1, 2], [3, 4], [5, 6]] should be converted into [1, 2, 3, 4, 5, 6]
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import itertools 
    a = [[1, 2], [3, 4], [5, 6]]
    print(list(itertools.chain.from_iterable(a)))

    chevron_right

    
    

    Output:

    [1, 2, 3, 4, 5, 6]
    
  9. Want to transpose a Matrix. Just use zip to do that.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    matrix = [[1, 2, 3], [4, 5, 6]]
    print(zip(*matrix))

    chevron_right

    
    

    Output:

    [(1, 4), (2, 5), (3, 6)]
    
  10. Want to declare some small function, but not using conventional way of declaring. Use lambda. The lambda keyword in python provides shortcut for declare anonymous function.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    subtract = lambda x, y : x-y
    subtract(5, 4)

    chevron_right

    
    



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.