50 Essential PYTHON Commands - #issue 11-20

in #utopian-io7 years ago

11 . Filtering
Where the command map returns a result for every element in an iterable, filter only returns a result if the function returns a True value. This means that you can create a new list of elements where only the elements that satisfy some condition are used. As an example, if your function checked that the values were numbers between 0 and 10, then it would create a new list with no negative numbers and no numbers above 10. This could be accomplished with a for loop, but this method is much cleaner.
If the function provided to fi lter is ‘None’, then it is assumed to be the identity function. This means that only those elements that evaluate to True are returned as part of the new list. There are iterable versions of filter available in the itertools module.

12 . Reductions
In many calculations, one of the computations you need to do is a reduction operation. This is where you take some list of values and reduce it down to a single value. In Python, you can use the command ‘reduce(function, iterable)’ to apply the reduction function to each pair of elements in the list. For example, if you apply the summation reduction operation to the list of the first five integers, you would get the result ((((1+2)+3)+4)+5).
You can optionally add a third parameter to act as an initialisation term. It is loaded before any elements from the iterable, and is returned as a default if the iterable is actually empty. You can use a lambda function as the function parameter to reduce to keep your code as tight as possible. In this case, remember that it should only take two input parameters.

13. How true is a list?
Python tips 13.png
In some cases, you may have collected a number of elements within a list that can be evaluated to True or False. For example, maybe you ran a number of possibilities through your computation and have created a list of which ones passed.
You can use the command ‘any(list)’ to check to see whether any of the elements within your list are true. If you need to check whether all of the elements are True, you can use the command ‘all(list)’. Both of these commands return a True if the relevant condition is satisfied, and a False if not. They do behave differently if the iterable object is empty, however. The command ‘all’ returns a True if the iterable is empty, whereas the command ‘any’ returns a False when given any empty iterable.

14 . Enumerating
Sometimes, we need to label the elements that reside within an iterable object with their indices so that they can be processed at some later point. You could do this by explicitly looping through each of the elements and building an enumerated list. The enumerate command does this in one line.
It takes an iterable object and creates a list of tuples as the result. Each tuple has the 0-based index of the element, along with the element itself. You can optionally start the indexing from some other value by including an optional second parameter. As an example, you could enumerate a list of names with the command ‘list(enumerate(names, start=1))’. In this example, we decided to start the indexing at 1 instead of 0.

15 . Casting
Variables in Python don’t have any type information, and so can be used to store any type of object. The actual data, however, is of one type or another. Many operators, like addition, assume that the input values are of the same type.
Very often, the operator you are using is smart enough to make the type of conversion that is needed. If you have the need to explicitly convert your data from one type to another, there are a class of functions that can be used to do this conversion process.

The ones you are most likely to use is ‘abs’, ‘bin’, ‘bool’, ‘chr’, ‘complex’, ‘fl oat’, ‘hex’, ‘int’, ‘long’, ‘oct’, and ‘str’. For the number-based conversion functions, there is an order of precedence where some types are a subset of others. For example, integers are “lower” than fl oats. When converting up, no changes in the ultimate value should happen.
When converting down, usually some amount of information is lost. For example, when converting from float to integer, Python truncates the number towards zero.

16. What is this?
Everything in Python is an object. You can check to see what class this object is an instance of with the command ‘isinstance(object, class)’. This command returns a Boolean value.

17 . Is it a subclass?
The command ‘issubclass(class1, class2)’ checks to see if class1 is a subclass of class2. If class1 and class2 are the same, this is returned as True.

18 . Global objects
You can get a dictionary of the global symbol table for the current module with the command ‘globals()’.

19 . Local objects
You can access an updated dictionary of the current local symbol table by using the command ‘locals()’.

20 . Variables
The command ‘vars(dict)’ returns writeable elements for an object. If you use ‘vars()’, it behaves like ‘locals()’.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

nice contribution @holudghuri, python lessons are needed even for experienced developers.

From my point of view it would be easier to get key take away if you emphasize stuff in your content, maybe with the use of italics or bold text. Also code between ticks, like this print("foo") would look better.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Thanks Sir.

Hey @holudghuri I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thanks For your Support @utopian-io

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.029
BTC 76511.74
ETH 3031.28
USDT 1.00
SBD 2.62