Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。自从20世纪90年代初Python语言诞生至今,它逐渐被广泛应用于处理系统管理任务和Web编程。Python是完全面向对象的语言。函数、模块、数字、字符串都是对象。并且完全支持继承、重载、派生、多继承,有益于增强源代码的复用性。Python支持重载运算符和动态类型。
(1)文本处理
包含文本格式化、正则表达式匹配、文本差异计算与合并、Unicode支持,二进制数据处理等功能
(2)文件处理
包含文件操作、创建临时文件、文件压缩与归档、操作配置文件等功能
(3)操作系统功能
包含线程与进程支持、IO复用、日期与时间处理、调用系统函数、写日记(logging)等功能
(4)网络通信
包含网络套接字,SSL加密通信、异步网络通信等功能
(5)网络协议
支持HTTP,FTP,SMTP,POP,IMAP,NNTP,XMLRPC等多种网络协议,并提供了编写网络服务器的框架
(6)W3C格式支持
包含HTML,SGML,XML的处理。
(7)其它功能
包括国际化支持、数学运算、HASH、Tkinter等
(1)Python
是一门跨平台的脚本语言,Python规定了一个Python语法规则,实现了Python语法的解释程序就成为了Python的解释器。
(2)CPython
ClassicPython,也就是原始的Python实现,需要区别于其他实现的时候才以CPython称呼;或解作C语言实现的Python。这是最常用的Python版本。
(3)Jython
原名JPython;Java语言实现的Python,现已正式发布。Jython可以直接调用Java的各种函数库。
(4)PyPy
使用Python语言写的Python。
Python跟其他的编程语言一样,基本架构都如下图所示:
说明:
1、输入方式有变量赋值和输入语句两种(当然不局限这两种);
2、处理方式有算术运算、逻辑运算和算法处理;
3、输出方式有打印输出,写入文件和写入数据库(后面两种输出方式后面再介绍)。
为了更好说明Python程序的基本架构,我们通过几个例子进行说明:
例子一:
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> x = 12 >>> y = 13 >>> z = x + y >>> print (z) 25上述例子的执行过程是:
输入(变量赋值)->处理(算术运算)->输出(打印输出)
例子二:
>>> str1=raw_input("Please input your name: ") Please input your name: lee >>> print (str1) lee
上述例子的执行过程是:
输入(输入语句)->处理(逻辑运算)->输出(打印输出)
例子三:继续例子二
>>> str1=raw_input("Please input your name: ") Please input your name: lee >>> print (str1) lee >>> n = len (str1) >>> print (n) 3
上述例子的执行过程是:
输入(输入语句)->处理(逻辑处理)->输出(打印输出)->处理(逻辑处理)->输出(打印输出)
前面我们已经初步使用了print函数进行打印输出,那么print函数的使用时怎么样的呢?以例子进行说明:
(1)print()打印整型数据
>>> print (12) 12
或者
>>> x = 12 >>> print (x) 12(2) print()打印浮点型数据
>>> print (12.4) 12.4
或者
>>> y = 12.4 >>> print (y) 12.4
(3)print()打印字符型数据
>>> print ('lee') lee
或者
>>> z = 'lee' >>> print (z) lee
(4)print()打印字符串
>>> print ('www.baidu.com') www.baidu.com
或者
>>> str1 = 'www.baidu.com' >>> print (str1) www.baidu.com注意:比较以下的输出方式:
>>> x = 1 >>> y = 2 >>> z = 3 >>> print x,y,z 1 2 3 >>> print (x,y,z) (1, 2, 3)
(1)格式
print(format(value,format_modifier))
即 print(format(value,'m,nf'))
(2)说明:
value:数值
format_modifier:格式字
m:输出占位符
n:精度
(3)例子:
>>> print (format(12.3456,'6.2f')) 12.35 >>> print (format(12.3456,'6.9f')) 12.345600000 >>> print (format(12.3456,'9.2f')) 12.35 >>> print (format(12.3456,'3.2f')) 12.35 >>> print (format(0.3456,'.2%')) 34.56% >>> print (format(0.3456,'3.2%')) 34.56% >>> print (format(0.3456,'1.3%')) 34.560%
Python输入语句主要采用raw_input函数,我们先查看一下raw_input函数的帮助信息
>>> help (raw_input)
Help on built-in function raw_input in module __builtin__:
raw_input(...)
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled. The prompt string, if given,
is printed without a trailing newline before reading.
1、raw_input函数的格式如下所示:
re = raw_input([prompt])--说明:[]符号表示可用可不用
说明:
prompt:指提示字符
re:返回值
2、例子:
>>> str1 = raw_input() www.baidu.com >>> print (str1) www.baidu.com >>> type (str1) <type 'str'>比较:
>>> str2 = raw_input('Please input your name: ') Please input your name: lee >>> print (str2) lee >>> type (str2) <type 'str'>由于raw_input函数处理的是str型,那么有没有办法处理整型和字符型呢?答案是肯定的,请看下面的例子:
>>> age = raw_input('Please input your age: ') Please input your age: 22 >>> type (age) <type 'str'> >>> age = age + 1 Traceback (most recent call last): File "<pyshell#66>", line 1, in <module> age = age + 1 TypeError: cannot concatenate 'str' and 'int' objects >>> age = int (age) >>> age = age + 1 >>> print (age) 23可以简化为一下的语句:
>>> age = int (raw_input('Please input your age: ')) Please input your age: 22 >>> print (age) 22 >>> type (age) <type 'int'>继续实验浮点型的例子:
>>> weight = float(raw_input('Please input your weight: ')) Please input your weight: 67.23 >>> print (weight) 67.23 >>> type (weight) <type 'float'>