Python, made famous by ARTIFICIAL intelligence, is a well-developed and very diverse language, and there’s bound to be some functionality you haven’t discovered yet. This article may help you learn some new techniques.
Python is one of the most popular and popular programming languages in the world for many reasons, such as:
Easy to learn
Super versatility
It has a large number of modules and libraries
This article will share some tips for using Python, in order a-Z.
1.all or any
One of the reasons Python is so popular is its readability and expressiveness.
Python is also often referred to as “executable pseudocode.” But when you can write code like this, it’s hard to argue against:
2.bashplotlib
Want to draw in the console?
Use the above lines to draw in the console.
3.collections
Python has some great default data types, but sometimes they don’t go your way.
However, the Python standard library provides the Collections module. This handy add-on can give you more data types.
The collections module: docs.python.org/3/library/c…
4.dir
Have you ever wondered how to look inside Python objects and what properties they have?
Enter the following command line:
This can be a very useful feature when running Python interactively, and allows you to dynamically explore the objects and modules you are using.
5.emoji
Yes, there really is. Please click here: pypi.org/project/emo…
Don’t think I didn’t know you were gonna try it
6.fromfutureimport
One result of Python’s popularity is that there are always new versions in development. New releases mean new features — unless your version is obsolete.
But don’t worry. The _ future_ module allows users to import new Python features. It’s like time travel or magic or something.
_ future_ modules: docs.python.org/2/library/f…
7.geopy
Geography can be a challenging area for programmers. But the Geopy module makes it surprisingly simple.
Geopy modules: geopy. Readthedocs. IO/en/latest /
It works by extracting apis from a range of different geocoding services, allowing users to retrieve a place’s full street address, latitude, longitude, and even altitude.
Another useful feature is distance: it can calculate the distance between two positions in your preferred unit of measurement.
8.howdoi
Stuck with a coding problem but can’t remember the solution you’ve seen before? Need to check StackOverflow, but don’t want to leave the terminal?
Then you need this useful command-line tool: github.com/gleitz/howd…
No matter what questions you have, you can ask it and it will try to answer them.
But be warned — it grabs code from the top vote answer on StackOverflow. That is, the information it provides is not always useful…
9.inspect
Python’s Inspect module is very helpful in understanding the details behind the problem. You can even call its methods on the Inspect module!
Inspect modules: docs.python.org/3/library/i…
The following code example prints its own source code using inspect.getSource (). It also prints the module that defines it using inspect.getModule ().
The last line prints out its own line number.
Of course, beyond these trivial uses, the Inspect module helps you understand what your code is doing. You can also use it to write self-documenting code.
10.Jedi
The Jedi library is a library for auto-completion and code analysis. It makes code writing faster and more efficient.
Unless you are developing your own IDE, you will definitely be interested in using the Jedi library as an editing plug-in.
Jedi: Jedi. Readthedocs. IO/en/latest/d…
You are probably already using Jedi. The IPython project uses Jedi for code completion.
11.**kwargs
There are many milestones in learning any language. For Python, understanding the arcane **kwargs syntax might be one of them.
The double star in front of the dictionary object lets you enter the contents of the dictionary as named arguments to the function.
The secret key of the dictionary is the parameter name, and the value is the value passed to the function. You don’t even need to call it kwargs!
This is useful when you want to write functions that can handle previously undefined named parameters.
12. List comprehensions
I like the Python programming is one of the reasons why the list comprehensions (docs.python.org/3/tutorial/…
These expressions make it easy to write clean, easy-to-read code that reads almost like natural language.
Use more information about them please check: www.learnpython.org/en/List_Com…
13.map
Python supports functional programming with many built-in features. The map() function is one of the most useful — especially when combined with lambda functions.
Lambda functions: docs.python.org/3/tutorial/…
In the above example, map() applies a simple lambda function to each element in X. It returns a map object that can be transformed into an iterable object, such as a list or tuple.
14.newspaper3k
If you haven’t seen it before, I suggest you check it out: pypi.org/project/new…
It helps you retrieve news articles and related metadata from a number of top international publications. You can retrieve images, text, and author names.
It also has some built-in NLP features.
Address: newspaper. Readthedocs. IO/en/latest/u…
If you want to use BeautifulSoup or some other DIY web scraping library for your next project, why not use $PIP Install Newspaper3K to save time and trouble?
15. Operator overloading
Python supports operator overloading.
It’s actually a simple concept. Have you ever wondered why Python allows users to add numbers and cascade strings using the + operator? This is operator overloading in action.
You can use Python’s standard operation notation to define objects so that you can use them in their context.
16.pprint
Python’s default print function allows you to print. But if you try to print larger nested objects, you’ll find that the print is ugly.
This is where the Pretty Printer module of the Python standard library comes into play. This module prints complex structured objects in an easy-to-read format.
Pretty printer module: docs.python.org/3/library/p…
One of the skills a Python developer must have is working with complex data structures.
17.Queue
Python supports multithreading, which is supported by the Queue module of the Python standard library.
This module allows users to implement queue data structures. Queue data structures allow users to add and retrieve items according to specific rules.
“First in, First Out” (FIFO) queues allow users to retrieve objects in the order in which they were added. The “Last in, First Out” (LIFO) queue allows users to access the newly added object first.
Finally, the Priority queue allows users to retrieve objects based on their corresponding priority category.
How to use queue implement multithreaded programming in Python, sample see: www.tutorialspoint.com/python3/pyt…
18.repr
When defining a category or object in Python, it is useful to represent the object “officially” as a string. Such as:
This makes code debugging much easier. Add a string to the category definition as follows:
19.sh
Python is a great scripting language, though it can sometimes be tricky to use the standard OS and subprocess libraries.
The SH library provides a good alternative.
Sh library: amoffat. Making. IO/sh /
The library allows users to invoke arbitrary programs as if they were normal functions, which is useful for automating workflows and tasks.
20. Type Hints
Python is a dynamic language. You do not need to specify data types when defining variables, functions, classes, and so on.
This helps shorten development cycles. However, run-time errors caused by simple typing issues are really annoying.
Starting with Python version 3.5, users can choose to turn on type hints when defining functions.
def addTwo(x : Int) -> Int:
return x + 2
You can also define type aliases:
Although not mandatory, type annotations can make code easier to understand.
They also allow you to catch TypeErrors using type checking tools before running. It is worthwhile to do this when working on large and complex projects.
21.uuid
A quick and easy way to generate Universally Unique ids (UUID) is to use the UUID module of the Python Standard library.
Uuid module: docs.python.org/3/library/u…
This creates a randomised 128-bit number that is almost necessarily unique.
In fact, you can generate 2¹²² possible UUID. The number of more than 5000000000000000000000000000000000000.
The probability of finding duplicate numbers in a given set is extremely low. Even with a trillion UUID, the probability of a duplicate number is far less than one in a billion.
22. Virtual Environment
This is probably my favorite thing about Python.
You may be working on multiple Python projects at the same time. Unfortunately, sometimes two projects depend on different versions of the same dependency. Which version do you want to install?
Fortunately, Python supports virtual environments, which allows users to take full advantage of both environments. See the following lines:
You now have multiple versions of Python independently on a single machine. Problem solved!
23.wikipedia
Wikipedia has a great API that allows users to programmatically access a huge amount of free knowledge and information.
The Wikipedia module makes accessing the API very easy.
Wikipedia modules: Wikipedia. Readthedocs. IO/en/latest/q…
Like the real Wikipedia site, the module supports multiple languages, page disambiguation, random page search, and even a donate() method.
24.xkcd
Humour is a key feature of the Python language, named after the British comedy Monty Python and the Flying Circus. Many official Python documentation references the comedy’s most famous plot.
Humor is not limited to documentation. Try running the following line:
The Python comic drawn by XKCD will open. Don’t change that, Python. Don’t change.
25.YAML
YAML stands for “YAML Ain” t Markup Language. It is a data format language that is a superset of JSON.
Unlike JSON, it can store more complex objects and reference its own elements. You can also write comments that make them especially useful for writing configuration files. Therefore, if you want to learn Python, it is necessary to listen to the teacher’s class and receive the benefits of Python. If you want to learn Python, you can go to the weixin (same pronunciation) of Teacher Mengya: the front group is: Mengy, the back group is: 7762, and she will arrange to learn the above two groups of letters in order.
PyYAML module (pyyaml.org/wiki/PyYAML… YAML is used in Python. Installation:
Then import into the project:
PyYAML enables you to store Python objects of any data type, as well as instances of any user-defined category.
26.zip
Here’s your last tip. It’s really cool. Still making a dictionary out of two lists?
The zip() built-in function takes multiple iterables as input and returns a list of tuples. Each tuple groups the elements of the input object by positional index.
You can also “unzip” the object by calling *zip().
Selected from FreeCodeCamp
Like to remember to have one