美文网首页大数据 爬虫Python AI Sql
Python 小白的吸星大法 !

Python 小白的吸星大法 !

作者: 14e61d025165 | 来源:发表于2019-07-27 14:55 被阅读7次

    在学习 Python 过程中,尤其是小白入门阶段,会遇到很多"别人家"光鲜的标签,今天我们来看两个常见的。

    # 零基础

    说"零基础"的案例真的比比皆是,本来 Python 编程掌握的不错,一说零基础学的,瞬间就吸引一众小白目光,尤其是再配上转行、跨专业等背景。

    要知道,别人的零基础,可能只是单纯未接触过 Python 这一门语言,但其他编程语言是有涉猎的。即使是真正彻头彻尾基础开始,他在背后付出的努力你是看不到的。所以,要正视"零基础"这个标签,它既不能说明 Python 学起来简单,也不能证明谁的强弱。

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1564210490045 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    Python资源共享群:484031800

    对于零基础呢,首先明确每个新学 Python 的人都是零基础起步的,说这些是没有意义的。学好 Python 并不在于你的基础起点如何,而在于知识的储备、方法的应用和练习的积累。

    # 快速入门

    一般紧跟"零基础"的就是"快速入门"这个标签了。随着 Python 这几年越来越受欢迎,培训、推广 Python 学习的文案随处可见。配合着零基础的起点,再加上最终快速入门实现了升职加薪的效果,这不就是 Python 培训机构最好的广告么?

    Python 的简单是通过与其他编程语言比较出来的,不是通过学习时间得出来的。资深的程序员入门 Python 可以是几周甚至几天,而纯小白几个月甚至几年可能还稀里糊涂,不同层次的横向比较是没有意义的。

    快速入门这一点可以拿来纵向督促自己:如果做得到,别自我陶醉,要继续加深学习;如果觉得入门很难,要摆正心态、稳扎稳打。对自己的基础和能力有大致掌握后,合理规划和高效练习,提高自己学习的质量。

    02

    结合我自己练习Python的经验,向小白朋友们推荐个学习方法:有想法需求时,搜现成的代码来消化,自己进行整合改造。听着颇有几分吸别人功力为己所用的味道,故自封"吸星大法"。

    吸星大法

    每个秘籍都有自己的前提,比如葵花宝典的"欲练此功,必先**"。咱这个没那么严苛,但是一定要学会去高效地搜索。

    很多很多小白是不会搜索问题的,宁愿花大把时间去群里、去论坛提问题,也不通过搜索引擎去直接寻找答案,这是大忌。同时,由于确实是小白,提问时不能掌握问题要点,说半天别人也没懂你到底想干嘛,大把时间精力全都浪费掉了。

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1564210490050 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    搜到合适的参考资料,要结合着别人的讲解来研读其代码,看懂之后自己进行整合改造。首先尝试模仿学习,消化个大概然后自己主动用代码去实现。

    整个流程下来,我们自行搜索筛选结果,研读别人完整代码,模仿修改整合形成自己新代码,吸星大法也就练成了。

    案例演练

    正好前几天有朋友问到用 Python 实现计算器,我们就拿这个问题来演练下吸星大法。

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1564210490056" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image> <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1564210490058" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    首先是搜索,需求其实是用 Python 写图形界面的计算器,搜索时要体现:

    • 搜索出的结果可能前几项是广告,直接忽略掉;
    • 在选择要参考的帖子时,首先根据发帖时间优先选择近期发布的最好附带源码和说明的,进入帖子后顺便看一眼评论区有没有反馈报错,如果有的话也不要浪费时间去看了;
    • 当有几篇质量不错的进入视野时,尽可能选代码简洁又说明详细地来参考。

    另外选择资源时要结合个人需求,因为我之前尝试过 tkinter 来写图形界面,这次打算借此机会学习下 Qt 的图形界面编程,所以选择了一篇 PyQt5 实现计算器的文章来学习。

    https://blog.csdn.net/LindaMan_/article/details/81777543

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1564210490064" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image> <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1564210490067" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    文章如截图,在 csdn 博客上贴出了代码并附了较为详细地说明,也贴出了其 GitHub 代码链接。这里小白要注意了,有机会的话了解下GitHub上如何查看下载代码,尝试将自己的代码上传GitHub进行分享来练习。参考文章的代码有200多行,开头一堆 import 完全看不懂,特别唬人。不着急,我们都来看看尝试去理解吸收。

    首先是开头的 import 区域:

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import sys
    from PyQt5.QtWidgets import (QMainWindow, QWidget, QApplication, QLineEdit, QPushButton, QGridLayout)
    from PyQt5.QtGui import QRegExpValidator
    from PyQt5.QtCore import Qt, QRegExp
    </pre>

    第一个 import sys, 如果之前没有接触过,可以搜索到介绍 sys 模块包含了与 Python 解释器和它的环境有关的函数。后面连续三个 PyQt5 相关的 from import 很明显是我们要用来构造图形界面的模块了,具体的我们先不深究,继续看代码。

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">class Calculator(QWidget):
    def init(self):
    def ui(self):
    def clear_line_edit(self):
    def deal_num_btn(self, sender_text):
    def deal_operator_btn(self, sender_text):
    def deal_point_btn(self):
    def deal_equal_btn(self):
    def show_msg(self):
    def auxiliary_calculate(self, first_num, second_num, operator: str):
    def calculate(self, operator='='):
    </pre>

    接下来很长的近两百行代码可以整理出以上结构,其实就是定义了一个类,声明了各种函数,我们也可以想到之后代码会将这个类实例化一个计算器对象运行起来,以实现计算器的种种功能。

    最后四行代码,是代码实际执行的命令:

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">if name == 'main':
    # 所有的PyQt5应用必须创建一个应用(Application)对象。sys.argv参数是一个来自命令行的参数列表
    app = QApplication(sys.argv)
    # cal就是我们要建立的计算器,它是Calculator类的一个实例
    cal = Calculator()
    # 计算器退出相关的
    sys.exit(app.exec_())
    </pre>

    说实话,在我过了一遍基础时,并不知道 name 这个用法,要执行哪些代码就直接写出来运行。该用法主要用来让脚本模块既可以导入到别的模块中用,同时此脚本模块呢也可以自己执行。即当这份 py 被其它模块导入时,name 就不是 “main”,其后的代码不会被执行;而当该py被执行时,会触发该条件直接运行其代码。

    实际执行的代码,乍一看很复杂看不懂,分析加搜索,便能看出大概。既然是要用 PyQt5 来做个图形界面应用,那么就要按它的格式来组建出其样式和功能。也如我们料想,会对 Calculator 类进行实例化,那么计算器的功能也是在上方一长串类的定义里都准备好了。

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># QWidget是Cal的父类
    class Calculator(QWidget):
    """
    计算器的基本页面的基本界面, 完成基本的计算
    """
    # init创建实例时传入的参数,参考链接 https://www.cnblogs.com/illusion1010/p/9527034.html
    def init(self):
    #super() 函数是用于调用父类(超类)的一个方法
    #参考链接 https://www.runoob.com/python/python-func-super.html
    super(Calculator, self).init()
    #这里先定义ui()函数,下面会具体定义
    self.ui()
    self.char_stack = [] # 操作符号的栈
    self.num_stack = [] # 操作数的栈
    self.nums = [chr(i) for i in range(48, 58)] # 用于判断按钮的值是不是数字 chr(48)-chr(57)对应数字0-9
    self.operators = ['+', '-', '', '/','x^y'] # 用于判断按钮的值是不是操作符
    self.empty_flag = True # 这个flag的含义是来判断计算器是不是第一次启动,在显示屏幕中无数据
    self.after_operator = False # 看了计算器的计算,比如1+2在输入+后,1还显示在屏幕上,输入了2之后,1就被替换了, 这个flag的作用就是这样的
    self.char_top = '' # 保留栈顶的操作符号
    self.num_top = 0 # 保留栈顶的数值
    self.res = 0 # 保留计算结果,看计算器计算一次后,在继续按等号,还会重复最近一次的计算1+2,得到3之后,在按等号就是3+2, 以此类推.
    # >先计算, 为什么同样的符号改成了后计算, 是为了方便做一项操作,
    # 就是在你计算一个表达式之后,在继续按住等号, 以及会执行最后一次的符号运算
    self.priority_map = {
    '++': '>', '+-': '>', '-+': '>', '--': '>',
    '+
    ': '<', '+/': '<', '-': '<', '-/': '<',
    '
    ': '>', '//': '>', '+': '>', '/+': '>',
    '-': '>', '/-': '>', '/': '>', '/*': '>'
    }
    </pre>

    这一段还是有些小白级别的难点,例如 class Calculator(QWidget) 是继承父类的形式,init 中 super() 的调用等。这一段是为计算器所需要的变量和功能都进行统一的定义和初始赋值。

    其后 ui() 相关代码对计算器图形界面的样式布局进行详细定义,并为界面中的按钮绑定上点击触发的事件。即当我们点击计算器数字按钮或运算符时,会在显示框内显示该数字或运算符;当点击“=”时,会进行系列运算将结果输出。

    代码下载

    按这个顺序过一遍源码,就对代码整体的思路和结构有了一定掌握。假设我们要为其增加功能,只需在添加按钮或更改布局的函数内进行相关修改,再将绑定的点击事件妥善处理,一个由我们优化的计算器就诞生了。

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1564210490085" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image> <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1564210490088" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    我在代码基础上添加了简单的指数计算和平方运算,如果你对代码有兴趣,可以移步留言区 获取源码下载链接,代码中加了随行注释以及部分参考链接~

    相关文章

      网友评论

        本文标题:Python 小白的吸星大法 !

        本文链接:https://www.haomeiwen.com/subject/pgkerctx.html