Python 基础 二
使用 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 基础二(学习打卡) 一.None值 二.函数返回多个值 三.局部变量和全局变量 四.异常处理(内含Zigzag代码) 五.列表 六 ...
- Python 基础二
一.Python介绍 1.python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打 ...
- 第一模块:Python基础(二)
目录 1.变量 常量 2.用户交互和注释 程序交互 注释 字符串 布尔型(bool) 格式化输出 运算符 while 循环 @(开发基础) 1.变量 变量用于存储要在计算机程序中引用和操作的信息.它们 ...
- python基础二 函数 递归 装饰器 迭代器 生成器 内置函数 二分法应用
函数 迭代器 生成器 拆包和装包 tup=("j1","a1","c1","k1") a,b,c,d=tup print ...
- py哪个函数可以返回输入的变量类型_[Python基础]二、pycharm,python变量
2.1 Python简介 Life is short,you need Python (人生苦短,我用Python) 解释器: 将其他语言翻译成机器语言的工具,称为编译器 编译器的翻译方法有两种: 编 ...
- python 基础(二)
循环 for name_k = ['teng', '红雀','rommel'] for i in name_k:if i == "红雀":print "超级碗是%s的&q ...
- python基础二:函数
2019独角兽企业重金招聘Python工程师标准>>> 1:函数定义 def fun_name: 2:函数返回用return,如果是返回None,可以直接用return代替,返回多个 ...
- Python基础二_操作字符串常用方法、字典、文件读取
一.字符串常用方法: name.captitalize() #字符串首字母大写 name.center(50,'*') ...
- 03.【python基础二】if判断语句之if-else、elif、if嵌套
最新文章
- C语言中的struct结构体对齐问题
- 华南理工计算机基础知识题,华南理工_计算机应用基础_随堂练习答案(2017年)
- 201803考试批次2C 程序设计语言,201803考试批次2可视化程序设计(VB)D卷
- 编译安装appach遇到的那些事
- 【CodeForces】961 F. k-substrings 字符串哈希+二分
- 用美颜照当广告犯法!要么就标注“照骗”,挪威针对明星网红出手了
- Android keymaster的介绍和总结
- Abp v2.8.0发布 路线图
- 递归删除.svn文件夹以及文件
- Java 算法 打水问题
- 调用css样式是不调用某个属性,CSS选择器可以引用另一个选择器属性吗?
- ACM解题总结——HihoCoder1237 (微软笔试题)
- linux yum命令详解,yum命令详解
- 书写阿拉伯数字 0、1、2、3、4、5、6、7、8、9
- Java操作Excel并显示到网页
- HTML+CSS实现渐变色标签,鼠标经过效果
- Windows 中 TCP 端口 139 和 445 的使用
- VBA-使用msgbox对话框
- 腾讯云GAME-TECH游戏开发者技术沙龙(深圳)开启报名
- Android开发免费短信验证码SDK。
热门文章
- 开发者社区SDK对应版本更迭信息
- RDKit | 基于RDKit的单分子多构象生成
- Survey | 基于图卷积网络的药物发现方法
- Factorization Machine
- python常见的文本处理-fasta文件格式处理
- 零基础入门学习Python(31)-异常处理2-try语句
- Science Bulletin:崔杰组发表了深浅海软甲纲动物比较病毒组学分析成果
- 眼界,是学习撑大的!
- 南农Nature Microbiology一作顾少华:我与铁载体的这5年
- 255套绝美ppt模板!适用于总结汇报、形象宣传、授课培训、项目介绍、毕业答辩等...