Tag archives: Python

RSS feed of Python

Python list

Python list

a.Method for creating a list

     1.create list
     eg.
        >>> a=["python",25,89.9]
        >>> a
        ["python",25,89.9]
     2.list index   
     eg.
        >>> a=["python",25,89.9]
        >>> a[0]
        'python'
        >>> a[1]
        25
        >>> a[2]
        89.9
        >>> a[-1]
        89.9
        #get the index ...

Continue reading

Between the characters and numbers in Python is how to compare the size?

Between the characters and numbers in Python is how to compare the size?

1. any two objects can be compared

2. The same type of objects (instances), if it is digital type (int/float/long/complex), in accordance with the simple size to compare; if is non numeric type and class (type) definition of the ...

Continue reading

Python set

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 ...

Continue reading

Implementation of Case Swithch syntax in Python

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 ...

Continue reading

Python basic usage of pickle

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 ...

Continue reading

Python basic grammar

python basic grammar

1.Constant and Variable

   1)ļ¼£onstant
    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 ...

Continue reading

Python read and write files

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 ...

Continue reading

Python randomly generated a 6 bit verification code

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 ...

Continue reading

virtualenv -- python Virtual Environment

1. Installation pip

 pip install virtualenv

2. Creating a Virtual Environment

virtualenv envfolder(Virtual environment name)

3. Start Virtual Environment

After you create a successful, will generate the corresponding directory files in the current directory.

cd envfolder/

Then start

source ./bin/activate

4.Quit Virtual Environment

   deactivate

CentOS yum upgrade Python 2.6 to 2.7

CentOS system comes python2.6.6 by yum upgrade to python2.7

1.Install Upgrade
wget http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm  
(x86_64) 
or
wget http://li.nux.ro/download/nux/dextop/el6/i386/nux-dextop-release-0-2.el6.nux.noarch.rpm 
(i386)
rpm -ivh nux-dextop-release-0-2.el6.nux.noarch.rpm ...

Continue reading