Python 基础 二
来源:互联网 发布:opencv2.4.4 sift算法 编辑:程序博客网 时间:2024/06/05 03:06
使用 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
- Python基础学习二
- Python 基础二
- Python 基础语法(二)
- Python基础学习二
- python基础(二)
- Python 基础语法(二)
- Python 基础语法(二)
- Python 基础语法(二)
- python基础(二)
- Python 基础语法(二)
- python基础二
- Python基础(二)
- python基础语法(二)
- Python 基础语法(二)
- python 基础(二)
- Python基础(二)
- Python 基础 二
- Python 基础语法(二)
- 【数论】【逆元】【贪心】HDU5976 Detachment
- 第十四周项目一数组大折腾(2)创建一个长度为10的整形数组并初始化,由后往前(由第9个元素向第0个元素方向)输出数组中所有元素的值,改变数组元素的值,令所有的数组加倍,输出改变后的值。
- linux下安装jdk的步骤和方法
- Android复习之旅--ViewPager(自动轮播条)
- Swift3 基础语法
- Python 基础 二
- POJ 2531(搜索剪枝)
- git命令 branch 切换远程分支Github
- intellij IDEA 启动tomcat时显示端口被占用
- servlet3.0 getPart()与getParts()获取上传文件
- Redis 主从复制
- CSS 设置字体大小用法介绍
- Java常考编程题(基础)--不断更新
- Web项目中使用Spring 3.x + Quartz 2.x实现作业调度详解