上一页 1 ··· 6 7 8 9 10 11 12 下一页
摘要: 加法:+,与C#中并无区别,并且一样可以作用于字符串。但Python中不支持字符串与数值类型的相加。1 i = 12 s = '1'3 print(s + i)这样是会在运行时报错的,正确写法如下:1 i = 12 s = '1'3 print(s + str(i))Python中不支持自增,即:1... 阅读全文
posted @ 2014-09-29 15:56 h82258652 阅读(861) 评论(0) 推荐(0) 编辑
摘要: 在Python中可以使用isinstance函数来判断某个值或变量是否为某个类型。例子:1 print(isinstance(1,int))2 print(isinstance(1,float))3 print(isinstance('str',str))则将会输出:TrueFalseTrue相当于... 阅读全文
posted @ 2014-09-29 15:34 h82258652 阅读(297) 评论(0) 推荐(0) 编辑
摘要: type函数可以检测任何值或变量的类型。例子: 1 def printType(var): 2 print(type(var)) 3 4 class TestClass: 5 pass 6 7 printType(1) 8 printType(1.5) 9 printType('... 阅读全文
posted @ 2014-09-29 15:29 h82258652 阅读(367) 评论(0) 推荐(0) 编辑
摘要: 在Python中有两种注释,一种是普通注释,另一种是文档注释。普通注释是使用#开头1 print('output something') # here is comment而Python中多行注释也是使用#1 # comment 12 # comment 23 # comment 3而文档注释则是使... 阅读全文
posted @ 2014-09-28 01:42 h82258652 阅读(255) 评论(0) 推荐(0) 编辑
摘要: 除了 Http 模块可以模拟 Http 请求外,使用 Urllib 模块也是可以模拟 Http 请求的,只不过功能相对弱一点。1 import urllib.request2 3 opener = urllib.request.urlopen("http://www.baidu.com")4 byt... 阅读全文
posted @ 2014-09-28 00:06 h82258652 阅读(188) 评论(0) 推荐(0) 编辑
摘要: 模拟 http 请求是比较常见的一种需求,在 Python 中,使用 http 模块操作。 1 import http.client 2 3 # 创建 Http 连接。 4 http = http.client.HTTPConnection('www.baidu.com')... 阅读全文
posted @ 2014-09-27 23:45 h82258652 阅读(1087) 评论(0) 推荐(0) 编辑
摘要: 在某些情况下,我们需要定义自己的异常并且抛出先定义一个错误:1 class MyError(BaseException):2 def __init__(self):3 pass上面定义了一个叫MyError的类,继承自BaseException。在Python中,所有的错误都... 阅读全文
posted @ 2014-09-27 23:06 h82258652 阅读(175) 评论(0) 推荐(0) 编辑
摘要: 错误是多种多样的,在 except 语句中,可以捕获指定的异常修改代码如下: 1 import io 2 3 path = r'' 4 mode = 'w' 5 6 try: 7 file = open(path,mode) 8 str = file.read() 9 p... 阅读全文
posted @ 2014-09-27 22:59 h82258652 阅读(183) 评论(0) 推荐(0) 编辑
摘要: 虽然叫错误,但跟 C# 中的异常是一回事。只不过 Python 中叫错误(Error)而 C# 中叫异常(Exception)。先手工产生一个异常:1 file = open('','r')上面一句由于路径是空路径,因此文件肯定是不存在的,执行这一句会引发 FileNotFoundError 这个错... 阅读全文
posted @ 2014-09-27 22:42 h82258652 阅读(217) 评论(0) 推荐(0) 编辑
摘要: Python应用最广泛的要数web方面了。因此,socket显得十分重要。要使用socket,必须引入socket模块,因此在Python脚本开头先写入1 import socket学过socket的都知道,就是客户端和服务端的通信。因此新建client.py和server.py文件。先编写serv... 阅读全文
posted @ 2014-09-27 22:26 h82258652 阅读(451) 评论(0) 推荐(0) 编辑
上一页 1 ··· 6 7 8 9 10 11 12 下一页