好久不碰Python了,一些基础的内容都有些忘了,只好重新拾起书籍看看,最近一段时间应该要好好的看看《learn Python 3 in the hard way》这本书了。
先说说这本书吧,我是被这个书名吸引的,然后就陷入了深渊(没找到中文版),就着英文版看一阵吧,可以安慰自己看书同时学学提升提升英语水平(强装开心。。)。这本书看了大概10%,觉得主要是为了那些学了一点编程基础的童鞋写的,里面写的比较的易懂,有一定的练习,还有一套配套的视频(纯正美式发音),不过对于内部深入的知识感觉没有较好的展示,毕竟面向的主要对象是那些基础浅或者没有基础的同学。但是粗略学过python的同学也是可以看看的,看一章大概就花个最多十分钟左右,很快就能看完。好,就此打住,说说今天的收获吧!
毕竟是书的开头,主要是一些基础的知识:print() input()
等
后面还写了一些其他的东西import argv
还有一些关于文件的方法
这里面说一下后面的东西吧
1、import: import 在英文里面的意思是引进,引进什么呢?引进一些模块、包,
这些模块和包就是别人已经写好了方法,通过引进可以直接在我们的代码里使用而不是自己重新写一遍,在本书中前面用到的是
这里导入了一个sys模块里的argv的包,argv包的作用是:接收用户的输入
下面放一个ex16的内容,里面主要是关于文件的操作,有清空:truncate()
有打开:open(),有输入:write()
有一些关于转义字符、导包,输入的一些其他操作,感觉还可以。
from sys import argv
script, filename = argv
print(f"We are going to erase {filename}")
print("If you don't want that, hit ctrl-c (^c).")
print("If you do want that, hit return.")
input("?")
print("Opening the file ")
target = open(filename, 'w')
print("Truncating the file. Goodbye!")
target.truncate()
print("Now I'm going to ask you for three lines.")
line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = 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()
在ex10中自己还在加减乘除操作的基础上增加了几个if语句,让它变成了一个加减乘除计算器,以下是代码:
print("Please input your x value: ", end = ' ')
x = int(input())
print("What about the y value: ", end = ' ')
y = int(input())
z = input('what do you want to deal with x and y ?')
if (z == '+'):
print('the result is ', x + y)
elif (z == '-'):
print('the result is ', x - y)
elif (z == '*'):
print('the result is ', x * y)
else:
print('the result is ', x / y)
刚开始还是挺容易的,重温了一些基础的操作,加油!