current position:Home>24 useful Python tips
24 useful Python tips
2022-02-01 18:40:50 【Machine Learning Institute】
This article is published in my official account 『 data STUDIO』:24 A good one Python Practical skills
Python Is the most popular in the world 、 One of the most popular programming languages , Yes, there are many reasons .
- It's easy to learn
- There are super many functions
- It has a large number of modules and Libraries
As a data worker , We use it every day Python Handle most of the work . In the process , We will continue to learn some useful skills and tricks .
ad locum , I tried to A - Z Share some of these tips in the beginning format , And these methods are briefly introduced in this paper , If you are interested in one or more of them , You can check the official documents through the references at the end of the article . I hope it can help you .
all or any
Python One of the many reasons why languages are so popular , Because it has good readability and expressiveness .
People often joke that Python yes Executable pseudocode
. When you can write code like this , It's hard to refute .
x = [True, True, False]
if any(x):
print(" At least one True")
if all(x):
print(" Is full of True")
if any(x) and not all(x):
print(" At least one True And a False")
Copy code
bashplotlib
Have you ever thought about drawing graphics in the console ?
Bashplotlib It's a Python library , He can help us on the command line ( A rough environment ) Draw data in .
# Module installation
pip install bashplotlib
# Draw examples
import numpy as np
from bashplotlib.histpgram import plot_hist
arr = np.ramdom.normal(size=1000, loc=0, scale=1)
plot_hist(arr, bincount=50)
Copy code
collections
Python There are some great default data types , But sometimes their behavior doesn't exactly meet your expectations .
Fortunately, ,Python The standard library provides collections modular . This convenient add-on provides you with more data types .
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 code
dir
Have you ever thought about how to check Python Object and see what properties it has ? Enter... On the command line :
dir()
dir("Hello World")
dir(dir)
Copy code
When running interactively Python And dynamically explore the objects and modules you are using , This can be a very useful feature . Read more here functions Related content .
emoji
emoji It is a visual emotional symbol used in wireless communication in Japan , Drawing refers to drawing , Words refer to characters , Can be used to represent a variety of expressions , A smiling face means a smile 、 Cake means food, etc . On Chinese mainland ,emoji Is often called “ Little yellow face ”, Or call it emoji.
# Install the module
pip install emoji
# Make a try
from emoji import emojize
print(emojize(":thumbs_up:"))
Copy code
from __future__ import
Python One of the results of popularity , There are always new versions under development . The new version means new features —— Unless your version is out of date .
But don't worry . Use this __future__ modular Can help you use Python Future version import function . Literally , It's like time travel 、 Magic or something .
from __future__ import print_function
print("Hello World!")
Copy code
geogy
Geography , This is a challenging area for most programmers . When getting geographic information or drawing maps , There will be many problems . This geopy modular Make geography related content very easy .
pip install geopy
Copy code
It abstracts a series of different geocoding services API Come to work . Through it , You can get the full street address of a place 、 latitude 、 Longitude and even altitude .
There is also a useful distance class . It calculates the distance between two positions in your preferred unit of measurement .
from geopy import GoogleV3
place = "221b Baker Street, London"
location = GoogleV3().geocode(place)
print(location.address)
print(location.location)
Copy code
howdoi
When you use terminal When programming the terminal , By having a problem in StackOverflow Search for answers on , After that, it will return to the terminal to continue programming , Sometimes you don't remember the solution you found before , You need to review StackOverflow, But I don't want to leave the terminal , Then you need to use this useful command line tool howdoi.
pip install howdoi
Copy code
Whatever problems you have , You can ask it , It will try its best to reply .
howdoi vertical align css
howdoi for loop in java
howdoi undo commits in git
Copy code
But please pay attention to —— It will be from StackOverflow Grab the code from the best answer . It may not always provide the most useful information ......
howdoi exit vim
Copy code
inspect
Python Of inspect modular Perfect for understanding what's going on behind the scenes . You can even call its own methods !
The following code example inspect.getsource()
For printing your own source code . inspect.getmodule()
It is also used to print the module that defines it .
The last line of code prints its own line number .
import inspect
print(inspect.getsource(inspect.getsource))
print(inspect.getmodule(inspect.getmodule))
print(inspect.currentframe().f_lineno)
Copy code
Of course , Except for these trivial uses ,inspect Modules can prove useful for understanding what your code is doing . You can also use it to write self documenting code .
Jedi
Jedi Library is an autocomplete and code analysis library . It makes writing code faster 、 More efficient .
Unless you're developing your own IDE, Otherwise you may use Jedi I'm interested in being an editor plug-in . Fortunately, , There are already loads available !
**kwargs
When learning any language , There will be many milestones . Use Python And understand the mysterious **kwargs
Grammar may count as an important milestone .
Double asterisk in front of dictionary object **kwargs Allows you to pass the contents of the dictionary as named parameters to the function .
The key of the dictionary is the parameter name , Value is the value passed to the function . You don't even need to call it kwargs
!
dictionary = {"a": 1, "b": 2}
def someFunction(a, b):
print(a + b)
return
# These do the same thing :
someFunction(**dictionary)
someFunction(a=1, b=2)
Copy code
When you want to write a function that can handle undefined named parameters , It's very useful .
list (list) The derived type
About Python Programming , One of my favorite things is its List derivation .
These expressions make it easy to write very smooth code , Almost like natural language .
numbers = [1,2,3,4,5,6,7]
evens = [x for 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 code
map
Python Support functional programming with many built-in features . The most useful map()
One of the functions is the function —— Especially with lambda function When used in combination .
x = [1, 2, 3]
y = map(lambda x : x + 1, x)
# Print out [2,3,4]
print(list(y))
Copy code
In the example above ,map()
Put a simple lambda
Function applied to x
. It returns a mapping object , The object can be converted into some iteratable objects , For example, list or tuple .
newspaper3k
If you haven't seen it yet , Then be ready to be Python newspaper module The module shocked to . It enables you to retrieve news articles and related metadata from a series of leading international publications . You can retrieve images 、 Text and author's name . It even has some Built in NLP function .
therefore , If you are considering using in your next project BeautifulSoup
Or something else DIY Web crawling Library , Using this module can save you a lot of time and energy .
pip install newspaper3k
Copy code
Operator overloading
Python Provide right Operator overloaded Support , This is one of the terms that makes you sound like a legitimate computer scientist .
This is actually a simple concept . Have you ever thought about why Python Allow you to use +
Operator to add numbers and connection strings ? This is what operator overloading does .
You can define how to use... In your own specific way Python The object of the standard operator symbol . And you can use them in the context of the object you are using .
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 code
pprint
Python Default print
The function does its job . But if you try to use print
Function prints out any large nested objects , The result is rather ugly . The beautiful printing module of this standard library pprint You can print out complex structured objects in an easy to read format .
This is anything that uses non trivial data structures Python A must-have for developers .
import requests
import pprint
url = 'https://randomuser.me/api/?results=1'
users = requests.get(url).json()
pprint.pprint(users)
Copy code
Queue
Python Standard library Queue The module implementation supports multithreading . This module allows you to implement queue data structures . These are data structures that allow you to add and retrieve entries according to specific rules .
“ fifo ”(FIFO) Queues allow you to retrieve objects in the order they are added .“ Last in, first out ”(LIFO) Queues allow you to access recently added objects first .
Last , Priority queues allow you to retrieve objects according to their sort order .
This is a how in Python Queue is used in Queue Examples of multithreaded programming .
__repr__
stay Python When a class or object is defined in , Provides an... That represents the object as a string “ official ” This method is very useful . for example :
>>> file = open('file.txt', 'r')
>>> print(file)
<open file 'file.txt', mode 'r' at 0x10d30aaf0>
Copy code
This makes it easier to debug code . Add it to your class definition , As shown below :
class someClass:
def __repr__(self):
return "<some description here>"
someInstance = someClass()
# Print <some description here>
print(someInstance)
Copy code
sh
Python It's a great scripting language . Sometimes standard os and subprocess Ku may have a headache .
The SH library Allows you to call any program like a normal function —— Useful for automating workflows and tasks .
import sh
sh.pwd()
sh.mkdir('new_folder')
sh.touch('new_file.txt')
sh.whoami()
sh.echo('This is great!')
Copy code
Type hints
Python It is a dynamically typed language . Defining variables 、 function 、 Class does not need to specify the data type . This allows fast development time . however , Nothing is more annoying than runtime errors caused by simple input problems .
from Python 3.5 Start , You can choose to provide type hints when defining functions .
def addTwo(x : Int) -> Int:
return x + 2
Copy 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]
return result
x = [[1.0, 0.0], [0.0, 1.0]]
y = [[2.0, 1.0], [0.0, -2.0]]
z = addMatrix(x, y)
Copy code
Although not mandatory , But type annotations can make your code easier to understand .
They also allow you to use type checking tools , Catch those stray before running TypeError. If you are dealing with large 、 Complex projects , It's very useful !
uuid
adopt Python Standard library uuid modular Generate universal unique ID( or “UUID”) A quick and simple method .
import uuid
user_id = uuid.uuid4()
print(user_id)
Copy code
This will create a random 128 Digit number , That number is almost certainly unique . in fact , Can generate more than 2¹²² It's possible UUID. This is more than five decimal ( or 5,000,000,000,000,000,000,000,000,000,000,000,000).
The probability of finding duplicates in a given set is very low . Even if there are a trillion UUID, The possibility of repetition is also far less than one in a billion .
Virtual environments
You may be in multiple... At the same time Python Work on projects . Unfortunately , Sometimes two projects will depend on different versions of the same dependency . What did you install on your system ?
Fortunately, ,Python Support for A virtual environment So you can have the best of both worlds . From the command line :
python -m venv my-project
source my-project/bin/activate
pip install all-the-modules
Copy code
Now? , You can run on the same machine Python Stand alone version and installation of .
wikipedia
Wikipedia has a great API, It allows users to programmatically access unparalleled and completely free knowledge and information . stay wikipedia modular Make access to the API Very convenient .
import wikipedia
result = wikipedia.page('freeCodeCamp')
print(result.summary)
for link in result.links:
print(link)
Copy code
Just like the real site , The module provides multilingual support 、 Page disambiguation 、 Random page search , There's even one donate()
Method .
xkcd
Humor is Python A key feature of language , It is based on English comedy skits Python Flying Circus Named .Python Many official documents cite the program's most famous sketches . however ,Python Humor is not limited to documents . Try running the following line :
import antigravity
Copy code
YAML
YAML refer to “ Unmarked language ” . It's a data format language , yes JSON Superset .
And JSON Different , It can store more complex objects and reference its own elements . You can also write notes , Make it especially suitable for writing configuration files . The PyYAML modular You can use YAML Use Python.
Install and then import into your project :
pip install pyyaml
import yaml
Copy code
PyYAML Allows you to store... Of any data type Python object , And any instances of user-defined classes .
zip
The finale is also a great module . Have you ever encountered the need to form a dictionary from two lists ?
keys = ['a', 'b', 'c']
vals = [1, 2, 3]
zipped = dict(zip(keys, vals))
Copy code
The zip()
Built in functions require a series of iteratable objects , And return a tuple list . Each tuple groups the elements of the input object according to the location index .
You can also call objects to “ decompression ” object *zip()
.
At the end
Python It is a very diverse and well developed language , So there will certainly be many functions I haven't considered . If you want to know more about python modular , You can refer to awesome-python.
Copyright notice
Welcome to share , Please indicate the author and the original link in the article , Thank you for your respect for knowledge and affirmation of this article .
Original author : data STUDIO
Link to the original text :mp.weixin.qq.com/s/U28UvPtFc…
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source , Relevant responsibilities will be investigated for infringement reprint
copyright notice
author[Machine Learning Institute],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011840480913.html
The sidebar is recommended
- Python data analysis - linear regression selection fund
- How to make a python SDK and upload and download private servers
- Python from 0 to 1 (day 20) - basic concepts of Python dictionary
- Django -- closure decorator regular expression
- Implementation of home page and back end of Vue + Django tourism network project
- Easy to use scaffold in Python
- [Python actual combat sharing] I wrote a GIF generation tool, which is really TM simple (Douluo continent, did you see it?)
- [Python] function decorators and common decorators
- Explain the python streamlit framework in detail, which is used to build a beautiful data visualization web app, and practice making a garbage classification app
- Construction of the first Django project
guess what you like
-
Python crawler actual combat, pyecharts module, python realizes the visualization of river review data
-
Python series -- web crawler
-
Plotly + pandas + sklearn: shoot the first shot of kaggle
-
How to learn Python systematically?
-
Analysis on several implementations of Python crawler data De duplication
-
leetcode 1616. Split Two Strings to Make Palindrome (python)
-
Python Matplotlib drawing violin diagram
-
Python crawls a large number of beautiful pictures with 10 lines of code
-
[tool] integrated use of firebase push function in Python project
-
How to use Python to statistically analyze access logs?
Random recommended
- How IOS developers learn Python Programming 22 - Supplement 1
- Python can meet any API you need
- Python 3 process control statement
- The 20th of 120 Python crawlers, 1637. All the way business opportunity network joined in data collection
- Datetime of pandas time series preamble
- How to send payslips in Python
- [Python] closure and scope
- Application of Python Matplotlib color
- leetcode 1627. Graph Connectivity With Threshold (python)
- Python thread 08 uses queues to transform the transfer scenario
- Python: simple single player strange game (text)
- Daily python, chapter 27, Django template
- TCP / UDP communication based on Python socket
- Use of pandas timestamp index
- leetcode 148. Sort List(python)
- Confucius old book network data collection, take one anti three learning crawler, python crawler 120 cases, the 21st case
- [HTB] cap (datagram analysis, setuid capability: Python)
- How IOS developers learn Python Programming 23 - Supplement 2
- How to automatically identify n + 1 queries in Django applications (2)?
- Data analysis starts from scratch. Pandas reads HTML pages + data processing and analysis
- 1313. Unzip the coding list (Java / C / C + + / Python / go / trust)
- Python Office - Python edit word
- Collect it quickly so that you can use the 30 Python tips for taking off
- Strange Python strip
- Python crawler actual combat, pyecharts module, python realizes China Metro data visualization
- DOM breakpoint of Python crawler reverse
- Django admin custom field stores links in the database after uploading files to the cloud
- Who has powder? Just climb who! If he has too much powder, climb him! Python multi-threaded collection of 260000 + fan data
- Python Matplotlib drawing streamline diagram
- The game comprehensively "invades" life: Python releases the "cool run +" plan!
- Python crawler notes: use proxy to prevent local IP from being blocked
- Python batch PPT to picture, PDF to picture, word to picture script
- Advanced face detection: use Dlib, opencv and python to detect face markers
- "Python 3 web crawler development practice (Second Edition)" is finally here!!!!
- Python and Bloom filters
- Python - singleton pattern of software design pattern
- Lazy listening network, audio novel category data collection, multi-threaded fast mining cases, 23 of 120 Python crawlers
- Troubleshooting ideas and summary of Django connecting redis cluster
- Python interface automation test framework (tools) -- interface test tool requests
- Implementation of Morse cipher translator using Python program