Home > Backend Development > Python Tutorial > Basic knowledge of Python functions

Basic knowledge of Python functions

高洛峰
Release: 2017-03-17 09:14:09
Original
1746 people have browsed it

This article describes the internal functions provided by the system, the function library provided by the third party + simple crawling code, the process of installing the httplib2 module and the user-defined functions. Has very good reference value. Let’s take a look at it with the editor

I have only recently started to learn the Python language, but I have discovered many of its advantages (such as the simplicity of the language and deep experience in web crawlers). I mainly learned it through the "Python Basic Tutorial" "Study with "51CTO College Zhipu Education's python video". When I watched the teacher talking about function knowledge in the video, I felt it was very good, so I wrote my first Python learning article to share with everyone. Main content:

1.Python installation and basic input and output, simple usage of print() function and raw_input() function.

2.I Based on the knowledge learned in the video, explain the basic knowledge of functions:

(1). The system provides internal functions: string function library, mathematical function library, network programming function library, OS function Library

(2). Third-party provided function library: Explain how to install the httplib2 third-party function library, and then make a simple web crawler example

(3). User-defined function: Explain custom functions such as non-return types, formal parameters, and preset value parameters

#3. At the same time, I briefly compared network programming with what I had learned in C# before, and found that python does have a lot Advantages, and very convenient and powerful.

PS: The article quoted a lot of knowledge from videos, books and my own knowledge. I would like to thank the authors and teachers. I hope the article will be useful to everyone. I just started to learn python knowledge with help. If there are any errors or deficiencies in the article, please forgive me. I also hope that everyone can provide comments and encourage you. Don’t criticize~

1. Python installation and input and output Function

Python interpreter can be installed and used built-in in Linux. In Windows, you need to download it from the downloads page of www.python.org official website (such as python-2.7.8.amd64.msi) and install Python integration. Development environment (Python Integrated Development Environment, IDLE) is enough. Run the program and enter ">>>print 'hello world'", and the python interpreter will print out the "hello world" string. As follows:

Basic knowledge of Python functions

Basic knowledge of Python functions

Then the basic framework of the Python program is "input-processing-output", and the input and output functions are as follows:

1.print() function

The function is used to output or print integer, floating point, and string data to the screen, such as print(3), print(12.5) , print('H'). It outputs the variable format "print(x) or print x", and can output multiple variables "print x, y, z". It also supports formatted output data and calls the format() function, The format is:

print(format(val,format_modifier)) where val represents the value and format_modifier represents the format word.


#简单输出 
>>> 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%
Copy after login

2.raw_input() function

The built-in function accepts input from sys.stdin, reads the input statement and returns the string string .The input ends with a newline character. You can find help through help(raw_input). The common format is:

s = raw_input([prompt]) The parameter [prompt] is optional and is used for prompts. User input.


#输入函数 
>>> str1 = raw_input("please input a string:") 
please input a string:I love you 
>>> print(str1) 
I love you 
#查看str1数据类型 
>>> type(str1) 
<type &#39;str&#39;>
Copy after login

Note the difference between raw_input() and input(): (1 ).Input supports the legal Python table format "abc". The string must be enclosed in quotation marks, otherwise the error "NameError: name 'abc' is not defined" will be reported, and raw_input() can accept any type of input; (2). raw_input() treats all inputs as strings and returns strings, while input() has its own characteristics when inputting pure numbers and returns the input number type int or float. An example is explained as follows:


#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 &#39;abc&#39; 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 &#39;float&#39;> 
#raw_input 数据类型均为str 
>>> age = raw_input("age:") 
age:20 
>>> type(age) 
<type &#39;str&#39;>
Copy after login

2. Function system provides internal functions

The system internal library functions provided by python mainly describe four types: (Quoted from the video, only briefly introduced)

1. String function library

You can query the string function library through help(str), in which "-More-" is entered during the query process Enter to realize scrolling information, output "q" to exit help (Quit). Everyone is familiar with string functions. They have learned a lot in C\C++\C#\Java, and they are basically the same. For example:

islower The () function determines whether the string is uppercase or lowercase, and returns False if it is uppercase. The format() function and the string length len() function used previously belong to the string function library. Help(str.replace) can query the specific function usage. This function is used to replace the old string with the new string.


