
python
娜然
横推八百无对手,轩辕重出武圣人
展开
-
第35个python程序:分支和函数
#!/usr/bin/env Python#-*-coding:utf-8-*-from sys import exitdef gold_room(): print "this room is full of gold. how much do you take?" next=raw_input("> ") if "0" in next or原创 2016-10-08 11:43:46 · 711 阅读 · 0 评论 -
第24个python程序:更多练习
[root@mysql1 pshell]# cat ex24.py #!/usr/bin/env Python#-*-coding:utf-8-*-print "let's practice everything."print 'you\'d need to know \'bout escapes with \\ that do \n newline and \t ta原创 2016-09-26 10:57:39 · 359 阅读 · 0 评论 -
第43个python程序:类
#!/usr/bin/env Python#-*-coding:utf-8-*-import randomfrom urllib import urlopenimport sysWORD_URL="http://learncodethehardway.org/words.txt"WORDS=[]PHRASES={ "class ###(##原创 2016-10-11 11:17:45 · 484 阅读 · 0 评论 -
python查看帮助文档的命令--pydoc
[root@mysql1 pshell]# pydoc raw_inputHelp on built-in function raw_input in module __builtin__:raw_input(...) raw_input([prompt]) -> string Read a string from standard input.原创 2016-09-02 16:33:04 · 2286 阅读 · 0 评论 -
第16个python程序:读写文件
[root@mysql1 pshell]# cat ex16.py #!/usr/bin/env Python#-*-coding:utf-8-*-from sys import argvscript,filename = argvprint "we're going to erase %r." % filenameprint "if you don原创 2016-09-08 11:11:15 · 395 阅读 · 0 评论 -
第15个python程序:读文件
[root@mysql1 pshell]# cat ex15.py #!/usr/bin/env Python#-*-coding:utf-8-*-from sys import argvscript,filename = argvtxt = open(filename)print "here's your file %r:" % filenamepri原创 2016-09-08 10:42:25 · 441 阅读 · 0 评论 -
第41个python程序1:类
[root@mysql1 pshell]# cat ex41-1.py #!/usr/bin/env Python#-*-coding:utf-8-*-class thething(object): def __init__(self): self.number=0 def some_function(self): print原创 2016-10-10 10:36:42 · 340 阅读 · 0 评论 -
第40个python程序:类
[root@mysql1 pshell]# cat ex40.py #!/usr/bin/env Python#-*-coding:utf-8-*-class song(object): def __init__(self, lyrics): self.lyrics=lyrics def sing_me_a_song(self):原创 2016-10-10 10:09:07 · 405 阅读 · 0 评论 -
第14个python程序:提示和传递
[root@mysql1 pshell]# cat ex14.py #!/usr/bin/env Python#-*-coding:utf-8-*-from sys import argvscript,user_name,user_id = argvprompt = '>> 'print "hi %s, i'm the %s script. your原创 2016-09-07 18:17:10 · 394 阅读 · 0 评论 -
第21个python程序:函数可以返回东西
[root@mysql1 pshell]# cat ex21.py #!/usr/bin/env Python#-*-coding:utf-8-*-def add(a,b): print "adding %d + %d" % (a,b) return a+bdef subtract(a,b): print "subtracting %d - %d"原创 2016-09-20 11:33:13 · 406 阅读 · 0 评论 -
第25个python程序
[root@mysql1 pshell]# cat ex25.py#!/usr/bin/env Python#-*-coding:utf-8-*-def break_words(stuff): """this function will break up words for us.""" words=stuff.split(' ') return words原创 2016-09-26 11:34:34 · 651 阅读 · 1 评论 -
第44个python程序:类的继承(隐式)
[root@mysql1 pshell]# cat ex44.py #!/usr/bin/env Python#-*-coding:utf-8-*-class parent(object): def implicit(self): print "parent implicit()"class child(parent): p原创 2016-10-12 10:34:34 · 718 阅读 · 0 评论 -
第44个python程序:类的继承(显示覆写)
[root@mysql1 pshell]# cat ex44-2.py #!/usr/bin/env Python#-*-coding:utf-8-*-class parent(object): def override(self): print "parent override()"class child(parent):原创 2016-10-12 10:39:52 · 776 阅读 · 0 评论 -
一个小的python输出函数测试
[root@mysql1 pshell]# cat test.py #!/usr/bin/env Python#-*-coding:utf-8-*-stuff=raw_input('> ');words=stuff.split();print words;[root@mysql1 pshell]# py原创 2016-10-17 10:39:54 · 487 阅读 · 0 评论 -
第33个python程序:while循环
[root@mysql1 pshell]# cat ex33.py #!/usr/bin/env Python#-*-coding:utf-8-*-i=0numbers=[]while i print "at the top i is %d" % i numbers.append(i) i=i+1 print "numnbers now: "原创 2016-09-30 15:36:38 · 356 阅读 · 0 评论 -
第32个python程序:循环和列表
[root@mysql1 pshell]# cat ex32.py #!/usr/bin/env Python#-*-coding:utf-8-*-the_count=[1,2,3,4,5]fruits=['apples','oranges','pears','apricots']change=[1,'pennies',2,'dimes',3,'quarters']原创 2016-09-30 15:06:28 · 388 阅读 · 0 评论 -
第31个python程序:逻辑判断
[root@mysql1 pshell]# python ex31.pyyou enter a dark room with two doors. do you go through door #1 or door #2?> 1there's a giant bear here eating a cheese cake. what do you do?1. take the cak原创 2016-09-29 17:32:43 · 1144 阅读 · 0 评论 -
第30个python程序:else和if
[root@mysql1 pshell]# cat ex30.py #!/usr/bin/env Python#-*-coding:utf-8-*-people=30cars=40buses=15if cars>people: print "we should take the cars."elif cars print "we sho原创 2016-09-29 17:01:39 · 327 阅读 · 0 评论 -
第29个python程序:if语句
[root@mysql1 pshell]# cat ex29.py #!/usr/bin/env Python#-*-coding:utf-8-*-people=20cats=30dogs=15if people print "too many cats! the world is doomed!"if people>cats:原创 2016-09-28 15:09:23 · 326 阅读 · 0 评论 -
第28个python程序:布尔表达式练习
[root@mysql1 pshell]# pythonPython 2.6.6 (r266:84292, Jan 22 2014, 01:49:05) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2Type "help", "copyright", "credits" or "license" for more information原创 2016-09-28 14:54:13 · 1091 阅读 · 0 评论 -
第44个python程序:类的合成
[root@mysql1 pshell]# cat ex44-4.py #!/usr/bin/env Python#-*-coding:utf-8-*-class other(object): def override(self): print "other override()" def implicit(self): prin原创 2016-10-12 11:28:54 · 622 阅读 · 0 评论 -
第44个python程序:类的继承(在运行前和后覆写)
[root@mysql1 pshell]# cat ex44-3.py #!/usr/bin/env Python#-*-coding:utf-8-*-class parent(object): def altered(self): print "parent altered()"class child(parent): d原创 2016-10-12 10:55:41 · 456 阅读 · 0 评论 -
第39个python程序:字典dict
[root@mysql1 pshell]# cat ex39.py #!/usr/bin/env Python#-*-coding:utf-8-*-class song(object): def __init__(self,lyrics): self.lyrics=lyrics def sing_me_a_song(self): for line原创 2016-10-09 10:59:49 · 345 阅读 · 0 评论 -
第38个python程序:列表的操作
[root@mysql1 pshell]# cat ex38.py #!/usr/bin/env Python#-*-coding:utf-8-*-# create a mapping of state to abbreviationstates={ 'oregon': 'or', 'florida': 'fl', 'california': 'c原创 2016-10-09 10:24:41 · 389 阅读 · 0 评论 -
python中的字符集
如果有中文,并且是utf8的中文[root@mysql1 pshell]# cat ex1.py #!/usr/bin/env Pythonprint "高"#print "hello world!"print "hello again"print "i like typing this."print "this is fun."print 'yay原创 2016-08-24 09:27:39 · 659 阅读 · 0 评论 -
安装MySQL-python
[root@mysql1 ~]# cd MySQL-python-1.2.3[root@mysql1 MySQL-python-1.2.3]# lltotal 160drwxrwxr-x 2 mycat mycat 4096 Jun 17 2010 doc-rw-rw-r-- 1 mycat mycat 10285 Jun 17 2010 ez_setup.py-rw-r原创 2016-08-11 17:14:02 · 493 阅读 · 0 评论 -
ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory
1.查看libmysqlclient.so.18的路径# find / -name libmysqlclient.so.182.链接# ln -s /mysql/base/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18我这里是64位的,如果是32位的链接到/usr/lib原创 2016-08-11 17:11:04 · 2887 阅读 · 2 评论 -
NameError: name 'exception' is not defined
报错的程序:#!/usr/bin/env python#encoding=utf-8import MySQLdbtry: conn=MySQLdb.connect(host="192.168.10.12",user="gw",passwd="gw",db="gw") cur=conn.cursor() cur.execute("set autocommit=0")原创 2016-08-11 17:06:05 · 7527 阅读 · 0 评论 -
IndentationError: expected an indented block
出错的程序如下:#!/usr/bin/env python#encoding=utf-8import mysqldbtry:conn=mysqldb.connect(host="192.168.10.12",user="gw",passwd="gw",db="gw")cur=conn.cursor()cur.execute("set autocommit=0")cu原创 2016-08-11 16:22:36 · 973 阅读 · 0 评论 -
第11个python程序:接收输入
[root@mysql1 pshell]# cat ex11.py#!/usr/bin/env Python#-*-coding:utf-8-*-print "how old are you?",age = raw_input()print "how tall are you?",height = raw_input()print "how much do yo原创 2016-09-01 11:41:54 · 1209 阅读 · 0 评论 -
第10个python程序:转义
[root@mysql1 pshell]# cat ex10.py #!/usr/bin/env Python#-*-coding:utf-8-*-print "i am 6'2\" tall." #将字符串中的双引号转义print 'i am 6\'2" tall.' #将字符串中的单引号转义#使用3引号""",可以在3引号之间放入任意多行的文字原创 2016-09-01 10:55:14 · 536 阅读 · 0 评论 -
第17个python程序:拷贝文件
[root@mysql1 pshell]# cat ex17.py #!/usr/bin/env Python#-*-coding:utf-8-*-from sys import argvfrom os.path import existsscipt, from_file, to_file = argvprint "copying from %s t原创 2016-09-12 15:23:07 · 487 阅读 · 0 评论 -
第9个python程序:打印,打印,打印
[root@mysql1 pshell]# cat ex9.py #!/usr/bin/env Python#-*-coding:utf-8-*-# here's some new strange stuff,remember type it exactly.days = "mon tue wed thu fri sat sun"months = "jan\nf原创 2016-08-31 18:02:36 · 867 阅读 · 0 评论 -
第2个python程序:注释#
[root@mysql1 pshell]# cat ex2.py #!/usr/bin/env Python#-*-coding:utf-8-*-# a comment,this is so you can read your program later.print "i could have code like this." # and the comment a原创 2016-08-24 09:33:51 · 353 阅读 · 0 评论 -
第1个python程序:打印
[root@mysql1 pshell]# cat ex1.py #!/usr/bin/env Pythonprint "hello world!"print "hello again"print "i like typing this."print "this is fun."print 'yay! printing.'print "i'd much rath原创 2016-08-24 09:19:21 · 457 阅读 · 0 评论 -
第12个python程序:提示输入
[root@mysql1 pshell]# cat ex12.py #!/usr/bin/env Python#-*-coding:utf-8-*-age = raw_input("how old are you? ")height = raw_input("how tall are you? ")weight = raw_input("how much do you原创 2016-09-02 16:28:02 · 942 阅读 · 0 评论 -
第6个python程序:字符串和文本
[root@mysql1 pshell]# cat ex6.py#!/usr/bin/env Python#-*-coding:utf-8-*-x="there are %d types of people." %10binary="binary"do_not="don't"y="those who know %s and those who %s." %(bina原创 2016-08-26 18:33:54 · 345 阅读 · 0 评论 -
第20个python程序:函数和文件
[root@mysql1 pshell]# cat ex20.py #!/usr/bin/env Python#-*-coding:utf-8-*-from sys import argvscript,input_file=argvdef print_all(f): print f.read()def rewind(f): f.s原创 2016-09-19 11:48:24 · 775 阅读 · 0 评论 -
第18个python程序:命名变量代码函数
[root@mysql1 pshell]# cat ex18.py #!/usr/bin/env Python#-*-coding:utf-8-*-# this one is like your scripts with argvdef print_two(*args): arg1,arg2=args print "arg1: %r,arg2: %r" %原创 2016-09-19 11:19:18 · 424 阅读 · 0 评论 -
第19个python程序:函数和变量
[root@mysql1 pshell]# cat ex19.py #!/usr/bin/env Python#-*-coding:utf-8-*-def cheese_and_crackers(cheese_count,boxes_of_crackers): print "you have %d cheeses!" % cheese_count print "you原创 2016-09-19 11:16:14 · 509 阅读 · 0 评论