1

怎么自学python,大概要多久?

 2 years ago
source link: https://zhuanlan.zhihu.com/p/370241105
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

怎么自学python,大概要多久?

公号:Python大数据分析,关注进学习群

学编程语言有个小秘诀,直接上项目就是干,做完后你就是pythoner了。

不要怕没有基础,边做边查边学,进步很快的。

因人而异,一周或者一个月就能搞定。当然需要足够的投入。

以前我也觉得收集资料、啃语法、敲代码是学python的套路,但这样学效率太低。

你要知道python是一门脚本语言,不需要传统的编写-编译-链接-运行过程,语法简答、执行方便。

也就是说python像是个瑞士军刀,可以写出很多有用的小工具,随写随用。

我在python专栏里写了很多python应用案例,其实大多是对新手友好的。


下面介绍适合新手的python小项目:

贪吃蛇小游戏

用100行python代码写个贪吃蛇小游戏,也不复杂但涵盖了大部分python语法。

v2-43b7949b7911baf17baecff37f431ef3_720w.jpgv2-35b93af11b2d0ad80139dc5d63593d39_720w.jpg

项目地址:https://gitee.com/codetimer/Snake/blob/master/main.py

可以尝试着先复制代码运行一遍,然后自己写。

人脸识别

调用开源项目,只需要简单的几十行python代码,就可以实现人脸识别。

从图片里找到人脸:

识别人脸关键点

实时人脸检测

项目地址:https://github.com/ageitgey/fac

中文分词&情感分析

这个也比较有意思,可以爬取电商评论数据,然后分词处理,并做情感分析,判断好评、差评。

jieba可以用来做分词处理

https://github.com/fxsjy/jieba

snownlp可以用来做情感分析。

import snownlp
sentense = '''亲,第一天秒杀买,比第二天的正常价还高,
说保价7天申请售后说退差价也比不退,你们还有信誉吗
            '''
result = snownlp.SnowNLP(sentense)
a = result.words  # list
b = result.sentiments  # float
print("%.2f" % b)

项目地址:

isnowfy/snownlp

车型识别

这里使用python调用百度的车型识别模型,只要导入车辆图片可以自动识别车型。

import requests
import base64
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# 输入你的api_key和secret_key,获取地址https://console.bce.baidu.com/ai
api_key = ''
secret_key = ''
url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + str(api_key) + '&client_secret=' + str(secret_key)
res = requests.get(url).text
a = eval(res)
access_token = a['access_token']
animal = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/car?access_token=' + str(access_token)
header = {
    'Content-Type':'application/x-www-form-urlencoded'
}
data = {}
with open('timg.jpg', 'rb') as f:
    image = base64.b64encode(f.read())
    data["image"] = str(image, 'utf-8')
    res2 = requests.post(url=animal,data=data, headers=header).text
    print('颜色:',eval(res2)['color_result'])
    print('车型预测')
    for each in eval(res2)['result']:
        print(each['name'], '\t相似度:', each['score'])
    plt.imshow(mpimg.imread(f))
plt.show()

用Python实现所有常见算法

这个项目包含了上千个算法的Python代码实现,几乎囊括了大部分常见算法。

包括回溯、布尔代数、元胞自动机、线性回归、图算法、网络流等等

以排序为例,该项目提供了近50种算法,比如下面的树形选择排序:

"""
Tree_sort algorithm.
Build a BST and in order traverse.
"""


class node:
    # BST data structure
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

    def insert(self, val):
        if self.val:
            if val < self.val:
                if self.left is None:
                    self.left = node(val)
                else:
                    self.left.insert(val)
            elif val > self.val:
                if self.right is None:
                    self.right = node(val)
                else:
                    self.right.insert(val)
        else:
            self.val = val


def inorder(root, res):
    # Recursive traversal
    if root:
        inorder(root.left, res)
        res.append(root.val)
        inorder(root.right, res)


def tree_sort(arr):
    # Build BST
    if len(arr) == 0:
        return arr
    root = node(arr[0])
    for i in range(1, len(arr)):
        root.insert(arr[i])
    # Traverse BST in order.
    res = []
    inorder(root, res)
    return res


if __name__ == "__main__":
    print(tree_sort([10, 1, 3, 2, 9, 14, 13]))
© 2021 GitHub, Inc.

其他排序:

项目地址:TheAlgorithms/Python

其他还有很多可以实操的小例子


如果想看书学习,之前我也写过一些推荐


基础语法看教程确实够了,但有的人会觉得枯燥,学东西还得有趣不是。

那就推荐去B站找找视频,搜python,排名靠前的教程都还不错。

有人在问买什么书看,我一向都是看网上教程的,新手非要看书的话,是有那么两三本。

零基础可以看python编程从入门到实践,书后面有不错的案例。

还有一本是注重实践、解决问题的书,叫作python让繁琐工作自动化,适合有点基础的选手。该书是从爬虫、自动化表格、邮件收发、桌面控制等角度来写的,比较贴合日常工作场景。

还有一本比较好的是python cookbook,是本工具书,而非语法书。

工具书当然是为了解决问题,所以cookbook的风格就是对症下药,先提问题再讲方法。

这本书不太适合小白看,因为里面概念比较多。

如果你英文好的话,当然首选看英文版,表达更精确。

https://ipython-books.github.io/

也有中文版,看起来不费劲。

https://python3-cookbook.readthedocs.io/zh_CN/latest/index.html

纸质也出版了。

v2-05e708c4cfdf3aef525077c4ea2bbd20_720w.jpg

好了,说的有点多。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK