50 Essential PYTHON Commands - #issue 21-30
21 . Making a global
A list of names can be interpreted as globals for the entire code block with the command ‘global names’.
22 . Nonlocals
Image Source
In Python 3.X, you can access names from the nearest enclosing scope with the command ‘nonlocal names’ and bind it to the local scope.
23 . Raising an exception
When you identify an error condition, you can use the ‘raise’ command to throw up an exception. You can include an exception type anda value.
24 . Dealing with an exception
Exceptions can be caught in a try-except construction. If the code in the try block raises an exception, the code in the except block gets run.
25 . Static methods
You can create a statis method, similar to that in Java or C++, with the command ‘staticmethod(function_name)’.
26 . Ranges
You may need a list of numbers, maybe in a ‘for’ loop. The command ‘range()’ can create an iterable list of integers. With one parameter, it goes from 0 to the given number. You can provide an optional start number, as well as a step size. Negative numbers count down.
27 . Xranges
One problem with ranges is that all of the elements need to be calculated up front and stored in memory. The command ‘xrange()’ takes the same parameters and provides the same result, but only calculates the next element as it is needed.
28 . Iterators
Iteration is a very Pythonic way of doing things. For objects which are not intrinsically iterable, you can use the command ‘iter(object_name)’ to essentially wrap your object and provide an iterable interface for use with other functions and operators.
29 . Sorted lists
You can use the command ‘sorted(list1)’ to sort the elements of a list. You can give it a custom comparison function, and for more complex elements you can include a key function that pulls out a ranking property from each element for comparison.
30 . Summing items
Above, we saw the general reduction function reduce. A specific type of reduction operation, summation, is common enough to warrant the inclusion of a special case, the command ‘sum(iterable_object)’. You can include a second parameter here that will provide a starting value.
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved yet. See the Utopian Rules. Please edit your contribution to reapply for approval.
It seems image you have shared belongs to a slide from another author. Can you please edit this post and cite your sources?
You can reply to this comment once you're done.
You can contact us on Discord.
[utopian-moderator]
Thanks for this opportunity and very sorry for this error. I edited my post and cited the image source. Please re verify it.
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Thanks For Acceptance.But why I'm ban to create post in utopisn.io? Please help me to solve this.Please Sir..
You have banned temporarily because of plagiarism on this post.
Thanks For the quick reply. Yes I took some references from internet.From now I will be more cautious. But how long will this Ban stay? Please Help..
My post was not showing in utopian.io but it was created in Steemit.com. When I created a new post in utopian.io, the submit button was hanged for the infinite time. What will be the reason? Can u please help me..
Hey @holudghuri I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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