Python Random Tips #7

This is not a Python Starter Tutorial but advanced Python Tips in a Random Order. 

In this section, let us see some quick questions & answers with Python code examples:

#1 How to remove the last element from a Python list?

#2 append() vs entend()?

Append() adds the element to the end of the list. the length is incremented by 1 as you can see in the results below.

However extend() will add ‘all elements’ from the interables to the end of the list.

Please note that ‘new’ is added 3 elements as ‘n’, ‘e’, ‘w’. Hence the total length became 5 + 3 = 8 after the extend.

#3 What is the type of the below declaration?

a_b_c = 12,00,000

int ? No, it is not int.

#4 Indexing and slicing. Is there any difference?

If we try to access a list with an index that exceeds the number of elements in the list, we will obviously get an index error.

But if we try to slice the above list using the same ‘index” that exceeds the number of elements, it will not give index out of range error.

The output is an empty list when we try to slice. Interesting, right?

#5 What is a conditional import in Python?

As the name suggests conditional import imports some modules with some specific conditions only. Otherwise we can skip the import of that module.

That’s all for this post. See you next time. Enjoy Python Programming.

Leave a comment

Design a site like this with WordPress.com
Get started