By Peter Gleeson
Compiler: Bot
Python is one of the most popular on-demand programming languages in the world because:
- It’s easy to learn
- It’s versatile
- It has a bunch of modules and libraries
As a data scientist, WORKING with Python is pretty routine for me. Now that I’ve collected some useful tips along the way, it’s time to share them with you!
Here, I’ll start with the letter “A” and end with the letter “Z” to briefly introduce some common techniques. Most of them I stumble upon in my daily work, and some come from The Python standard library documentation and others’ summaries.
all or any
Python’s popularity is due to its readability and expressiveness. People often joke about Python as “executable pseudocode,” and while that sounds uncomfortable, when you’re actually writing code, you’ll find that you really don’t have much to refute:
x = [True.True.False]
if any(x):
print("At least one True")
if all(x):
print("Not one False")
if any(x) and not all(x):
print("At least one True and one False")
Copy the code
bashplotlib
Do you want to draw basic graphics on the terminal?
$ pip install bashplotlib
Copy the code
One command for data visualization without a GUI.
collections
Python has some built-in data types, but sometimes they don’t fit our needs. Fortunately, the Python standard library has a Collections module, which provides additional data types on top of STR, int, list, tuple, set, dict, and so on.
from collections import OrderedDict, Counter
Remember the order in which keys are added!
x = OrderedDict(a=1, b=2, c=3)
# Count the frequency of each character
y = Counter("Hello World!")
Copy the code
dir
How do we view objects and their properties in Python? The answer is to use the dir() function.
>>> dir()
>>> dir("Hello World")
>>> dir(dir)
Copy the code
This is a very useful function when you are running Python interactively and dynamically exploring the modules of the objects you are using.
emoji
Yes, what’s the soul of code without emojis?
from emoji import emojize
print(emojize(":thumbs_up:"))
Copy the code
👍
from __future__ import
Python is updated frequently, with each new release adding new features, or making changes to older versions, or fixing bugs — meaning that some changes are incompatible with older versions.
To make previously working code available in the new version, we can call Python’s __future__ module to import features from the new version into the current version.
from __future__ import print_function
print("Hello World!")
Copy the code
geopy
Geography can be a challenging area for programmers, but the Geopy module makes the whole process very simple.
$ pip install geopy
Copy the code
It provides aN API for a number of different geocoding services, allowing you to easily get country, city, full street address, latitude, longitude and even altitude information for a particular part of the world.
It also contains a useful distance class that measures the distance between two positions.
from geopy import GoogleV3
place = "221b Baker Street, London"
location = GoogleV3().geocode(place)
print(location.address)
print(location.location)
Copy the code
howdoi
Sometimes, you may have just seen the solution to a problem, but forget it as soon as you get started. In this case, what if you don’t want to leave the terminal and you want to go on StackOverflow to check?
All you need is this command-line tool:
$ pip install howdoi
Copy the code
It will try to answer any questions you have.
$ howdoi vertical align css
$ howdoi for loop in java
$ howdoi undo commits in git
Copy the code
Note that just because it’s a truncated version of StackOverflow’s top answer doesn’t mean it’s 100% useful.
$ howdoi exit vim
Copy the code
inspect
Python’s inspect module examines basic information about a module. It serves four purposes: type checking, obtaining source code, obtaining information about class or function parameters, and parsing the stack.
Here is an example of printing source code with inspect.getSource (), which can also print custom modules with inspect.getModule ().
import inspect
print(inspect.getsource(inspect.getsource))
print(inspect.getmodule(inspect.getmodule))
print(inspect.currentframe().f_lineno)
Copy the code
In addition to the above purposes, examining modules helps us better understand code and write maintainable code.
Jedi
Jedi is an autocomplete/static analysis library that makes writing code faster and more efficient.
If you’re not an IDE developer, you might just want to use Jedi as a browser plug-in or in a shell. It can be done. Read this introduction for details.
**kwargs
No matter what programming language you are learning, you will encounter several difficult points. In Python, the **kwargs syntax is one of them.
The double star in front of the dictionary object allows you to pass the contents of the dictionary to functions as named arguments, where the keys are parameter names and the values are values passed to functions.
dictionary = {"a": 1, "b": 2}
def someFunction(a, b):
print(a + b)
return
# Do the same as above
someFunction(**dictionary)
someFunction(a=1, b=2)
Copy the code
This module is useful when you want to write functions that can handle named parameters that are not predefined.
List comprehensions
List parsing is one of the reasons many people like Python. These expressions can easily be written in very clean code, almost like natural language.
Numbers = [1,2,3,4,5,6,7for x in numbers if x % 2 is 0]
odds = [y for y in numbers if y not in evens]
cities = ['London'.'Dublin'.'Oslo']
def visit(city):
print("Welcome to "+city)
for city in cities:
visit(city)
Copy the code
map
Python supports functional programming with many built-in features. The most useful of these is the map() function — especially in combination with lambda functions.
x = [1, 2, 3]
y = map(lambda x : x + 1 , x)
# print out [2,3,4]
print(list(y))
Copy the code
In the example above, map() applies a simple lambda function to each element x. It returns a map object that can be converted to some iterable, such as a list or tuple.
newspaper3k
If you’ve never heard of Newspaper3K before, this module might surprise you.
This is a library for article extraction that allows you to retrieve news articles and associated metadata such as images, text, and author names from a large number of international publications. It even has some built-in NLP functions.
Operator overloading
Python supports operator overloading. While it sounds technical, the concept is actually quite simple. Have you ever wondered why Python allows us to add numbers and concatenation strings using the + operator?
This is operator overloading in practice. You can define objects that use operator symbols to suit your needs and then put them into your code.
class Thing:
def __init__(self, value):
self.__value = value
def __gt__(self, other):
return self.__value > other.__value
def __lt__(self, other):
return self.__value < other.__value
something = Thing(100)
nothing = Thing(0)
# True
something > nothing
# False
something < nothing
# Error
something + nothing
Copy the code
pprint
Python’s pprint module provides a way to correctly display data of Python’s known types in a format that can be parsed by a parser and is easy to read, making it a must-have method for Python developers.
import requests
import pprint
url = 'https://randomuser.me/api/? results=1'
users = requests.get(url).json()
pprint.pprint(users)
Copy the code
Queue
The Queue module is a Python module that provides Queue operations and provides many conveniences for supporting multithreaded operations.
The rule of this structure is “first in, first out”. Where “first in, first out” (FIFO) means that queues allow us to retrieve objects in the order they are added. Last in, first out (LIFO) allows us to access newly added objects. And PriorityQueue looks at the level of the PriorityQueue, who’s lower, who comes out first.
__repr__
When defining a class or object in Python, it can be useful to provide a “normal” way to represent that object as a string. Such as:
>>> file = open('file.txt'.'r') > > >print(file)
<open file 'file.txt', mode 'r' at 0x10d30aaf0>
Copy the code
This makes for easier debugging. If you want to put it in a defined class:
class someClass:
def __repr__(self):
return "<some description here>"
someInstance = someClass()
# prints <some description here>
print(someInstance)
Copy the code
sh
Python is a great scripting language, but having to use its standard OS and subprocess libraries for every script you write is too restrictive.
Sh is a mature Python subprocess interface that allows you to call any program as if it were a function.
from sh import *
sh.pwd()
sh.mkdir('new_folder')
sh.touch('new_file.txt')
sh.whoami()
sh.echo('This is great! ')
Copy the code
Type hints
Python is a dynamically class language: when defining variables, functions, classes, and so on, we don’t need to specify data types.
While this approach greatly reduces development time, once we’ve written so much code, we tend to forget exactly what each type is, and it’s too much trouble to go back and read it carefully.
Starting with Python 3.5, you can optionally provide type annotation hints when defining functions:
def addTwo(x : Int) -> Int:
return x + 2
Copy the code
You can also define type aliases:
from typing import List
Vector = List[float]
Matrix = List[Vector]
def addMatrix(a : Matrix, b : Matrix) -> Matrix:
result = []
for i,row in enumerate(a):
result_row =[]
for j, col in enumerate(row):
result_row += [a[i][j] + b[i][j]]
result += [result_row]
returnThe result of x = [[1.0, 0.0], [0.0, 1.0]] y = [[2.0, 1.0], [0.0, 2.0]] z = addMatrix (x, y)Copy the code
While not mandatory, type annotations can make your code easier to understand. In addition, it allows us to catch stray TypeErrors when running the type checking tool, which is helpful for large and complex projects.
UUID
The most convenient way to generate universal unique identifiers (UUID) is through the UUID module of the Python standard library. The probability of it generating a duplicate ID is very low, less than one in a billion.
import uuid
user_id = uuid.uuid4()
print(user_id)
Copy the code
The code above creates a random 128-bit number, which is almost certainly unique. In fact, we can use it to generate more than one possible UUID, soak up the order of magnitude: 5000000000000000000000000000000000000000.
Virtual environments
Typically, we use one version of Python for multiple projects, but sometimes different projects, or even one project, use different versions of Python, with different packages. What do we do?
Python’s support for virtual environments helps us solve this problem:
python -m venv my-project
source my-project/bin/activate
pip install all-the-modules
Copy the code
wikipedia
A lot of free knowledge can be accessed programmatically through the wikipedia API, which is very convenient.
import wikipedia
result = wikipedia.page('freeCodeCamp')
print(result.summary)
for link in result.links:
print(link)
Copy the code
Like the real site, the module has support for multiple languages, disambiguation pages, random page searches, and even donate(Wikipedia is going broke, help us!). .
xkcd
Python is so popular that humor is essential. After all, its name comes from the British surreal comedy troupe Monty Python. The egg is hidden in most of its official documentation.
Input:
import antigravity
Copy the code
You will see:
Ground: You’re in the sky! How did you do that?
Sky: Python!
Sky: I learned Python last night! That’s easy! — Just print “Hello World! Hello World!
Ground: Meow meow meow? Dynamic type? Universal space?
Sky: Join us! Programming is fun again! It’s a whole new world here!
Ground: how did you get up there?
I just wrote import Antigravity…
Ground: This is it?
Sky: And then for comparison, I took all the pills in the medicine cabinet. But I still think Python is what makes me “fly.”
YAML
YAML full name YAML Is Not Markup Language, it Is a data formatting Language, very simple and powerful.
YAML is considered by many to be a superior file format to XML and JSON. In contrast to XML, it is simple enough to use, in addition to having many of the advantages of XML. For JSON, YAML can be written as a normalized configuration file.
Install the YAML module:
$ pip install pyyaml
Copy the code
Import into the project:
import yaml
Copy the code
PyYAML lets you store Python objects of any data type, as well as instances of any user-defined class.
zip
In Python, the zip() function takes an iterable object as an argument, packs the corresponding elements of the object into tuples, and returns a list of those tuples.
keys = ['a'.'b'.'c']
vals = [1, 2, 3]
zipped = dict(zip(keys, vals))
Copy the code
If the iterators do not have the same number of elements, it returns the same list length as the shortest object. Using the * operator, you can unpack the tuple into a list. (In Python 3.x, to reduce memory, zip() returns an object.)