【Python3.x和Python2.x的区别】教程文章相关的互联网学习教程文章

python 图片爬虫【代码】

#!/usr/bin/env python #coding:utf-8import urllib import redef GetHtml(url):"""获取HTML页面所有元素."""page = urllib.urlopen(url)html = page.read()return htmldef GetImg(html):""" 获取HTML页面所有.jpg图片."""reg = r‘src="(.+?\.jpg)"‘ imgre = re.compile(reg)imglist = re.findall(imgre, html)x = 0for imgurl in imglist:urllib.urlretrieve(imgurl, ‘%s.jpg‘ %x, cbx)x += 1print"img: %s is done!" %xdef...

Python实现合并排序MergeSort

def merge(sort_list, start, mid, end): left_list = sort_list[start:mid] right_list = sort_list[mid:end] left_list.append(float("inf")) right_list.append(float("inf")) left_index = right_index = 0 i = start while i < end: if left_list[left_index] < right_list[right_index]: sort_list[i] = left_list[left_index] left_index += 1 else: ...

python 字符串操作【代码】

特性:不可修改 name.capitalize() 首字母大写 name.casefold() 大写全部变小写 name.center(50,"-") 输出 ‘---------------------Alex Li----------------------‘ name.count(‘lex‘) 统计 lex出现次数 name.encode() 将字符串编码成bytes格式 name.endswith("Li") 判断字符串是否以 Li结尾 "Alex\tLi".expandtabs(10) 输出‘Alex Li‘, 将\t转换成多长的空格 name.find(‘A‘) 查找A,找到返回其索引, 找不到...

wxpython仿写记事本【代码】【图】

闲来无事,偶尔看到wxpython桌面开发,这是之前开发的..从别的博客中搬过来..以后大家开发桌面应用也可多了一种选择..# -*- coding: utf-8 -*- import hashlib import os import wx #编辑器主类 """ 记录:menu:Menu Dialog: MessageDialog,MultiChoiceDialog,NumberEntryDialog,PasswordEntryDialog,PrintDialog,ProgressDialog,SingleChoiceDialog,TextEntryDialog, FileDialog,FindReplaceDialog,FontDialog Stati...

python核心-类-1【代码】

#!/usr/bin/env python # -*- coding=utf-8 -*-class ClassT1:# 定义类变量__classVar = 0# 定义构造函数def __init__(self):ClassT1.__classVar += 1# 定义set方法def setName(self, _name):self.__name = _name# 定义get方法def getName(self):return self.__name# 定义类方法@staticmethoddef getClassVar():return ClassT1.__classVardef puts(self):print ClassT1.__classVarfoo = ClassT1() bar = ClassT1() foo.puts() prin...

Python学习之模块【代码】【图】

模块与包模块的概念#在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护。 #为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很多编程语言都采用这种组织代码的方式。在Python中,一个.py文件就称之为一个模块(Module)。模块的好处#最大的好处是大大提高了代码的可维护性。其次,编写代码不必从零开始。当一个模块编写完毕...

python进入adb shell交互模式【代码】

import subprocess#方法一:进入某个环境执行语句(adb shell),注意shell内部命令需要带\n,执行完后一定记得执行exit命令退出,否则会阻塞 obj = subprocess.Popen([‘adb‘, ‘shell‘], shell = True, stdin=subprocess.PIPE, stdout=subprocess.PIPE ,stderr=subprocess.PIPE) obj.stdin.write(‘ls\n‘.encode(‘utf-8‘)) obj.stdin.write(‘exit\n‘.encode(‘utf-8‘)) #重点,一定要执行exit info,err = obj.communica...

49 Python - 装饰器 函数定义装饰器【图】

