《笨办法学python3》的学习笔记(10-15)节

来源:互联网 发布:新股自动申购软件 编辑:程序博客网 时间:2024/06/03 13:04

10.那是什么?

两种字符串扩展到多行的方法。
第一种是:使用”“”三引号。在三引号之间可以放入多行的字符串。
第二种:在行之间插入一个换行的字符。使用反斜杠\可以将难打印出来的字符放到字符串。\配合不同的字符表达不同的含义。其中\是用来打印出一个\来。
这里引用书中的一段话:

一种重要的序列是将’和”进行转义。比如:”I “understand” me”这种句子如果不转义会让python 认为”I “和”me“是一起的。解决的办法就是将双引号和单引号都转义。

“I am 6’2\” tall.” #将字符串中的双引号进行转义
‘I am 6\’2” tall.’ #将字符串中的单引号进行转义

程序.ex10.py:

tabby_cat="I'm tabbed in."persian_cat="\tI'm split on a line."backslansh_cat="I'm\\a\\cat."fat_cat="""I'll do a list:\t* Cat food\t* Fishies\t* Catnip\n\t* Grass"""print tabby_catprint persian_catprint backslansh_catprint fat_cat
  • 注意正确的写法是\n和\
  • 如果将%s替换称%r那么转义序列将会失效,因为原语句中包含了原始的转义字符,因此用于调试的还是%s比较好。
  • -

习题11.提问

前面的基本上都是将给定的简单语句进行输出。我理解的程序就死接受输入,打印输出
程序ex11.py:

print"How old are you?",age=raw_input()print"How tall are you?",height=raw_input()print"How much do you weight?",weight=raw_input()print"So,you 're %r old,%r tall and %r heavy."%(age,height,weight)
  • 每一行的末尾加上逗号是使得程序在这行执行完毕之后进行暂留,默认的直接运行到了下一行。
  • raw-input()input()的函数是不相同的。这里找了一下区别:
  • -

raw_input()和input()都可以读取控制台的输入,但是在处理纯数字时是有区别的。

1.纯数字输入:input返回的是数值类型:float,int等;raw_input返回的是字符类型:string
2.输入的字符串为表达式:input会计算在字符中的数字表达式,而raw_input**还是保持原样.**-输入25+5 则raw_input得到的是25+5,而input得到的是30.
3.python input 的实现 input实际上是通过raw_input来实现的.

简单的讲就是input更加的智能也就是书中说的会把输入的东西当做python的代码来处理,所以没要求的话针对数字还是使用raw_input函数为好.


12.提示别人

raw_input()这个()是可以放入一个想要作为提示的字符串.(提示用户要输入的什么内容).
一个简单的例子就是a=raw_input(“how old are you?”)-这段话会显示出how old are you /来提示用户输入,输完之后给变量y来保存.
程序ex12.朋友:

age=raw_input("How old are you?")height=raw_input("How tall are you?")weight=raw_input("How much do you weight?")print"So,you're %r old,%r tall and %r heavy."%(age,height,weight)

13.参数,解包和变量

12 中提到的将变量写读到程序中的方法是利用raw_input()函数读取用户的输入然后再输出.我们运行的方式是在终端中加入;python exX.py运行这个脚本文件.可以认为XX.py就是一个参数(filename作为参数)
程序ex13.py:

from sys import argvscript,first,second,thrid=argvprint"The script is called:",scriptprint"Your first variable is: ",firstprint"Your second varibale is:",secondprint"Your third varibale is: ",third
  • argv的意思是参数变量,这个变量**保存运行python脚本时传递给python脚本的参数.**a,b,c…=argv的含义是解包;就是将argv接受到的参数值赋给左侧的每一个对应的变量.
  • 第一行是从sys库中导入需要的导入argv模块,有的语言也称之为库.
    程序ex13.py(与以往不同的是需要在运行脚本前就将参数给出):

  • argv和raw_input()的不同之处在于用户是什么时候输入的.在执行命令时输入argv如果是在脚本运行过程中输入那么就是raw_input()


14.提示和传递

这里体现了人机交互的一面.使用raw_input让它显示一个简单的>提示符这样会使得人们在输入时更加的有互动性和可读性.
程序如下(照例还是需要添加一个参数)ex14.py:

from sys import argvscript,user_name=argvprompt='>'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_namelikes=raw_input(prompt)print"where do you live %s?"% user_namelives=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)

15.读取文件

读操作和写操作是文件是必备的基础.这个示例里我们需要用脚本文件(.py)打开你所在的txt文件(文本文件)这里我们要用raw_input和argv从用户那里获取信息从而明白需要打开什么如果一开始的时候就限制文件名字,那么我们是无法再次更改要打开的文件名的.
程序ex15.py(打开的文本为ex15_sample.txt):
目标文件:ex15_sample.txt:

This is stuff I typed into a file.It is really cool stuff.Lots and lots of fun to have in here.执行命令:

python ex15.py ex15_sample.txt

运行的程序为:

from sys import argv

script,filename=argv

txt=open(filename)

print”Here’s your file %r:”%filename
print txt.read()

print”Type the filename again:”
file_again=raw_input(“>”)

txt_again=open(file_again)

print txt_again.read()

  • 打开文件的过程是:*open命令会接收一个参数目的文件名,返回一个参数打开的状态?,将这个值赋值给一个变量最终文件进行的操作.*
  • 作者说从open处获得的东西是一个文件file.文件本身也支持一些命令,他接受命令的方式是使用一些句点.紧跟着你的命令.然后是执行的动作.write或者read等等
  • -

txt=open(filename)返回的东西不是文件的内容是叫做file object.可以随便的访问内容的任意位置,然后读取这些内容.但是文件本身并不是它的内容.

0 0
原创粉丝点击