Python函数

Python函数

  1.   系统提供内部函数
  2.   第三方提供的函数
  3.   自定义函数

系统库函数

  字符函数库

>>> s = 'bling'
>>> s.islower()
True
>>> s = 'Bling'
>>> s.islower()
False
>>> help(str.islower)
Help on method_descriptor:

islower(...)
    S.islower() -> bool
    
    Return True if all cased characters in S are lowercase and there is
    at least one cased character in S, False otherwise.

>>> s2 = 'Bling'
>>> s2.islower()
False
>>> help(str)
>>> help(str.replace)
Help on method_descriptor:

replace(...)
    S.replace(old, new[, count]) -> string
    
    Return a copy of string S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.

>>> s4 = 'hhhnnjmjmjmjababababababababababab2222'
>>> s4.replace('ab','AB')
'hhhnnjmjmjmjABABABABABABABABABABAB2222'

  数学函数库

>>> import math
>>> val = math.sin(3.14/6)
>>> print(val)
0.499770102643
>>> math.pi
3.141592653589793
>>> val = math.sin(math.pi/6)
>>> print(val)
0.5
>>> val = math.cos(math.pi/6)
>>> print(val)
0.866025403784
>>> help(math)
Help on built-in module math:
>>> 3*3*3*3
81
>>> math.pow(3,4)
81.0
>>> 3**4
81
>>> 3*3*3*3*3*3*3
2187
>>> 3**7
2187
>>> math.pow(3,7)
2187.0
>>> 

  操作系统库

>>> import os
>>> os.getcwd()
'C:\\Users\\bling\\Desktop'
>>> help(os.getcwd)
Help on built-in function getcwd in module nt:

>>> currentdir = os.getcwd()
>>> print currentdir
C:\Users\bling\Desktop
>>> help(listdir)
>>> help(os.listdir)
Help on built-in function listdir in module nt:

listdir(...)
    listdir(path) -> list_of_strings
    
    Return a list containing the names of the entries in the directory.
    
        path: path of directory to list
    
    The list is in arbitrary order.  It does not include the special
    entries '.' and '..' even if they are present in the directory.

