50 Essential PYTHON Commands - #issue 21-30

in #utopian-io7 years ago (edited)

50 python.png
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
nonlocals.jpg
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

Sort:  

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

  • 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

Coin Marketplace

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