Python从零学起成长励志读书

[Python学习路线]--Python基础no.03

2019-04-23  本文已影响1人  FANGQI777

回顾之前的内容

  1. 了解了文本和字符串
  2. 学习了变量和命名规范
  3. 巩固了打印的多种使用

1. 参数、解包和变量

类似于Java的args[],Python在调用时也有一个类似的传参方式argv
下图中第4行也可以称为解包.

argv
FANGQIdeMacBook-Pro:PythonStudy fangqi$ python3 ex8.py random1 haha heihei
Called script is: ex8.py
The first parameter is: random1
The second parameter is: haha
The third parameter is: heihei

解包之后这些参数就可以在此程序中正常使用了。

2. input巩固

通过向input传入变量,设计使得程序提示变得更加友好。


input(prompt)
FANGQIdeMacBook-Pro:PythonStudy fangqi$ python3 ex9.py fangqi
The coding author is fangqi, I'm the ex9.py script.
Let's do some simple test for you
What's your name?
> xiao7
What's your favoutite food?
> fish
Do you like coding?
> yes
What species are you?
> cat

Alright, your name is xiao7, your like food is fish,
and you said you yes coding. then you are a cat

3. 读取文件

FANGQIdeMacBook-Pro:PythonStudy fangqi$ python3 ex10.py ex10_operate.txt
Here is your file ex10_operate.txt:
《Had I not seen the Sun》
When you are old and grey and full of sleep,
And nodding by the fire,take down this book,
And slowly read,and dream of the soft look
Your eyes had once,and of their shadows deep;
How many loved your moments of glad grace,
And loved your beauty with love false or true,
But one man loved the pilgrim Soul in you
And loved the sorrows of your changing face;
And bending down beside the glowing bars,
Murmur,a little sadly,how Love fled
And paced upon the mountains overhead
And hid his face amid a crowd of stars.
Type the file name again
> ex10_operate.txt
《Had I not seen the Sun》
When you are old and grey and full of sleep,
And nodding by the fire,take down this book,
And slowly read,and dream of the soft look
Your eyes had once,and of their shadows deep;
How many loved your moments of glad grace,
And loved your beauty with love false or true,
But one man loved the pilgrim Soul in you
And loved the sorrows of your changing face;
And bending down beside the glowing bars,
Murmur,a little sadly,how Love fled
And paced upon the mountains overhead
And hid his face amid a crowd of stars.

4. 读写文件

文件的操作:

读写_01.png 读写_02.png

使用open()函数
以任何方式使用文件之前,都必须打开文件;打开文件使用open()函数,其返回一个文件对象
打开模式,如下
'r' 读取模式
'w' 写入模式
'a' 附加模式
'r+' 读取和写入模式
默认 只读模式

FANGQIdeMacBook-Pro:PythonStudy fangqi$ python3 ex11.py ex11_operate.txt 
We are going to do some file exercise in ex11_operate.txt
If you don't want this, press the Control + C(^C).
If you do want do this, press return.
> 
Opening
adjwi
ceiw
dwdwoo
Let's truncate the file!
Now you can input the information what you want input to file
Line 1:窗前明月光
Line 2:疑是地上霜
Line 3:?????
Then let's write these in the file
Now the file information is:
窗前明月光
疑是地上霜
?????
Finally, let's close this file
上一篇下一篇

猜你喜欢

热点阅读