Archives February 2016
Between the characters and numbers in Python is how to compare the size?
Python set
Written by
on
in
Notes.
1.Method for creating a set
1)s={1,"python"}
2)s=set("python") or s=set([1,"python"])
ps. set to list:
lst=list(s)
list to set(can remove duplicate elements from the set):
s=set([1,"python"])
Create an immutable set in "frozenset"
2.Common method of set
1)add
eg.
b ...
Implementation of Case Swithch syntax in Python
Written by
on
in
Notes.
Python itself has no switch statement, there are 3 kinds of Solutions:
1.use dictionary
cases = {
case1: do_some_stuff1,
case2: do_some_stuff2,
...
caseN: do_some_stuffN,
}
cases.get(var, do_default_stuff)()
2.use lambda
result = {
'a': lambda x: x * 5,
'b': lambda x: x + 7,
'c': lambda x: x - 2
}[value](x)
3.Beck Brian provides a class switch to ...
Python basic usage of pickle
Written by
on
in
Notes.
Python pickle
import pickle
#dumps(object) :Object serialization
lista=["john","levi","kate"]
listb=pickle.dumps(lista)
#print listb
#loads(string) :The object is restored, and the object type is restored to the original format.
listc=pickle.loads(listb)
#print listc
#dump(object,file) :To store objects in a file.
group1=("cat","is","kkkk")
f1=file ...
Python basic grammar
Written by
on
in
Notes.
python basic grammar
1.Constant and Variable
1)Constant
eg.
import const
const.value=5
print const.value
const.value=6
2)Variable
eg.
a=1
2.Number and string
1)Single quotation mark
eg. a='string'
2)Double quotation marks
eg. b="string"
3)Three single quotation marks (output special characters)
eg. c='''string ...
Python read and write files
Written by
on
in
Notes.
Python read and write file
1.Python read and write file
fr=open("readfile.txt","r")
fw=open("writefile.txt","w")
print fr.readline()
print fr.tell()
print fr.readlines()
fw.write("===write line===")
fw.close()
fr.seek(0,0) #The first parameter represents the number of bytes, after which a parameter represents the relative ...
Python randomly generated a 6 bit verification code
Written by
on
.
source code:
# -*- coding: utf-8 -*-
import random
def generate_verification_code():
''' randomly generated a 6 bit verification code '''
code_list = []
for i in range(10): # 0-9 number
code_list.append(str(i))
for i in range(65, 91): # A-Z
code_list.append(chr(i))
for i in range(97, 123): # a-z
code_list.append(chr(i))
myslice = random.sample(code_list, 6)
verification_code ...