#判断字符串是否小写 
>>> str1 = "Eastmount" 
>>> str1.islower() 
False 
#字符串替换 
>>> str2 = &#39;adfababdfab&#39; 
>>> str2.replace(&#39;ab&#39;,&#39;AB&#39;) 
&#39;adfABABdfAB&#39; 
#字符串求长度 
>>> print(len(str2)) 
11 
>>>
Copy after login

2. Math function library

使用数学函数库时需要注意的是导入库"import math",该库中函数我们也非常熟悉,如sin()求正弦,cos()求余弦,pow(x,y)计算x的y次幂,如pow(3,4)=3*3*3*3,python中也可以使用3**4表示.help(math)中可以查看详细信息,而且库中定义了两个常数DATA:

e = 2.718281... pi = 3.14159265...


#导入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). 
>>>
Copy after login

3.网络编程库

系统提供内部库函数中网络编程库,我此处仅仅举个简单例子,socket(套接字网络编程)获取主机ip地址这是非常常见的运用,我将与C#网络编程进行简单对比.后面的博文中将详细讲述python网络编程.


>>> import socket 
>>> baiduip = socket.gethostbyname(&#39;www.baidu.com&#39;) 
>>> print baiduip 
61.135.169.125
Copy after login

其中socket编程很常用,gethostbyname()返回指定主机ip,而C#中获取百度网址的ip地址代码如下所示.代码中可能会出现"警告:Dns.GetHostByName()函数已过时",可替换为IPHostEntry myHost = Dns.GetHostEntry(www.baidu.com).输出:

61.135.169.121

61.134.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(); 
 } 
 } 
}
Copy after login

4.操作系统(OS)函数库

操作系统库函数引用"import os",举例获取当前工作路径和先死当前路径下的文件和目录.使用os.system("cls")可以实现清屏功能.安装python目录Lib文件夹下含有很多py库文件供使用.


