
python
文章平均质量分 57
妙舞汉宫人
这个作者很懒,什么都没留下…
展开
-
Python 用文件保存游戏(1)
from random import randint#读取文件中的成绩数据f=open('game.txt')score=f.read().split()#分别存入变量中game_times=int(score[0])min_times=int(score[1])total_times=int(score[2])#计算游戏的平均轮数,注意浮点数和避免除零错误if game_转载 2016-07-22 15:28:08 · 647 阅读 · 0 评论 -
Python 正则表达式(5)电话号匹配
#写一个正则表达式,能匹配出多种格式的电话号码,包括#(021)88776543 010-55667890 02584453362 0571 66345673#\(?0\d{2,3}[) -]?\d{7,8}import retext="(021)88776543 010-55667890 02584533622 057184720483 837922740"m=re.findal转载 2016-08-08 08:50:16 · 10829 阅读 · 4 评论 -
Python 随机数
import randoma=random.randint(1,100)print ('a='+str(a))b=random.randint(3, 3)print ('b='+str(b))c=random.random() #生成一个0到1之间的随机浮点数,包括0但不包括1,也就是[0.0, 1.0)print ('c='+str(c))d=random.uniform(2转载 2016-08-08 09:46:53 · 570 阅读 · 0 评论 -
Python 2-3的坑
我们在课程最开始的时候就讲过 print,在版本2的使用方法是:print 'this is version 2'也可以是print('this is version 2')但到了3,就只能加上括号,像一个函数一样来使用 print:print('this is version 3')假如你看了基于2的教程(比如我写的),然后又装了python 3,可能就会转载 2016-08-08 10:27:58 · 384 阅读 · 0 评论 -
Python pickle
存储的过程import pickletest_data = ['Save me!', 123.456, True]f = file('test.data', 'w')pickle.dump(test_data, f)f.close()取存储的过程:f = file('test.data')test_data = pickle.load(f)f.close()print tes转载 2016-08-08 12:26:20 · 850 阅读 · 0 评论 -
Python 列表解析
list_1 = [1, 2, 3, 5, 8, 13, 22]list_2 = []for i in list_1: if i % 2 == 0: list_2.append(i)print list_2 #[2, 8, 22]print '-----'list_3 =[1,2,4,6,7,8]list_4 = [i /转载 2016-08-08 12:34:11 · 234 阅读 · 0 评论 -
Python 列表解析课后
# 用一行 Python 代码实现:把1到100的整数里,能被2、3、5整除的数取出,以分号(;)分隔的形式输出。print ';'.join([str(i) for i in range(1,101) if i%2==0 and i%3==0 and i%5==0]) #同时除尽 30;60;90print '-----'print ';'.join([str(i) for i in r转载 2016-08-08 12:41:02 · 571 阅读 · 0 评论 -
Python 函数的参数传递(2)
def calcSum(*args): sum = 0 for i in args: sum += i print sumcalcSum(1,2,3) #6calcSum(123,456) #579calcSum() #0def printAll(*args): for转载 2016-08-10 16:38:53 · 1132 阅读 · 0 评论 -
Python函数的参数传递(3)
def printAll(**kargs): for k in kargs: print k, ':', kargs[k]printAll(a=1, b=2, c=3)printAll(x=4, y=5)print '------'def func(x, y=5, *a, **b): print x, y, a, bfunc(1)转载 2016-08-10 16:47:29 · 741 阅读 · 0 评论 -
Python lambda表达式
#常规做法def sum(a, b, c): return a + b + cprint sum(1, 2, 3) #6print sum(4, 5, 6) #15print '-------'#lambda表达式sum = lambda a, b, c: a + b + cprint sum(1, 2, 3) #6print su转载 2016-08-11 10:32:37 · 306 阅读 · 0 评论 -
变量的作用域
def func(x): print 'X in the beginning of func(x): ', x x = 2 #局部变量 print 'X in the end of func(x): ', xx = 50func(x)print 'X after calling func(x): ', x#X in the beginning of func(x): 5转载 2016-08-11 10:55:03 · 240 阅读 · 0 评论 -
Python map
lst_1=[1,2,3,4,5,6]lst_2=[]for item in lst_1: lst_2.append(item*2)print lst_1 print lst_2print '------------'lst_3=[i*2 for i in lst_1]print lst_1print lst_3print '------------'#map转载 2016-08-11 15:16:23 · 314 阅读 · 0 评论 -
Python reduce函数
lst=xrange(1,101)def add(x,y): return x+yprint reduce(add,lst) #5050reduce(function,iterable[,initializer])第一个参数是作用在序列上的方法,第二个参数是被作用的序列,另外=还有一个可选参数,是初始值function需要一个接受2个参数,并有返回值的函数转载 2016-08-11 15:50:27 · 295 阅读 · 0 评论 -
Python 字典方法
1)clear d.clear() 清除字典中所有项,无返回值(返回None)2)copy d.copy() 返回一个具有相同键-值对的新字典 在副本中替换值,原字典不受影响,但如果修改某个值(原地修改,而不是替换),原字典的值也会改变 deepcopy deepcopy(d) (from copy import deepcopy) 值不会改变3)fromkeys d原创 2016-10-15 21:27:32 · 294 阅读 · 0 评论 -
Python 迭代
1.并行迭代names=['anne','beth','george','damon']ages=[12,34,73,18]for i in range (len(names)): print names[i],'is',ages[i],'years old'print '--------------' for name,age in zip(names,ages): pr转载 2016-10-15 21:26:27 · 200 阅读 · 0 评论 -
捕捉异常(1)
try: x=input('Enter the first number:') y=input('Enter the second number:') print x/yexcept ZeroDivisionError: print "The second number can't be zero!"Enter the first number:10Ent转载 2016-11-08 21:51:43 · 219 阅读 · 0 评论 -
捕捉异常(2)
class MuffledCalculator: muffled=False def calc(self,expr): try: return eval(expr) except ZeroDivisionError: if self.muffled: print 'Div转载 2016-11-08 22:01:41 · 259 阅读 · 0 评论 -
捕捉异常(3)
while True: try: x=input('Enter the first number:') y=input('Enter the second number:') value=x/y print 'x/y is ',value except: print "Invalid input.Ple转载 2016-11-08 22:12:00 · 243 阅读 · 0 评论 -
Python 正则表达式(4)
1.我们已经了解了正则表达式中的一些特殊符号,如\b、\d、.、\S等等。这些具有特殊意义的专用字符被称作“元字符”。常用的元字符还有:\w - 匹配字母或数字或下划线或汉字(我试验下了,发现3.x版本可以匹配汉字,但2.x版本不可以)\s - 匹配任意的空白符^ - 匹配字符串的开始$ - 匹配字符串的结束2.\S其实就是\s的反义,任意不是空白符的字符。同理,还有:转载 2016-08-05 15:51:53 · 459 阅读 · 0 评论 -
Python 正则表达式(3)匹配手机号
#匹配手机号import retext="s127 3628391387 17648372936 183930627 1g82732973 28649703767"m=re.findall(r"1\d{10}",text) if m: print melse: print 'not match'结果:['17648372936']匹配手机号,转载 2016-08-05 15:20:54 · 19019 阅读 · 3 评论 -
Python 正则表达式(2)作业
#匹配出所有s开头,e结尾的单词。import retext="site sea sue sweet see case sse ssee loses"m=re.findall(r"\bs\S*e\b",text) # [site,sue,see,sse,ssee]if m: print melse: print 'not match'原创 2016-08-05 15:10:06 · 824 阅读 · 0 评论 -
Python 点球小游戏
from random import choicescore=[0,0]direction =['left','center','right']def kick(): print '===You Kick!===' print 'Choice one side to kick:' print 'left ','center ','rig转载 2016-07-17 15:20:09 · 473 阅读 · 0 评论 -
Python input()
print 'What do you think I am?'this=input()if this5: print 'You are wrong!'if this==5: print 'oh,yes!'print 'How old are you?'age=input()if (age==24): print 'oh,yes!'if (age>24 or原创 2016-07-13 16:50:25 · 509 阅读 · 0 评论 -
python 点球小游戏
from random import choicescore_you=0score_com=0#默认初始分均为0direction =['left','center','right']for i in range(5): print '===Round %d - You Kick!==='%(i+1) print 'Choice one side to转载 2016-07-17 14:50:25 · 914 阅读 · 0 评论 -
python while()语句
i=1sum=0while i<101: sum+=i i+=1print sum结果:5050原创 2016-07-13 16:52:09 · 374 阅读 · 0 评论 -
Python 字符串格式化 数字游戏
from random import randintnum=randint(1,67)print 'Guess what I think?'bingo =Falsewhile bingo==False: answer = input() if answer<num: print '%d is too small!'%answer if answer>原创 2016-07-13 17:57:26 · 276 阅读 · 0 评论 -
Python 字符串格式化
print 'My age is' + str(18)str1='good'str2='bye'print str1+str2num= 18print 'My age is' + str( num )num= 20print 'My age is %d'%numprint 'Price is %f'%4.99print 'Price is %.2f'%4.99name='cro转载 2016-07-13 17:37:05 · 310 阅读 · 0 评论 -
Python 点球小游戏
from random import choiceprint 'Choice one side to shooot:'print 'left','center','right'you= raw_input()print 'You kicked '+youdirection =['left','center','right']com=choice(direction)pr转载 2016-07-17 14:31:02 · 1265 阅读 · 0 评论 -
Python 面向对象
s='how are you'#s字符串类型的对象I=s.split()#split是字符串的方法,返回一个list类型的对象 #I是list类型的对象#dir(s)#dir(list)显示:>>>dir(s)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq_原创 2016-07-29 09:24:09 · 196 阅读 · 0 评论 -
Python 面向对象(2)-1
class MyClass: pass #空的代码块mc=MyClass()print mc结果: #mc是_main_模块中MyClass类的一个实例(Instance),后面的遗传16进制的数字是这个对象的内存地址转载 2016-07-29 09:37:38 · 186 阅读 · 0 评论 -
Python 面向对象(3)
#面向过程speed=60.0distance=100.0time =distance/speedprint timeprint '--------'#面向对象class Car: speed=0 def drive(self,distance): time =distance/self.speed #必须用self.speed,否则用的是第一次的s原创 2016-07-29 09:54:55 · 302 阅读 · 0 评论 -
Python 面向对象(4)
class Vehicle: def __init__(self, speed): self.speed = speed def drive(self, distance): print 'need %f hour(s)' % (distance / self.speed)class Bike(Vehicle):转载 2016-07-29 15:22:06 · 285 阅读 · 0 评论 -
Python 面向对象(2)-2
class MyClass: #类 name='Sam' #类变量 def sayHi(self): #类方法的第一个参数必须是self print 'Hello %s'%self.namemc=MyClass() #对象print mc.name #调用类变量的方法是“对象.变量名”mc.name='Lily转载 2016-07-29 09:39:05 · 224 阅读 · 0 评论 -
Python 写文件
f=file('data.txt') #从data中读内容,保存至data1中data=f.read()print dataout=open('data1.txt','w')out.write(data)out.close()结果:I love you!hahahabiubiubiu!!!(在文件夹中创建了名为data1的txt文件,里面的内转载 2016-07-20 13:08:40 · 277 阅读 · 0 评论 -
Python 数学运算
math包里有两个常量:math.pi圆周率π:3.141592...math.e自然常数:2.718281...数值运算:math.ceil(x)对x向上取整,比如x=1.2,返回2math.floor(x)对x向下取整,比如x=1.2,返回1math.pow(x,y)指数运算,得到x的y次方math.log(x)对数,默认基底为e。可以使转载 2016-07-29 18:47:01 · 539 阅读 · 0 评论 -
Python and-or例子说明
d = ""e = "hell"f = (False and [d] or [e])print f #['hell']d = ""e = "hell"f = (False and [d] or [e])[0]print f #helld = ""e = "hell"f = (True and [d] or [e])[0]print f #空白d =原创 2016-07-29 16:21:48 · 331 阅读 · 0 评论 -
Python 二次函数的解
# -*- coding: gbk -*- #在Windows中用import mathdef result(a,b,c): derat = b**2-4*a*c if a == 0: if b != 0: x = -c / b转载 2016-08-05 10:16:09 · 8292 阅读 · 0 评论 -
Python 正则表达式(2)
import retext="Hi,I am Shirley Hilton.I am his wife."m=re.findall(r"i.",text) #['i,', 'ir', 'il', 'is', 'if']if m: print melse: print 'not match'print '---------'n=re.findall(r"i.\b",原创 2016-08-05 15:02:32 · 324 阅读 · 0 评论 -
Python 元组
postion = (1, 2)geeks = ('Sheldon', 'Leonard', 'Rajesh', 'Howard')print postion[0]for g in geeks: print gprint geeks[1:3]print "---------------"def get_pos(n): return (n/2, n*2)x, y = g转载 2016-08-05 09:46:00 · 193 阅读 · 0 评论