Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox
manishel, Author at The Linux Juggernaut

Author: manishel

How to use all() and any() built-ins in python

This is the first part of the series on python built-in functions. In this tutorial we will present any() and all() built-ins. Both these functions serve to analyze the content of a sequence when applying a specific condition/requirement. 1. any(sequence) any() function is used to check if at least one element of a sequence fulfills a given condition, i.e. returns “True”. Eg.:   >>> any ( [ True ] ) True >>> any ( [ True, True, True ] ) True >>> any ( [ True, True, False ] ) True >>> z = [ 10, 20, 30 ] >>> any ( [ x > 10 for x in z ] ) True >>> any ( [ x > 50 for x in z ] ) False >>> In order to make a good use of this function you need to know the return value of different types in python. For example, all numbers except 0 return True: >>> z = [ 0, 0, 0.01, 0 ] >>> any ( z ) True >>> z = [ 0, 0, 0, 0 ] >>> any ( z ) False Strings, apart the empty string, return True: >>> any( [ 0, 0, "0", 0 ] ) True >>> any ( [ 0, 0, "", 0 ] ) False Empty sequences and None type are always False. Be careful! A sequence containing...

Read More

5 Useful value unpacking and * (star) operator idioms in python

Value unpacking is a very useful feature in Python. It helps make the code readable and more chic 🙂 The idioms presented here are the most common ways to use unpacking in Python and are intended to help you understand how it works and when it can be useful. These idioms work for iterables (lists, strings) and tuples. 1. Classical variable unpacking in Python The one idiom you are probably familiar with from your python course is a classical unpacking idiom, where you assign each element of an iterable (or a tuple) to a specific variable: >>> data = ("Elena", "nhoj546", "Mon, 7 Nov 2016 08:16:56 GMT", "", ("", "http", "")) >>> username, passwd, last_login, email, connection_details = data >>> username 'Elena' >>> passwd 'nhoj546' >>> connection_details ('', 'http', '') >>> connection_details[1] 'http' >>> ip_for, protocol, ip_by = connection_details >>> protocol 'http' >>> 2. Replace unnecessary values with "_" If you do not want to litter the namespace with additional variables you can assign the elements that you do not need to an underscore (“_”) while unpacking. The items assigned to an underscore are dropped: >>> user = "lena:x:1000:1000:lena user,,,:/home/lena:/bin/bash" >>> username, _, id, _, fullname, homedir, shell = user.split(":") >>> id '1000' >>> shell '/bin/bash' >>> 3.Wrapping several values into a list while unpacking Note: available in Python3 This idiom allows you to unpack certain values and wrap...

Read More

Over 16,000 readers, Get fresh content from “The Linux juggernaut”

Email Subscribe


My photo
My name is Surendra Kumar Anne. I hail from Vijayawada which is cultural capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. At present I work at Bank of America as Sr. Analyst Systems and Administration. You can contact me at surendra (@) linuxnix dot com.