03 函数定义装饰器 前面讲解了装饰器的一些概念和装饰器会用到的函数的知识,接下来讲解装饰器的定义,有两种方式可以定义装饰器一种是通过函数定义,一种是通过类定义。 001函数定义装饰器(1)装饰器定义如何定义装饰器名称本身是一个函数,定义时候类似函数一样定义装饰器需要修饰另外一个函数,所以装饰器需要一个传入一个函数作为参数装饰器内部再定义一个函数用于对传递进来的函数进行装饰的,定义一个函数wrapper(*arg...

Python中的魔术方法【代码】

魔术方法就是一个类/对象中的方法,和普通方法唯一的不同时,普通方法需要调用!而魔术方法是在特定时刻自动触发。这里列举出几个常用的魔术方法:1.__init__初始化魔术方法 触发时机:初始化对象时触发(不是实例化触发,但是和实例化在一个操作中) 参数:至少有一个self,接收对象 返回值:无 作用:初始化对象的成员 注意:使用该方式初始化的成员都是直接写入对象当中,类中无法具有2.__new__实例化魔术方法 触发时机: 在实例...

python装饰器作业【代码】

# user_dic={‘xiaohong‘:‘123‘,‘xiaobai‘:‘123‘,‘xiaohei‘:‘123‘} # with open(‘idpd.txt‘,‘w‘,encoding=‘utf-8‘) as f: # f.write(str(user_dic)) # with open(‘idpd.txt‘,‘r‘,encoding=‘utf-8‘) as f2: # read=f2.read() # print(read) # user_dic=eval(read) #将user_dic字典写入文件,以及将文件字典导入# idpd_path=r‘F:\Pycharmprojects\0615\idpd.txt‘ # idpd_dic={‘id‘:False...

Python基础—16-网络编程

网络编程相关概念OSI七层模型:开放系统互连参考模型。它从低到高分别是:物理层、数据链路层、网络层、传输层、会话层、表示层和应用层。TCP/IP:在OSI七层模型的基础上简化抽象出来的一套网络协议簇,现在得到了广泛使用。TCP:传输控制协议 有连接的,数据是安全有保障的传输的速度稍慢,三次握手、四次挥手、数据检查UDP:用户数据报协议 无连接的,数据是不可靠的传输的速度比较快IP地址:唯一的一台计算标识 查看:ipconfigp...

Python操作Excel【代码】

安装模块Python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库。pip2 install xlrdpip2 install xlwtxlrd是读excel xlwt是写excel# ecoding:utf-8import os import xlwtdef set_style(name, height, bold = False):style = xlwt.XFStyle() #初始化样式font = xlwt.Font() #为样式创建字体font.name = namefont.bold = boldfont.color_index = 4font.height = heightstyle.font = fontreturn s...

Ubuntu16.04安装python3.6.5详细步骤

下载python3.6.5安装包1. 上传安装包。打开终端,利用命令cd 进入文件所在文件夹里python@ubuntu:~/workspace$pwd/home/python/workspace2. 解压文件 tar xfzPython-3.6.5.tgz 注意:这里使用xfz命令,而不建议使用-xvzf命令,因为其释放的文件夹需要root权限才可以更改或者删除。python@ubuntu:~/workspace$tar xfz Python-3.6.5.tgzpython@ubuntu:~/workspace$ll总用量 22472drwxrwxr-x 4 python python 4096 5月 12 1...

那些年被我坑过的Python——道阻且长(第五章实用模块讲解)【代码】【图】

random模块我的随机验证吗程序:   首先保证了字母和数字出现的概率是50% VS 50%,其次是可以订制输出多少位 1def Captcha(size):2 Captcha_list = []3for i in range(size):4 rand_num = random.randint(1, 2)5if rand_num == 1:6 Captcha_list.append(chr(random.randint(65, 90)))7elif rand_num == 2:8 Captcha_list.append(str(random.randint(0, 9)))9else: 10pass11return‘‘.join(...

python_射门小游戏【代码】

1import random2def shoot(fs = 0,i = 0,j = 0):3while i < 5:4print("<<<<<<<<<<<<<<<<<<开始游戏,游戏5局三胜制>>>>>>>>>>>>>>>>>>>>>>")5 门将扑球方向 = random.choice(["左","中","右"])6 玩家选择射门方向 = input("请输入您要射门的方向(左中右): ")7if 玩家选择射门方向 == 门将扑球方向:8 fs = fs - 19 i = i+1 10print("您的球被门将扑出,分数:%d\n您的命中率是:%d中%d请继...