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:UserslingDesktop
>>> 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
['00xc9xe8xb1xb8xc0xe0xb1xf0.xls', '1.png', '360xd2xbbxbcxfcRoot.lnk', '360xd4xc6xc5xcc.lnk', '360xb0xb2xc8xabxcexc0xcaxbf.lnk', '360xcaxd6xbbxfaxd6xfaxcaxd6.lnk', '360xc8xedxbcxfexb9xdcxbcxd2.lnk', '3DCIMP.unity3d', 'CheckExcel', 'desktop.ini', 'eclipse - xbfxecxbdxddxb7xbdxcaxbd.lnk', 'eclipse.lnk', 'EditPlus 3.lnk', 'Edraw.exe - xbfxecxbdxddxb7xbdxcaxbd.lnk', 'EVM-DEMO', 'EVMxc6xf3xd2xb5xbfxc9xcaxd3xbbxafxb9xdcxc0xedxc6xbdxccxa8xd3xc3xbbxa7xcaxd6xb2xe1 v1.1.doc', 'Foxmail - xbfxecxbdxddxb7xbdxcaxbd.lnk', 'FSCapture.lnk', 'goagent.exe - xbfxecxbdxddxb7xbdxcaxbd.lnk', 'Google Chrome.lnk', 'install.sql', 'JsonView.exe.lnk', 'myeclipse - xbfxecxbdxddxb7xbdxcaxbd.lnk', 'Navicat for MySQL.lnk', 'plsqldev - xbfxecxbdxddxb7xbdxcaxbd.lnk', 'ppturl.pptx', 'PUTTY.EXE - xbfxecxbdxddxb7xbdxcaxbd.lnk', 'startup.bat.lnk', 'STS.exe.lnk', 'ttt', 'UltraISO - xbfxecxbdxddxb7xbdxcaxbd.lnk', 'WinSCP - xbfxecxbdxddxb7xbdxcaxbd.lnk', '[xc3xc0xbaxd3xd1xa7xcfxb0xd4xdaxcfxdfeimhe.com]linuxxd4xcbxcexacxd6xd0xbcxb6xc3xe6xcaxd4xccxe2.pdf', 'xc4xdaxb2xbfxb5xdaxd2xbbxb4xcexd1xddxcaxbexd7xdcxbdxe1.docx', 'xd0xc2xbdxa8xcexc4xbcxfexbcxd0', 'xd0xc2xbdxa8xcexc4xbcxfexbcxd0 (2)', 'xbbxfaxb9xf1xd6xd0xc9xe8xb1xb8xbaxcbxb6xd4.txt', 'xc4xa3xd0xcdxc1xacxbdxd3.txt', 'xb0xd9xb6xc8xd4xc6xb9xdcxbcxd2.lnk', 'xc9xe8xb1xb8xcaxd9xc3xfcxd4xa4xb2xe2-xbdxd8xcdxbc.docx', 'xc8xfcxb6xfb-xc1xd9xcaxb1xc4xbfxc2xbc', 'xc8xfcxb6xfb-xd0xdexb8xc4xbaxf3xcaxfdxbexdd', 'xc8xfcxb6xfb-xb3xf5xcaxbcxcaxfdxbexdd', 'xc8xfcxb6xfbxd7xeexd6xd5', 'xc8xfcxb6xfbxcfxeexc4xbfxbbxe3xb1xa8PPTxb4xf3xb8xd9.docx', 'xb4xedxcexf3xc9xe8xb1xb8xc1xacxcfxdf.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')
原文地址:https://www.cnblogs.com/yangml/p/3852351.html