zoukankan      html  css  js  c++  java
  • Python 常用函数大体分类

    ==================系统库函数================

    字符串函数

    举例数学函数

    import math

    val=math.sin(3.14/6)

    val=math.sin(math.pi/6)

    >>> 3*3*3*3

    81

    >>> math.pow(3,4)     #3的4次方

    81.0

    举例操作系统函数

    >>> import os

    >>> os.getcwd()    #列出当前目录

    'C:\Users\Administrator\Desktop'

    >>> help(os.getcwd)

    Help on built-in function getcwd in module nt:

    getcwd(...)

        getcwd() -> path

       

        Return a string representing the current working directory.

    列出当前目录下所有的文件

    >>>>>> import os

    >>> print os.getcwd()

    C:UsersAdministratorDesktop

    >>> cwd=os.getcwd()

    >>> print cwd

    C:UsersAdministratorDesktop

    >>> ldirs=os.listdir(cwd)

    >>> print ldirs

    ['001.png', '002.png', '1000', '11.txt', '111', '151207-xd4xc6xc9xccxc6xbd。。。。

    网络编程库

    >>> import urllib

    >>> import webbrowser  as web  #as 是给函数库起一个假名

    ………

    >>> open('163.com.html','w').write(content)

    >>> webbrowser.open_new_tab('163.com.html')   #打开一个本地网页

    >>> webbrowser.open_new_tab('http://baidu.com') #打开一个新的网页

    >>> import socket

    >>> baiduip=socket.gethostbyname('www.baidu.com')   #获取域名对应的ip地址

    >>> print baiduip

    180.97.33.107

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

    Help(list)可以查到所有的list的函数,同理其他数据类型  help(str) help(file) help(tuple) help(dict)

     import socket

    help(socket)

    查找更细的函数使用则用这种类似规则>>> help(str.count)

    ============第三方提供的函数库================

    第三方库的安装

    Linux 下

    下载安装非常简单直接输入以下命令,就自动下载安装了

    easy_install httplib2

    Windows下

    一下载第三方库

    二将第三方库解压到python相关的目录下(好找C:/python27)

    三 检查加上系统环境变量的Path加上;C:python27

    四 在cmd中进入第三方库的解压目录,运行python setup.py install

    ==============自定义函数=============

    语法结构(没有返回值,没有类型)

    def function_name(parameters):

           starement1

           statement2

           statement3

    函数的定义:形参

    定义函数的好处  模块化

    函数的调用

    function_name(parameters)

    #coding uft-8

    def myfunction01(a,b):

        print a

        print b

    myfunction01('amily','hello!')

    myfunction01(12,13)

    如何返回给主调函数返回值 用return

    单值返回

    #coding uft-8

    def myfunction02(a,b):

        return a+b

    n=myfunction02(123,45)

    print n

    多值返回,主调函数接收

    #coding:utf-8

    #a**b表示a的b次方

    def myfunction03(a,b):

        m= a+b

        n=a*b

        p=a-b

        e=a**b    

        return n,m,p,e

    sum1,sum2,sum3,sum4=myfunction03(12,13)

    num=myfunction03(2,3)

    print sum1,sum2,sum3,sum4

    print num

    #num为数组(6, 5, -1, 8)

    预设值参数函数   设定预定值的参数可以不传值

    #coding:utf-8

    #a**b表示a的b次方

    #6、4默认都是第一个参数

    #有预定值的参数写在最右边,如果调用时重新赋值预设值就可以被冲掉了

    def myfunction04(a,b=1,c=2):

        m= a+b

        n=a*b

        p=a-b

        e=a**b

        d=a+c

        return m,n,p,e,d

    sum1,sum2,sum3,sum4,sum5=myfunction04(3)

    num0=myfunction04(a=3)

    num1=myfunction04(3)

    num2=myfunction04(1)

    num3=myfunction04(3,b=3,c=1)

    print sum1,sum2,sum3,sum4,sum5

    print num1

    print num2

    #num为数组(6, 5, -1, 8)

    自定义传入形参  a   使用时a=2, 实参就是2

  • 相关阅读:
    LeetCode Best Time to Buy and Sell Stock
    LeetCode Scramble String
    LeetCode Search in Rotated Sorted Array II
    LeetCode Gas Station
    LeetCode Insertion Sort List
    LeetCode Maximal Rectangle
    Oracle procedure
    浏览器下载代码
    Shell check IP
    KVM- 存储池配置
  • 原文地址:https://www.cnblogs.com/AmilyWilly/p/5103485.html
Copyright © 2011-2022 走看看