简明python教程 学习

查漏补缺,更加系统的学习吧
感觉这本书有些老旧了吧
不过让那个实战教程搞的头都大了

心得体会

  1. IDE的使用,我一直都不知道可以用,书上还特意说了不要用Nopad,因为没有相应的语法高亮。
  2. 字符串的乘法也有讲
    ‘ha’*3 =’hahaha’
    n = 25.5%2.25
    print(n)
    书上说是1.5
    我试了一下,是0.75,我觉得应该就是0.75
    哦,书上是-25.5

  3. &位与
    |位或
    ^位异或
    ~位翻转

  4. 还非常贴心有一些给其它语言程序员的小tips

  5. while在最后还可以加一个else,相当于while…else…了,话说直接while后面加不行吗?书的作者也觉得多余……

  6. 诶,while…else…有用了,如果在while内置了break,执行break,直接跳出while,else便不会被执行

  7. contine函数,很不常用啊,没什么特别的

  8. global好像不是在声明时使用的,函数内调用时才声明

  9. 关键参数指定赋值时可以不分先后,虽然感觉也没啥用

  10. DocStrings,一个新东西…不过这个教程确实有点过分简明了,真碰到不会的还真不好懂。
    为了增强代码的可读性,可以在函数后书写“文档字符串”(Documentation Strings,或者简称docstrings),用于解释函数的作用、参数的类型与意义、返回值类型与取值范围等。它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。可以使用.__doc__(注意双下划线)调用printMax函数的文档字符串属性(属于函数的名称)

  11. pyc文件,有点太简洁了
    是一种py文件的经过几层编译后输出的文件,比python语言更加底层,相对于py文件可以被更快的执行,一般被import的文件会生成pyc文件方便下次调用。

  12. from…import…可以直接获取from里的变量,注意变量名冲突就好了。

  13. __name__只有被执行的模块的__name__才会是__main__,其它被import的都不是。可以被用来做

  14. 当你为dir()提供一个模块名的时候,它返回模块定义的名称列表。如果不提供参数,它返回当前模块中定义的名称列表

  15. print(x,end = ” )即在print函数后面加上end = ”即可不换行,嗯……print函数里不知道有多少个形参呢

  16. 话说这本书的实战跨度也不太小,不过还好

  17. 这本书的打开文件教程里没有with,估计那时候还没有吧

  18. 18.

实战心得

版本一

初稿:

import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = [r'G:\xx\pyt.py', r'G:\yy\new.txt']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that
# 2. The backup must be stored in a main backup directory
target_dir = r'G:\xx'# Remember to change this to what you will be using
# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date and time
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'
# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command='"H:\WinRAR\Rar.exe" a %s %s'%(target,' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
    print ('Successful backup to', target)
else:
    print ('Backup FAILED')

相对而言理解起来要简单不少,只有在压缩文件命令那里出现了问题
windows本身不是自带zip命令的,这就需要上网搜索
由于使用的是winrar,所以对应的压缩命令也应选用winrar的,可是更换了压缩命令,配置了环境变量,仍然不行,只有将压缩命令改为

zip_command='"H:\WinRAR\Rar.exe" a %s %s'%(target,' '.join(source))

在顺利运行后,检测结果,发现和预想的有些差距,压缩后的文件本来在设计中应该放入G盘的xx文件夹内,结果却成了G盘里一个以xx为文件名开头的文件。将r'G:\xx'变为r'G:\xx\'后,依然报错,
这里写图片描述
看了一下,很像是\把’转义了,但是加r的不是说好不转义的吗。然后变成双斜杠,便可以成功运行了,但是感觉好蠢,于是上网搜索是怎么回事,具体解释倒是没有,不过找到了解决办法,就是将斜杠变成反斜杠即可(终于知道谁是正的谁是反的了)(卧槽,书上说的正反和这里不一样啊)。代码如下:
终稿:

import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = [r'G:\xx\pyt.py', r'G:\yy\new.txt']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that
# 2. The backup must be stored in a main backup directory
target_dir = r'G:/xx/'# Remember to change this to what you will be using
# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date and time
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'
# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command='"H:\WinRAR\Rar.exe" a %s %s'%(target,' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
    print ('Successful backup to', target)
else:
    print ('Backup FAILED')

版本二

日期为目录,时间为文件名
我猜是把代码改成这样

target = target_dir + time.strftime('%Y%m%d') + '\\'+ time.strftime('%H%M%S') + '.zip'

很可惜不能运行。
终稿:

import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = [r'G:\xx\pyt.py', r'G:\yy\new.txt']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that
# 2. The backup must be stored in a main backup directory
target_dir = r'G:/xx/'# Remember to change this to what you will be using
# 3. The files are backed up into a zip file.
# 4. The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')
# Create the subdirectory if it isn't already there
if not os.path.exists(today):  #判断文件夹是否存在
    os.mkdir(today) # make directory 创建目录
    print ('Successfully created directory', today)
# The name of the zip file
target = today + os.sep + now + '.zip'#os.sep根据你的操作系统给出目录分隔符,即在Linux、Unix下它是'/',在Windows下它是'\\',而在Mac OS下它是':'。使用os.sep而非直接使用字符,会使我们的程序具有移植性,可以在上述这些系统下工作。
# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command='"H:\WinRAR\Rar.exe" a %s %s'%(target,' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
    print ('Successful backup to', target)
else:
    print ('Backup FAILED')

版本三

可以手动输入些注释进去,并加在文件名后面(当然可以不加)
终稿:

import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = [r'G:\xx\pyt.py', r'G:\yy\new.txt']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that
# 2. The backup must be stored in a main backup directory
target_dir = r'G:/xx/'# Remember to change this to what you will be using
# 3. The files are backed up into a zip file.
# 4. The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')
# Take a comment from the user to create the name of the zip file
comment = input('Enter a comment --> ')
if len(comment) == 0: # check if a comment was entered
    target = today + os.sep + now + '.zip'
else:
    target = today + os.sep + now + '_' +comment.replace(' ', '_') + '.zip'#replace替换函数,可以将字符串内的所有前边的都换成后边的
# Create the subdirectory if it isn't already there
if not os.path.exists(today):
    os.mkdir(today) # make directory
    print ('Successfully created directory', today)
# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command='"H:\WinRAR\Rar.exe" a %s %s'%(target,' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
    print ('Successful backup to', target)
else:
    print ('Backup FAILED')

直接在版本二上改的,漏删版本二的一句话,结果输出文件名一直没变…

版本四

。。。然而并没有什么版本四,不存在的

__init__方法

__init__方法在类的一个对象被建立时,马上运行。这个方法可以用来对你的对象做一些你希望的初始化 。注意,这个名称的开始和结尾都是双下划线。

__del__方法

__del__方法在类的一个对象被消除时,运行,不确定具体时间。这个方法可以用来对你的对象做一些你希望的结尾 。注意,这个名称的开始和结尾都是双下划线。

exec

exec语句用来执行储存在字符串或文件中的Python语句。例如,我们可以在运行时生成一个包含Python代码的字符串,然后使用exec语句执行这些语句。

eval

eval语句用来计算存储在字符串中的有效Python表达式。

写在最后的话

接下来应该是去写一些实战以及翻阅文档吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值