>>> ldirs = os.listdir(currentdir)
>>> print ldirs
['00\xc9\xe8\xb1\xb8\xc0\xe0\xb1\xf0.xls', '1.png', '360\xd2\xbb\xbc\xfcRoot.lnk', '360\xd4\xc6\xc5\xcc.lnk', '360\xb0\xb2\xc8\xab\xce\xc0\xca\xbf.lnk', '360\xca\xd6\xbb\xfa\xd6\xfa\xca\xd6.lnk', '360\xc8\xed\xbc\xfe\xb9\xdc\xbc\xd2.lnk', '3DCIMP.unity3d', 'CheckExcel', 'desktop.ini', 'eclipse - \xbf\xec\xbd\xdd\xb7\xbd\xca\xbd.lnk', 'eclipse.lnk', 'EditPlus 3.lnk', 'Edraw.exe - \xbf\xec\xbd\xdd\xb7\xbd\xca\xbd.lnk', 'EVM-DEMO', 'EVM\xc6\xf3\xd2\xb5\xbf\xc9\xca\xd3\xbb\xaf\xb9\xdc\xc0\xed\xc6\xbd\xcc\xa8\xd3\xc3\xbb\xa7\xca\xd6\xb2\xe1 v1.1.doc', 'Foxmail - \xbf\xec\xbd\xdd\xb7\xbd\xca\xbd.lnk', 'FSCapture.lnk', 'goagent.exe - \xbf\xec\xbd\xdd\xb7\xbd\xca\xbd.lnk', 'Google Chrome.lnk', 'install.sql', 'JsonView.exe.lnk', 'myeclipse - \xbf\xec\xbd\xdd\xb7\xbd\xca\xbd.lnk', 'Navicat for MySQL.lnk', 'plsqldev - \xbf\xec\xbd\xdd\xb7\xbd\xca\xbd.lnk', 'ppturl.pptx', 'PUTTY.EXE - \xbf\xec\xbd\xdd\xb7\xbd\xca\xbd.lnk', 'startup.bat.lnk', 'STS.exe.lnk', 'ttt', 'UltraISO - \xbf\xec\xbd\xdd\xb7\xbd\xca\xbd.lnk', 'WinSCP - \xbf\xec\xbd\xdd\xb7\xbd\xca\xbd.lnk', '[\xc3\xc0\xba\xd3\xd1\xa7\xcf\xb0\xd4\xda\xcf\xdfeimhe.com]linux\xd4\xcb\xce\xac\xd6\xd0\xbc\xb6\xc3\xe6\xca\xd4\xcc\xe2.pdf', '\xc4\xda\xb2\xbf\xb5\xda\xd2\xbb\xb4\xce\xd1\xdd\xca\xbe\xd7\xdc\xbd\xe1.docx', '\xd0\xc2\xbd\xa8\xce\xc4\xbc\xfe\xbc\xd0', '\xd0\xc2\xbd\xa8\xce\xc4\xbc\xfe\xbc\xd0 (2)', '\xbb\xfa\xb9\xf1\xd6\xd0\xc9\xe8\xb1\xb8\xba\xcb\xb6\xd4.txt', '\xc4\xa3\xd0\xcd\xc1\xac\xbd\xd3.txt', '\xb0\xd9\xb6\xc8\xd4\xc6\xb9\xdc\xbc\xd2.lnk', '\xc9\xe8\xb1\xb8\xca\xd9\xc3\xfc\xd4\xa4\xb2\xe2-\xbd\xd8\xcd\xbc.docx', '\xc8\xfc\xb6\xfb-\xc1\xd9\xca\xb1\xc4\xbf\xc2\xbc', '\xc8\xfc\xb6\xfb-\xd0\xde\xb8\xc4\xba\xf3\xca\xfd\xbe\xdd', '\xc8\xfc\xb6\xfb-\xb3\xf5\xca\xbc\xca\xfd\xbe\xdd', '\xc8\xfc\xb6\xfb\xd7\xee\xd6\xd5', '\xc8\xfc\xb6\xfb\xcf\xee\xc4\xbf\xbb\xe3\xb1\xa8PPT\xb4\xf3\xb8\xd9.docx', '\xb4\xed\xce\xf3\xc9\xe8\xb1\xb8\xc1\xac\xcf\xdf.txt']
>>> 

  网络编程库

>>> import socket
>>> baiduip = socket.gethostbyname('www.baidu.com')
>>> print baiduip
61.135.169.105
>>> help(socket.gethostbyname)
Help on built-in function gethostbyname in module _socket:

gethostbyname(...)
    gethostbyname(host) -> address
    
    Return the IP address (a string of the form '255.255.255.255') for a host.

>>> help(socket)

第三方提供的函数httplib2

  linux安装第三方函数库

    easy_install httplib2

  windows安装第三方库

1 下载模块

   到 “https://code.google.com/p/httplib2/” 下载一款适合你的压缩包“httplib2-0.4.0.zip”

2 解压下载的压缩包“httplib2-0.4.0.zip”到某目录下

3配置python在dos下的运行环境

  (之前需要配置系统环境变量,在系统环境变量Path后添加python安装目录,例如c:\python2.7)

 

4 dos下安装httpLib2模块

 进入httplib2-0.4.0.zip的解压目录,运行python settup.py install 进行安装

  使用第三方库

 

##简单的网络爬虫,下载163主页,并用默认浏览器的tab页打开
import urllib
import webbrowser as web
url = 'http://www.163.com'
content = urllib.urlopen(url).read()
open('D:/work_space/pythonWork/python_example/163.com.html','w').write(content)
web.open_new_tab('D:/work_space/pythonWork/python_example/163.com.html')
web.open_new_tab('http://www.baidu.com')

posted on 2014-07-17 22:53  森林行走  阅读(1521)  评论(0编辑  收藏  举报

导航