1.python的斐波那契数:
1 def fab(max): 2 n,a,b=0,0,1 3 while n<max: 4 print b 5 a,b=b,a+b 6 n=n+1 7 8 fab(4)
2.python求偶数(奇数可以推出,注意缩进)
1 def test(n): 2 var=0 3 L=[] 4 for var in range(n): 5 if (var%2==0): 6 print var 7 test(7)
3.python对数据库的操作(注意要导入,库MySQLdb)下载地址http://sourceforge.net/projects/mysql-python/
1 #coding=utf-8 2 import MySQLdb 3 4 conn= MySQLdb.connect( 5 host='127.0.0.1', 6 port = 3306, 7 user='root', 8 passwd='miao', 9 db ='testweb', 10 ) 11 cur = conn.cursor() 12 13 #创建数据表 14 #cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))") 15 16 #插入一条数据 17 cur.execute("insert into student values('2','Tom','3 year 2 class','9')") 18 19 20 #修改查询条件的数据 21 #cur.execute("update student set class='3 year 1 class' where name = 'Tom'") 22 23 #删除查询条件的数据 24 #cur.execute("delete from student where age='9'") 25 26 cur.close() 27 conn.commit() 28 conn.close()