>>> import os 
#获取当前工作路径 
>>> current = os.getcwd() 
>>> print current 
G:\software\Program software\Python\python insert 
#获取当前路径下的文件和目录 
>>> dir = os.listdir(current) 
>>> print dir 
[&#39;DLLs&#39;, &#39;Doc&#39;, &#39;h2.txt&#39;, &#39;include&#39;, &#39;Lib&#39;, &#39;libs&#39;, &#39;LICENSE.txt 
&#39;, &#39;NEWS.txt&#39;, &#39;python.exe&#39;, &#39;pythonw.exe&#39;, &#39;README.txt&#39;, &#39;tcl&#39;, &#39;Tools&#39;] 
>>>
Copy after login

三. 函数之第三方提供函数库及安装httplib2模块过程

(一).安装第三方函数库httplib2过程

Python中第三方开源项目中提供函数库供我们使用,比如使用httplib2库函数.在Linux中直接使用"easy_install httplib2"搜索自动安装,Windows下python开发工具IDLE里安装httplib2模块的过程如下(其他模块类似).

1.下载httplib2模块"https://code.google.com/p/httplib2/"到指定目录,解压压缩包"httplib2_0.8.zip"到某目录下,如果该网址google访问失败,可以到此处下载:

2.配置python运行环境

右键"计算机"->"属性"->在"系统"中选择"高级系统设置"->在"系统属性"中"高级"选择"环境变量"

Basic knowledge of Python functions

在系统环境变量Path后添加python安装目录"G:\software\Program software\Python\python insert"

Basic knowledge of Python functions

3.在dos下安装httpLib2

管理员模式运行cmd,利用cd命令进入httplib2_0.8.zip解压目录,输入"python settup.py install",如下图所示安装成功.

Basic knowledge of Python functions

4.使用httplib2

如果httplib2库函数没有安装成功,"import httplib2"会提示错误"ImportError: No module named httplib2".否则安装成功,举例如下.


import httplib2 
#获取HTTP对象 
h = httplib2.Http() 
#发出同步请求并获取内容 
resp, content = h.request("http://www.csdn.net/") 
#显示返回头信息 
print resp 
#显示网页相关内容 
print content
Copy after login

输出头信息:


{&#39;status&#39;: &#39;200&#39;, &#39;content-length&#39;: &#39;108963&#39;, &#39;content-location&#39;: &#39;http://www.csdn.net/&#39;, .... &#39;Fri, 05 Sep 2014 20:07:24 GMT&#39;, &#39;content-type&#39;: &#39;text/html; charset=utf-8&#39;}
Copy after login

(二).简单网页爬虫示例

使用第三方函数库时的具体格式为module_name.method(parametes) 第三方函数名.方法(参数).

讲述一个引用web库,urllib库访问公网上网页,webbrowser库调用浏览器操作,下载csdn官网内容并通过浏览器显示的实例.


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")
Copy after login

它会输出True并在浏览器中打开下载的静态网页.引用import webbrowser as web使用web,也可以直接引用,使用时"module_name.method"即可.

content = urllib.urlopen(url).read()表示打开url并读取赋值

open("soso.html","w").write(content)表示在python安装目录写静态soso.html文件

web.open_new_tab("soso.html")表示打开该静态文件新标签.

同样可以使用web.open_new_tab('http://www.soso.com')直接在浏览器打开动态网页.效果如下图所示:

Basic knowledge of Python functions

Basic knowledge of Python functions

四. 函数之自定义函数

1.无返回值自定义函数

其基本语法格式如下:


  def function_name([parameters]):  函数名([参数]),其中参数可有可无  (TAB) statement1
  (TAB) statement2   ...
Copy after login

注意:

(1).自定义函数名后面的冒号(:)不能省略,否则报错"invalid syntax",而且无需像C语言一样加{};

(2).函数里每条语句前都有缩进TAB,否则会报错"There's an error in your programs:expected an indented block",它的功能相当于区分函数里的语句与函数外的语句.

举例:打开IDLE工具->点击栏"File"->New File新建文件->命名为test.py文件,在test文件里添加代码如下.


def fun1(): 
 print &#39;Hello world&#39; 
 print &#39;by eastmount csdn&#39; 
print &#39;output&#39; 
fun1() 
def fun2(val1,val2): 
 print val1 
 print val2 
fun2(8,15)
Copy after login

保存,在test.py文件里点击Run->Run Module.输出结果如下图所示,其中fun1()函数无形参自定义函数,fun2(val1,val2)是有形参自定义函数,fun2(8,15)为函数的调用,实参8和15.

Basic knowledge of Python functions

2.有返回值自定义函数

其基本语法格式如下:


   def funtion_name([para1,para2...paraN])
  statement1
  statement2
  ....
  return value1,value2...valueN
Copy after login

返回值支持一个或多个返回,需要注意的是自定义函数有返回值,主调函数就需要接受值(接受返回的结果).同时在定义变量时可能sum这些可能是关键字(注意颜色),最好使用不是关键字的变量.举例:


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 &#39;the result are &#39;,a,b,c,d,e 
re = fun3(2,10) 
print re
Copy after login

输出结果如下,其中需要注意的是参数一一对应,在除法中2/10=0,**幂运算2的10次方=1024.而使用re=fun3(2,10)直接输出的结果成为元组,后面会详细讲述,(12,-8,20,0,1024)元组中re[0]存储12,re[1]存储-8,依次~


2 10 
the result are 12 -8 20 0 1024 
2 10 
(12, -8, 20, 0, 1024)
Copy after login

3.自定义函数参数含预定义

预设值的基本格式如下:


  def function_name(para1,para2...parai=default1...paran=defaultn)
  statement1
  statement2
  ...
  return value1,value2...valuen
Copy after login

其中需要注意的是预定义值得参数不能先于无预定义值参数,举个例子讲解.


def fun4(n1,n2,n3=10): 
 print n1,n2,n3 
 n = n1 + n2 + n3 
 return n 
re1 = fun4(2,3) 
print &#39;result1 = &#39;,re1 
re2 = fun4(2,3,5) 
print &#39;result2 = &#39;,re2
Copy after login

输出结果如下图所示,预定义的参数在调用时,实参可以省略,也可以替换默认定义的预定义值.


2 3 10 
result1 = 15 
2 3 5 
result2 = 10
Copy after login

其中如果函数定义为def fun4(n3=10,n2,n1)就会报错"non-default argument follows default argument"(没预定义的参数在预定义参数的后面),所以定义函数时需要注意该点.

同时需要注意函数调用时的赋值顺序问题,最好采用一对一复制,也可以函数调用中给出具体形参进行赋值,但需要注意的是在函数调用过程中(使用函数时),有预定义值的参数不能先于无预定义值参数被赋值.

如例子中自定义函数def fun4(n1,n2,n3=10),在调用时可以:

(1).s=fun4(2,3)省略预定义参数,它是一对一赋值,其中n1赋值2、n2赋值3、n3赋值10(预定义)

(2).s=fun4(4,n2=1,n3=12)它也是一对一赋值,而且预定义值n3替换为12

(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会赋值多个.

So, it’s best to call functions one by one. You don’t usually make things difficult for yourself when writing programs. Just correspond.

Summary: The article starts from the internal functions provided by the system and the function library provided by the third party + simple The process of climbing out of the code and installing the httplib2 module and the user-defined functions will be described in three aspects. If there are any errors or deficiencies in the article, Haihan ~ Finally, thank you to the video teacher, the above-mentioned bloggers, book teachers and me.

The above is the detailed content of Basic knowledge of Python functions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template