10 Lesser known features of Python

10 Lesser known features of Python

Mishel Shaji
Mishel Shaji

Python is one of the most widely used languages in the world that is known for its clean syntax and beginner friendliness. In this post, I'm sharing some lesser-known features of Python that I recently came to know about.

1. Python can return multiple values

Python can return multiple values in a single statement. This doesn’t mean that in other languages like C# or Java, it is impossible to return multiple values. Such languages have to either return it as an array or use some keywords.

def calc(a, b):
    return a+b, a-b, a*b, a/b

add, sub, mul, div = calc(5, 5)
print('5 + 5 is: ', add)
print('5 - 5 is: ', sub)
print('5 * 5 is: ', mul)
print('5 / 5 is: ', div)

That’s interesting. Isn’t it?

2. The Zen of Python

Tim Peters, a major contributor to the Python community, wrote this poem to highlight the philosophies of Python. If you type in “import this” in your Python IDLE, you’ll find this poem:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

3. Swapping made simple

How will you swap the values of two variables? Will you use a third variable like this for swapping?

a = 5
b = 6

c = a # c is now 5
a = b # a is now 6
b = c # b is now 5

Let me show you a shorter way to do this in Python.

a = 5
b = 6
a, b = b, a

4. else with for and while

Will you believe if I say that you can use else block with for and while loops in Python? But you have to…

for i in range(2):
    print("From while")

# Output
# 0
# 1
# From while

And with while loop:

a = 0
while(a < 2):
    a += 1
    print("From else")

5. String concatenation

Python will concatenate two strings if they are separated by a space.

s = "Hello" "World"
# Output
# HelloWorld

6. Try to use import braces, you will get an error

Have you ever tried to use braces like in C instead of indentation in Python. If not, try to import it from __future__ package. You will get an error like this.

from __future__ import braces

# Output
# SyntaxError: not a chance

7. Index of a list

We can use enumerate method to access elements of a list with its index.

a = ['One', 'Two', 'Three']
for index, value in enumerate(a):
    print(index, value)

# Output
# 0 One
# 1 Two
# 2 Three

8. You can access private members of a class

Usually, private members are not directly accessible by any object or function outside the class. Only the member functions or the friend functions can access the private data members of a class.

But in Python, we can use an alternate way to access private member functions of any class with this syntax.


Here’s an example.

class SeeMee:

    # Public method
    def youcanseeme(self):
        return 'you can see me'

    # Private method
    def __youcannotseeme(self):
        return 'you cannot see me'

Check = SeeMee()
# you can see me
# print(Check.__youcannotseeme())
# AttributeError: 'SeeMee' object has no attribute '__youcannotseeme'

##########BUT YOU CAN STILL ACCESS IT################

9. Antigravity module

Open Python IDLE and import antigravity module and see what happens.

import antigravity

10. Keys should be unique

Keys of a dictionary should be unique by equivalence. Check this example.

languages = {}
languages[5.5] = "Java"
languages[5.0] = "PHP"
languages[5] = "Python"

# Output
# Python

What happened to PHP? Was it replaced by Python?