昨天开始,我边学c++边开始学习python。两种语言的比较是一件有趣的事情。我现在
对python的了解还不够,不适合发表什么长篇大论呵呵,但是我觉得python和c++确实有很
多的不同,比如我在python不需要管理内存,所有对象都由解释器来进行引用计数。有学
习python这个想法是源于前几天的一场讲座,周一民教授关于开源浪潮的报告,报告中他
就推荐我们学习python,我对python是一点都不了解,只是在编程排行榜上看见过,也不
知道这个是脚本语言。。。。。很是无知,还好,据说python是一种很容易上手的语言,
而且又和各种语言的接口很好,希望我能好好掌握。用的教材是PDF版本的python核心编
程,去图书馆查了一下,竟然只有一本关于python的书籍,可见python在国内的冷落。
今天帖两个代码,纪念自己开始python的旅途开始,纯粹的第一个python实际上我还是用
的hello world。不过今天写了个稍微长一点的,呵呵
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 2.6 >>> def writeToFile(): '一个用来把内容保存到文件的脚本' import os ls = os.linesep while True: fname = raw_input('输入要输出到的文件名') if os.path.exists(fname): print 'FILE IS ALREAY EXIST,TRY AGAIN' else: break temp = [] #用来接收输入 while True: line = raw_input('输入一行') if line == '.': break else: temp.append(line) #输出到文件 hand = open(fname,'w') for eachline in temp: hand.write('%s%s'%(eachline,ls)) hand.close() Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 2.6 >>> def readFromFile(): fname = raw_input('输入你要打开的文件名') import os hand = open(fname,'r') while True: if os.path.exists(fname): hand = open(fname,'r') break else: print 'error try again\n' fname = raw_input('输入你要打开的文件名') for eachline in hand: print '%s'%eachline, hand.close()
|
阅读(1200) | 评论(0) | 转发(0) |