前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 基础 二

Python 基础 二

作者头像
用户1148525
发布2019-05-26 12:15:11
3010
发布2019-05-26 12:15:11
举报
文章被收录于专栏:机器学习、深度学习

使用 python 2.6 源于:http://wiki.jikexueyuan.com/project/learn-python-hard-way/

接收参数: from sys import argv

script, first, second, third = argv

print “The script is called:”, script print “Your first variable is:”, first print “Your second variable is:”, second print “Your third variable is:”, third

$ python ex13.py first 2nd 3rd The script is called: ex13.py Your first variable is: first Your second variable is: 2nd Your third variable is: 3rd

exercise14 提示和传递: from sys import argv

script, user_name = argv prompt = ‘> ‘

print “Hi %s, I’m the %s script.” % (user_name, script) print “I’d like to ask you a few questions.” print “Do you like me %s?” % user_name likes = raw_input(prompt)

print “Where do you live %s?” % user_name lives = raw_input(prompt)

print “What kind of computer do you have?” computer = raw_input(prompt)

print “”” Alright, so you said %r about liking me. You live in %r. Not sure where that is. And you have a %r computer. Nice. “”” % (likes, lives, computer)

exercise16.读写文件 •close – 关闭文件。跟你编辑器的 文件->保存.. 一个意思。 •read – 读取文件内容。你可以把结果赋给一个变量。 •readline – 读取文本文件中的一行。 •truncate – 清空文件,请谨慎使用该命令。 •write(‘stuff’) – 将 stuff 写入文件。

from sys import argv

script, filename = argv print “Opening the file…” target = open(filename, ‘w’) print target .read() print “Truncating the file. Goodbye!” target.truncate()

print “Now I’m going to ask you for three lines.”

line1 = raw_input(“line 1: “) line2 = raw_input(“line 2: “) line3 = raw_input(“line 3: “)

print “I’m going to write these to the file.”

target.write(line1) target.write(“\n”) target.write(line2) target.write(“\n”) target.write(line3) target.write(“\n”)

print “And finally, we close it.” target.close()

‘w’表示”以写(write)模式。有’r’表示只读模式,’a’表示追加模式 w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。 r+ 以可读写方式打开文件,该文件必须存在。 a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留

exercise17.更多文件操作

from sys import argv from os.path import exists

script, from_file, to_file = argv

print “Copying from %s to %s” % (from_file, to_file)

// we could do these two on one line, how? in_file = open(from_file) indata = in_file.read()

print “The input file is %d bytes long” % len(indata)

print “Does the output file exist? %r” % exists(to_file) print “Ready, hit RETURN to continue, CTRL-C to abort.” raw_input()

out_file = open(to_file, ‘w’) out_file.write(indata)

print “Alright, all done.”

out_file.close() in_file.close()

exercise18.命名, 变量, 代码, 函数 函数定义:

def print_one(arg1): print “arg1: %r” % arg1

print_one(“First!”)

def secret_formula(started): jelly_beans = started * 500 jars = jelly_beans / 1000 crates = jars / 100 return jelly_beans, jars, crates

start_point = 10000 beans, jars, crates = secret_formula(start_point)

定义 ex25.py 如下: def break_words(stuff): “”“This function will break up words for us.”“” words = stuff.split(’ ‘) return words

$ python import ex25 sentence = “All good things come to those who wait.” words = ex25.break_words(sentence) words

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年12月02日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档