10 Interesting Python Cool Tricks - GeeksforGeeks

31 Dec, 2018
In python we can return multiple values –
It’s very unique feature of Python that returns multiple value at time.def GFG():    g = 1     f = 2    return g, f   x, y = GFG()print(x, y)Output:(1, 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.my_list = ['geeks', 'practice', 'contribute']print(my_list[-1])Output:contribute
Combining Multiple Strings. We can easily concatenate all the tokens available in the list.my_list = ['geeks', 'for', 'geeks']print(''.join(my_list))Output:geeksforgeeks
Swapping is as easy as none.See, How we could swap two object in Python.x = 1y = 2  print('Before Swapping')print(x, y)  x, y = y, xprint('After Swapping')print(x, y)Output:Before Swapping
(1, 2)
After Swapping
(2, 1)
Want to create file server in PythonWe can easily do this just by using below code of line.python -m SimpleHTTPServer # default port 8080You can access your file server from the connected device in same network.Want to know about Python version you are using(Just by doing some coding). Use below lines of Code –import sysprint("My Python version Number: {}".format(sys.version))  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.Store all values of List in new separate variables.a = [1, 2, 3]x, y, z = a print(x)print(y)print(z) Output:1
2
3
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]import itertools a = [[1, 2], [3, 4], [5, 6]]print(list(itertools.chain.from_iterable(a)))Output:[1, 2, 3, 4, 5, 6]
Want to transpose a Matrix. Just use zip to do that.matrix = [[1, 2, 3], [4, 5, 6]]print(zip(*matrix))Output:[(1, 4), (2, 5), (3, 6)]
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.subtract = lambda x, y : x-ysubtract(5, 4)
It’s very unique feature of Python that returns multiple value at time.def GFG():    g = 1     f = 2    return g, f   x, y = GFG()print(x, y)Output:(1, 2)

def GFG():    g = 1     f = 2    return g, f   x, y = GFG()print(x, y)
(1, 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.my_list = ['geeks', 'practice', 'contribute']print(my_list[-1])Output:contribute

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

Combining Multiple Strings. We can easily concatenate all the tokens available in the list.my_list = ['geeks', 'for', 'geeks']print(''.join(my_list))Output:geeksforgeeks

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

Swapping is as easy as none.See, How we could swap two object in Python.x = 1y = 2  print('Before Swapping')print(x, y)  x, y = y, xprint('After Swapping')print(x, y)Output:Before Swapping
(1, 2)
After Swapping
(2, 1)

See, How we could swap two object in Python.
x = 1y = 2  print('Before Swapping')print(x, y)  x, y = y, xprint('After Swapping')print(x, y)
Before Swapping
(1, 2)
After Swapping
(2, 1)

Want to create file server in PythonWe can easily do this just by using below code of line.python -m SimpleHTTPServer # default port 8080You can access your file server from the connected device in same network.
python -m SimpleHTTPServer # default port 8080
You can access your file server from the connected device in same network.
Want to know about Python version you are using(Just by doing some coding). Use below lines of Code –import sysprint("My Python version Number: {}".format(sys.version))  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.
import sysprint("My Python version Number: {}".format(sys.version))  
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.
Store all values of List in new separate variables.a = [1, 2, 3]x, y, z = a print(x)print(y)print(z) Output:1
2
3

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

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]import itertools a = [[1, 2], [3, 4], [5, 6]]print(list(itertools.chain.from_iterable(a)))Output:[1, 2, 3, 4, 5, 6]

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

Want to transpose a Matrix. Just use zip to do that.matrix = [[1, 2, 3], [4, 5, 6]]print(zip(*matrix))Output:[(1, 4), (2, 5), (3, 6)]

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

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.subtract = lambda x, y : x-ysubtract(5, 4)
subtract = lambda x, y : x-ysubtract(5, 4)
python-basics
Technical Scripter 2018
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
*args and **kwargs in Python
Selecting rows in pandas DataFrame based on conditions