Useful Python tricks from A to Z

Posted by Infocampus HR on January 29th, 2019

Python is one of the world’s preferred, in-demand programming languages. this is for several reasons:

  •           it’s simple to find out
  •           it’s super versatile
  •           it includes a large range of modules and libraries

Here, an attempt at sharing some of them in an A-Z format.

all or any

One of the various reasons why Python is such a preferred language is as a result of its clear and communicative.

bashplotlib

You want to plot graphs within the console?

$ pip install bashplotlib

You can have graphs within the console.

Collections

Python has some nice default data types, however generally they only won’t behave specifically however you’d like them to.

Luckily, the Python normal Library offers the collections module. This handy add-on provides you with further data types.

dir

Ever wondered however you can look within a Python object and see what attributes it has? After all you have got.

This can be a extremely helpful feature once running Python interactively, and for dynamically exploring objects and modules you're working with.

emoji

Yes, really.

$ pip install emoji

from __future__ import

One consequence of Python’s quality is that there are forever new versions below development. New versions mean new features — unless your version is outdated.

Fear not, however. The __future__ module permits you to import practicality from future versions of Python. It’s virtually like time travel, or magic, or one thing.

geopy

Geography may be a difficult tract for programmers to navigate (ha, a pun!). However the Python Training in Bangalore geopy module makes it unnervingly simple.

howdoi

Stuck on a coding problem and can’t keep in mind that solution you saw before? got to check StackOverflow, however don’t need to leave the terminal?

inspect

Python’s inspect module is nice for understanding what's happening behind the scenes. you can even call its strategies on itself!

It additionally uses inspect.getmodule() to print the module within which it had been outlined.

Jedi

Jedi  is code analysis library. It makes writing code faster and additional productive.

Unless you’re developing your own IDE, you’ll probably be most inquisitive about using Jedi as an editor plugin. Luckily, there are already masses available!

You may already be using Jedi, however. The IPython project makes use of Jedi for its code autocompletion practicality.

**kwargs

When learning any language, there are several milestones on the way. With Python, understanding the mysterious **kwargs syntax probably counts together.

The double-asterisk before of a dictionary object allows you to pass the contents of that dictionary as named arguments to a function.

This is helpful after you need to write functions that may handle named arguments not outlined before.

List comprehensions

One of my favourite things regarding programming in Python are its list comprehensions.

These expressions create it simple to write terribly clean code that reads nearly like natural language.

map

Python supports functional programming through a number of built-in options. One in all the foremost helpful is that the map() function — especially together with lambda functions.

newspaper3k

If you haven’t seen it already, then be ready to have your mind blown by Python’s newspaper module.

It allows you to retrieve news articles and associated meta-data from a range of leading international publications. You can retrieve pictures, text and author names.

Operator overloading

Python provides support for operator overloading, that is one in all those terms that cause you to sound sort of a legit computer scientist.

It’s truly an easy construct. Ever puzzled why Python permits you to use the +operator to feature numbers and additionally to concatenate strings? That’s operator overloading in action.

You can outline objects that use Python’s commonplace operator symbols in their own specific means. This permits you to use them in contexts relevant to the objects you’re operating with.

pprint

Python’s default print operate will its job. However strive printing out any giant, nested object, and therefore the result's rather ugly.

Here’s wherever the quality Library’s pretty-print module steps in. This prints out advanced structured objects in an easy-to-read format.

Queue

Python supports multithreading, and this can be expedited by the quality Library’s Queue module.

This module lets you implement queue information structures. These are information structures that allow you add and retrieve entries according to a specific rule.

‘First in, 1st out’ (or FIFO) queues let you retrieve objects within the order they were other. ‘Last in, 1st out’ (LIFO) queues let you access the foremost recently other objects 1st.

Finally, priority queues allow you to retrieve objects in line with the order in which they're sorted.

__repr__

When process a category or AN object in Python, it's helpful to produce an ‘official’ means of representing that object as a string.

sh

Python makes a good scripting language. Generally using the Python Training in Marathahalli standard os and sub process libraries may be a touch of a headache.

The sh library provides a neat various.

It permits you to decision any program as if it were a standard function — useful for automating workflows and tasks, all from inside Python.

Type hints

Python could be a dynamically-typed language. You don’t got to specify data types after you outline variables, functions, classes etc.

This allows for rapid development times. However, there are few things additional annoying than a runtime error caused by an easy writing issue.

Since Python 3.5, you've got the choice to provide type hints when defining functions.

They also enable you to use kind checking tools to catch those stray Type Errors before runtime. Probably worthy if you're functioning on giant, advanced projects!

uuid

A quick and simple thanks to generate universally distinctive IDs (or ‘UUIDs’) is thru the Python commonplace Library’s uuid module.

Virtual environments

This is in all probability my favourite Python factor of all.

Chances are you're functioning on multiple Python comes at any one time. Sadly, generally 2 comes can have confidence completely different versions of an equivalent dependency. that does one install on your system?

Luckily, Python’s support for virtual environments permits you to have the most effective of each world. From the command line:

wikipedia

Wikipedia has a nice API that allows users programmatic access to a nonpareil body of completely free knowledge and data.

xkcd

Humour could be a key feature of the Python language — after all, it's named once British people comedy sketch show Monty Python’s Flying Circus. a lot of of Python’s official documentation references the show’s most famed sketches.

YAML

YAML stands for ‘YAML Ain’t Mark-up Language’. It an information data formatting language, and could be a superset of JSON.

Unlike JSON, it will store more advanced objects and ask its own components. You can additionally write comments, creating it significantly suited to writing configuration files.

zip

One last trick for ya, and it very could be a cool one. Ever needed to create a dictionary out of 2 lists?

Conclusion

So there you've got it, an A-Z of Python tricks — hopefully you’ve found one thing helpful for your next project.

Python’s a really various and well-developed language, so there’s bound to be several features I haven’t got round to as well as.

 

Like it? Share it!


Infocampus HR

About the Author

Infocampus HR
Joined: December 10th, 2016
Articles Posted: 792

More by this author