The following are all based on the PYTHON3 environment but (also support 2.7 only to note the syntax)First, add the interpreterWhen writing a file, you need to identify which interpreter to execute and add a Chinese interpreter.1 #!/usr/bin/env
Use of the Itertools module:#Coding=utf-8"""Itertools Module"""ImportItertoolsImportUnitTestclassTestitertools (unittest. TestCase):defTest_count (self):"""Itertools.count () infinite generation of natural numbers"""natuals= Itertools.count (1, 1)
Type one: serialization of form dataErrordict need to be used at this time.ret[' Errors ' = obj.errors.as_data () result = Json.dumps (ret, cls=jsoncustomencoder) #JsonCustomEncoder是自定义的cls, Used to convert a special error field type in RET return
ObjectiveThe basics of regular expressions do not say, interested can click here, extract the general in two cases, one is to extract the text to extract a single position of the string, and the other is to extract a number of consecutive positions
In the previous article we talked about Python's function as a return value and closure concept, we continue to explain the function to do parameters and adorners, this function is very convenient and practical, can greatly simplify the code, let's
1. Get the current working directory in PythonCurDir = OS.GETCWD ()OS.GETCWD () returns the directory where the command was executed, not the directory in which the script itself resides2, Os.path
Os.path.abspath (path) #返回绝对路径
Os.path.split
In the above code,%s is a placeholder, also called the formatting of a string. Integer is%d, floating-point number is%f, but%s can include all, why not%s ... Also less remember things, so lazy ...The number of%s in quotation marks in print is the
def binary_search (data_source,find_n):Mind=int (len (data_source)/2)If Len (data_source) >=1:If Data_source[mid]>find_n:Print ("Data in left of [%s]"%sdata_souerce[mid])Print (Data_souerce[:mid]Binary_search (Data_source[:mid],find_n) Elif
Python Basic Instruction Pack and class usage
Create a folder Filepackage
Create __init__.py within the Filepackage folder
With __init__.py, Filepackage is a package, otherwise it is just a normal folder.
Create file.py within the Filepackage
This article describes a Python implementation method that reads the last n rows of a file. Share to everyone for your reference, as follows:
#-*-Coding:utf8-*-import osimport timeimport datetimeimport mathimport stringdef get_last_line
Background
Today, I see someone asking if a Python class has an attribute that is similar to a public property, which modifies the corresponding property of an instance, modifies the corresponding properties of all instances of the class, and then
Morning up idle to do nothing, inexplicably pop up embarrassing things Encyclopedia of the jokes, then a think since you come to, then I write a crawler to your site crawl, one as practice practiced hand, and second also count for some fun.
In
Python list and pythonlist
Operation
1. Add
Append: adds an element to the end of the list.
Insert (n, element value): Add an element to the specified index
2. Delete
Pop (n): WHEN n is specified, the element at n is deleted. If n is not
File Operations, c ++ File OperationsFile Operations
File Operation Procedure
The existing file is as follows: the file name is "song"
1 hiding from the rain and snow 2 hiding in the rain and snow 3 trying to forget but I won't let go 4 try to
For more information, see the following two methods:
It is often used in development to traverse the dictionary, list, and other data cyclically. However, in python, Dictionary traversal is very unfamiliar to many beginners, today, let's talk about
Python dictionary setdefault () method tutorial, pythonsetdefault
Preface
As mentioned in the basic knowledge of python, the dictionary is a variable data type, and its parameters are key-pair values. The setdefault () method is similar to the get ()
Python implements scheduled tasks and Python implements tasks
There are many ways to implement scheduled tasks in Python. Below are several
Loop sleep:
This is the easiest way to put the task to be executed in a loop, and then run it after sleep for
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service