in programming, python, tutorial

The Top 5 Hidden Features of Python

Share Button

Python aims to be an elegant and expressive language; this post includes its top 5 hidden features:

  1. List slicing
  2. For…else syntax
  3. Yield statement
  4. Multiple assignments
  5. Argument unpacking

The term hidden is loosely used to indicate features which are generally unique to Python, or not very well known. I covered the most interesting Easter eggs which are really hidden in Python in this post.

1. List slicing

Python has the ability to slice list using the [start:end] notation. Given a list a, a[start:end] returns the sublist from the  start-th to the  end-th element.

list2_6

Python allows to omit end to indicate the last element:

list2_end

You can also omit start, which is equivalent to use  instead:

list0_6

One of the most annoying things when it comes to lists and arrays is to work with their last elements. Python allows to use negative indices, which behaves exactly like positive ones but on the right side:

list2_-4

2. For…else syntax

For loops are often used to search for a specific element within a list. When this is the case, a flag is usually used to indicate if it has been found or not:

Python offers the for…else construct, which replaces the code above with the more expressive:
The else statement is executed if the loop has been completed without ever invoking break.

3. Yield statement

In Python there is the concept of iterator. Lists, strings and ranges are all iterators. They allows to drive for loops, such as:

Technically speaking, they are classes which:

  1. Have a __init__ method
  2. Have a __iter__ method which return self
  3. Have a next method ( __next__ in Python 3)

For instance, the following class is an iterator that counts up to a certain value:

As you can see, iterators are extremely tedious to create; something as easy as counting to n suddenly requires dozens of lines of code. Generators are the solution to this problem: they are methods which invoke the yield statement. The same class can be rewritten as a generator like this:

Both iterators and generators can be used in for loops and other functions which takes sequences:

4. Multiple assignments

Languages such as C and Java only support single assignment. One variable equal one vale. Python allows to have multiple assignments on the same line.

This also allows for the infamous in-line swap:

Multiple assignments in Python is just a clever way of packing and unpacking variables. Python natively supports list and tuples unpacking:

5. Argument unpacking

List unpacking fails when used on a function; Python doesn’t natively unpack a list or a tuple when is passed to a function. This is because it may cause ambiguity: it’s up to the developer to specify when this has to be done. For instance:

is equivalent to the more compact:

The magic is done by the star operator. Python also has the double star operator, which is used to semantically unpack dictionaries:

In this case, Python will match the keys of the dictionary with the name of the arguments in the function. In order for this to work, the keys in the dictionary must match the name of the arguments.

Other resources

📧 Stay updated

A new tutorial is released every week.

💖 Support this blog

This websites exists thanks to the contribution of patrons on Patreon. If you think these posts have either helped or inspired you, please consider supporting this blog.

Paypal
Ko-fi
Twitter_logo

Write a Comment

Comment

Webmentions

  • The Top 5 Easter Eggs in Python – Alan Zucconi July 11, 2017

    I can use this. 🙂

  • The Top 5 Hidden Features of Python - Programming Languages July 11, 2017

    I can use this. 🙂

  • Interactive Graphs in the Browser - Alan Zucconi July 11, 2017

    I can use this. 🙂

  • Recreational Maths in Python - Alan Zucconi July 11, 2017

    I can use this. 🙂