#简单输出 >>> print(12.5) 12.5 >>> print("eastmount") eastmount # 输出"123.46",6.2表示 输出6位,小数点后精度2位, 输出最后一位6是四舍五入的结果 >>> print(format(123.45678,'6.2f')) 123.46 # 输出"口口口123",采用右对齐补空格方式 输出总共6位 >>> print(format(123.45678,'6.0f')) 123 # 输出"123.456780000"小数点后面9位,超出范围会自动补0 >>> print(format(123.45678,'6.9f')) 123.456780000 # 输出"34.56%"表示打印百分率 >>> print(format(0.3456,'.2%')) 34.56%
#输入函数 >>> str1 = raw_input("please input a string:") please input a string:I love you >>> print(str1) I love you #查看str1数据类型 >>> type(str1) <type 'str'>
#SyntaxError语法错误 >>> str1 = input("input:") input:abc Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'abc' is not defined #正确输入输出 >>> str1 = input("input:") input:"I love you" >>> print str1 I love you #input纯数字 数据类型 >>> weight = input("weight:") weight:12.5 >>> type(weight) <type 'float'> #raw_input 数据类型均为str >>> age = raw_input("age:") age:20 >>> type(age) <type 'str'>
#判断字符串是否小写 >>> str1 = "Eastmount" >>> str1.islower() False #字符串替换 >>> str2 = 'adfababdfab' >>> str2.replace('ab','AB') 'adfABABdfAB' #字符串求长度 >>> print(len(str2)) 11 >>>
#导入math库 >>> import math >>> print math.pi 3.14159265359 #计算sin30度 >>> val = math.sin(math.pi/6) >>> print val 0.5 #pow函数 >>> math.pow(3,4) 81.0 >>> 3**4 81 >>> help(math.pow) Help on built-in function pow in module math: pow(...) pow(x, y) Return x**y (x to the power of y). >>>
>>> import socket >>> baiduip = socket.gethostbyname('www.baidu.com') >>> print baiduip 61.135.169.125
//引用新命名空间 using System.Net; namespace GetIp { class Program { static void Main(string[] args) { //获取DNS主机名的DNS信息 IPHostEntry myHost = Dns.GetHostByName("www.baidu.com"); IPAddress[] address = myHost.AddressList; for (int index = 0; index < address.Length; index++) { Console.WriteLine(address[index]); } Console.ReadKey(); } } }
>>> import os #获取当前工作路径 >>> current = os.getcwd() >>> print current G:\software\Program software\Python\python insert #获取当前路径下的文件和目录 >>> dir = os.listdir(current) >>> print dir ['DLLs', 'Doc', 'h2.txt', 'include', 'Lib', 'libs', 'LICENSE.txt ', 'NEWS.txt', 'python.exe', 'pythonw.exe', 'README.txt', 'tcl', 'Tools'] >>>
import httplib2 #获取HTTP对象 h = httplib2.Http() #发出同步请求并获取内容 resp, content = h.request("http://www.csdn.net/") #显示返回头信息 print resp #显示网页相关内容 print content
import urllib import webbrowser as web url = "http://www.soso.com" content = urllib.urlopen(url).read() open("soso.html","w").write(content) web.open_new_tab("soso.html")
def function_name([parameters]): 函数名([参数]),其中参数可有可无 (TAB) statement1 (TAB) statement2 ...
def fun1(): print 'Hello world' print 'by eastmount csdn' print 'output' fun1() def fun2(val1,val2): print val1 print val2 fun2(8,15)
def funtion_name([para1,para2...paraN]) statement1 statement2 .... return value1,value2...valueN
def fun3(n1,n2): print n1,n2 n = n1 + n2 m = n1 - n2 p = n1 * n2 q = n1 / n2 e = n1 ** n2 return n,m,p,q,e a,b,c,d,e = fun3(2,10) print 'the result are ',a,b,c,d,e re = fun3(2,10) print re
2 10 the result are 12 -8 20 0 1024 2 10 (12, -8, 20, 0, 1024)
def function_name(para1,para2...parai=default1...paran=defaultn) statement1 statement2 ... return value1,value2...valuen
def fun4(n1,n2,n3=10): print n1,n2,n3 n = n1 + n2 + n3 return n re1 = fun4(2,3) print 'result1 = ',re1 re2 = fun4(2,3,5) print 'result2 = ',re2
2 3 10 result1 = 15 2 3 5 result2 = 10
(3).s=fun4(n2=1,n1=4,n3=15)它的顺序与定义函数没有对应,但是调用时给出具体对应也行
下面的情况就会出现所述的"有预定义值的参数先于无预定义值参数被赋值"错误:
(1).s=fun4(n3=12,n2=1,4)此时会报错"non-keyword arg after keyword arg",它不能指定n1=4,就是没有指定预定值n1=4在有预定值n3=12,n2=1之后,如果改成s=fun4(4,n2=1,n3=12)或s=fun4(4,n3=12,n2=1)即可.
(2).s=fun4(4,n1=2)此时也会报错"TypeError: fun4() got multiple values for keyword argument 'n1'",它不能指定n1=2&n2=4,而是n1会赋值多个.
所以,最好还是一一对应的调用函数,平时写程序没有这样去刁难自己,对应即可
总结:文章从系统提供的内部函数、第三方提供函数库+简单爬出代码及安装httplib2模块过程和用户自定函数三个方面进行讲述.文章中如果有错误或不足之处,海涵~最后感谢那个视频老师和上述博主、书籍老师及me.
以上就是Python函数的基础知识的详细内容,更多请关注Gxl网其它相关文章!
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did82612