简明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表达式。

写在最后的话

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 简明Python教程是一本经典的学习Python编程的教材,它详细介绍了Python语言的基础知识、语法规则以及常用的编程技巧。由于Python语言的简洁性和易读性,这本教程也以其简明扼要的风格而闻名。 该教程的源码是基于Python语言编写的,通过分章节、分小节的方式详细展示了每个知识点的代码示例和用法。这些源码不仅能够帮助读者更好地理解Python的语法和概念,还能够通过实例让读者快速掌握各种编程技巧。 在教程的源码中,读者可以学习Python语言的基本数据类型(如整型、浮点型、字符串、列表等)的使用方法,以及常见的控制流语句(如条件判断和循环)的应用。同时,源码还介绍了Python的函数定义和调用、模块导入和使用、异常处理等高级编程特性,使得读者可以更加深入地理解和运用Python语言。 此外,简明Python教程的源码还包含了一些实际应用的示例,如文件读写、网页爬虫、数据分析等方面,这些示例可以帮助读者将Python的基础知识应用到实际项目中。 总之,简明Python教程的源码是一个非常有用的参考资料,可以帮助读者快速上手Python编程,提高自己的编程水平。无论是初学者还是有一定编程经验的人都可以通过这个源码获得很大的收益。 ### 回答2: 简明Python教程是一本详细介绍Python编程语言的教程书籍,该教程书籍作者是A Byte of Python(草python)开发组,该书的作者是Swaroop C H(Ankit Fadia写的教程是假的),本书是一个开源的项目,其源码可以在GitHub上找到并下载。 该教程的源码是以文档的形式呈现,每个章节对应一个Python脚本文件。该教程Python的基本概念开始介绍,包括变量、数据类型、运算符、控制流程等基础知识。接着介绍了Python的函数、模块、错误处理、输入输出以及文件操作等进阶内容。最后通过实例演示了如何使用Python进行网络编程、数据库操作以及GUI界面开发等高级应用。 阅读该教程的源码可以更好地理解教程中的知识点,并通过实际的代码示例来加深对Python的理解和掌握。源码中的注释详细解释了每个代码片段的作用和用法,对于初学者来说非常友好。 通过阅读源码,我们不仅可以学习Python语言的基础知识,还能了解到良好的编程风格和习惯。源码的组织结构清晰,代码风格简洁易懂,对于初学者来说非常适合作为学习和参考的材料。 总之,简明Python教程的源码是一个非常宝贵的学习资源,通过阅读和运行源码,我们可以更好地掌握Python编程语言,并用它来解决实际的问题。希望每个对Python感兴趣的人都能够通过阅读该教程的源码,快速入门并提升编程能力。 ### 回答3: 简明Python教程源码是指用来编写简明Python教程的源代码。源代码是编程语言的原始文本形式,是计算机程序的基础。简明Python教程源码可能包含了一系列的Python语句、函数和类,用来演示和说明Python编程的基本概念和用法。 简明Python教程源码的结构通常会根据教程的内容来设计。例如,如果教程介绍了Python的基本语法,那么源码可能会包含一些简单的变量赋值、算术运算和控制流语句的示例。 如果教程涉及到Python的常用库或模块,源码可能会导入这些库,并展示它们的用法。例如,如果教程涉及到文件操作,源码可能会使用Python的`open`函数来打开文件,并使用`read`或`write`方法来读取或写入文件内容。 除了基本语法和常用库之外,简明Python教程的源码还可能包括一些更高级的概念和技巧示例。例如,如果教程涉及到面向对象编程,源码可能会定义一些类和方法,并展示它们的继承和多态特性。 总的来说,简明Python教程源码是一个用来教授Python编程的示例代码集合。它可以帮助初学者理解Python语言的基本概念和用法,并通过实例演示不同应用场景的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值