1 Star 4 Fork 2

tonybearpan/30-Days-Of-Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MulanPSL-2.0

🐍 Python 30 天

# 日期 标题
第一天 介绍
02 变量和内置方法
03 运算符
04 字符串
05 列表
06 元组
07 集合
08 字典
09 条件
10 循环
11 方法
12 模块
13 列表推导式
14 高阶函数
15 类型错误
16 日期时间
17 异常处理
18 正则表达式
19 文件处理
20 包管理
21 类和对象
22 爬虫
23 虚拟环境
24 统计学
25 Pandas
26 网络编程
27 MongoDB
28 接口
29 构建接口
30 结论

🧡🧡🧡 HAPPY CODING 🧡🧡🧡

支持 作者 去创造更多的教材
Paypal Logo

Python30天: 第一天 - 介绍

Twitter Follow

作者: Asabeneh Yetayeh
第二版: 2021年7月

译者: Tonybearpan
2021年11月

第2天 >>

30DaysOfPython

📘 第1天

欢迎

恭喜你决定参与这个Python30天编程的挑战。在这个挑战中你会学习到座位一个python开发者所需要的所有技能和完整的开发理念。 在这个挑战的最后你会过的一个_Python30天_编程挑战证书。

如果你想积极参与到这个挑战中来,你可以参入 Python30天挑战 的telegram 组.

介绍

Python是一个高级通用编程语言。它是开源的解释性面向对象编程语言。 Python的创始人为荷兰人吉多·范·罗苏姆(Guido van Rossum)。 编程语言Python的名字是取自英国20世纪70年代首播的电视喜剧《蒙提·派森的飞行马戏团》(Monty Python's Flying Circus)。 第一个公开发行版是在1991年2月20日。这个Python30天的挑战将帮助你逐步学习最新版本的Python,Python3。所有知识点被分到30天,每一天的内容都用浅显易懂的方式和实例来介绍,供大家实际练习。

这个挑战是给想学习python编程语言的初学者和从业者设计的。 可能需要30到100天去完成这个挑战,积极参与到telegram组的朋友完成这个挑战的概率会比较高。如果你喜欢通过视频学习,可以从这个视频开始。给绝对初学者的Python视频.

为什么用 Python ?

Python是一门与自然语言非常接近的编程语言,因此它非常荣誉学习。 Python被很多机构和公司(包括谷歌)使用。 它可以开发网站、桌面端程序、系统管理后台以及机器学习库。 Python在数据科学和机器学习社区非常受欢迎。我希望这些可以说服你开始学习Python。巨蟒(Python)正在吞食世界,而你将在它吃掉你之前杀死它。

环境搭建

安装 Python

要运行Python脚本你需要先安装Python。快来 下载 python。 如果你是windows用户,点击红框内的链接。

installing on Windows

如果你是macOS用户. 点击红框内的链接。

installing on Mac

检查python是否安装成功,在你的命令行终端输入下面的命令。

python --version

Python Version

你可以在终端看到_Python 3.7.5_版本号,你的版本号可能与我的不同但是应该是3.6或者更高版本。 如果你已经看到了python版本号,干的很棒。Python已经安装到你的电脑上,继续下面的内容。

Python终端

Python是一门解释型语言,所以它不需要编译。这意味着它可以逐行的执行。Python自带一个Python Shell(Python终端)。它可以直接执行python命令并且输出结果。

Python shell等待着用户输入Python代码。当你输入代码的时候,他会解释代码并在下一行输出结果。打开你的终端或者命令行程序,输入:

python

Python Scripting Shell

Python终端启动,并且等待你输入Python代码(Python脚本)。你可以在 >>> 符号后面输入你的Python脚本,然后敲击回车键。我们在python脚本程序中输入第一个脚本。

Python script on Python shell

干的漂亮,你在Python终端中写了你的第一段Python脚本。那么,如何关掉Python终端呢? 要关掉程序,在 >>> 符号后面输入**exit()**命令,然后敲击回车键。

Exit from python shell

现在你知道如何打开Python终端并且关闭它了。

如果你写了Python可以理解的脚本,Python会给你一个结果,否则,会报错。我们故意制造一个错误,看看Python会返回什么。

Invalid Syntax Error

正如你看到的,报错了。不过Python很聪明,他知道我们制造了一个语法错误: invalid syntax。 在Python里用 x 做乘法运算是个语法错误,因为(x)在Python里不是一个有效的语法。我们用(*)代理(x)做乘法运算。报错明确了需要修复的内容。

在程序里定位并且解决一个错误的过程叫做 debugging。我们通过输入 * 代替 x 来debug这个问题。

Fixing Syntax Error

我们的bug解决了,代码可以执行并且返回我们期望的结果。作为一个程序员你基本上每天都会看到这样的错误。知道如何debug很重要。为了更好的debugging,你需要理解你将要面对那些错误。你可能会遭遇很多Python的错误,例如语法错误-SyntaxError, 索引错误-IndexError, 命名错误-NameError, 找不到模块错误-ModuleNotFoundError, 键错误-KeyError, 引用错误-ImportError, 参数错误-AttributeError, 类型错误-TypeError, 值错误-ValueError, 除数是0错误-ZeroDivisionError等等。我们会在以后的内容看到各种不同的Python**错误类型**。

让我们联系如何使用Python终端,打开你的终端或者命令行程序CMD,然乎输入单词 python

Python Scripting Shell

Python终端打开,我们做一些基本的数学运算。(加减乘除,取模和幂运算)。

我们在写其他Python代码之前,先做一些数学运算:

  • 2 + 3 = 5
  • 3 - 2 = 1
  • 3 * 2 = 6
  • 3 / 2 = 1.5
  • 3 ^ 2 = 3 x 3 = 9

在python里我们有额外的一些运算:

  • 3 % 2 = 1 => 取余数
  • 3 // 2 = 1 => 除法向下取整

让我门把上面的书写公式变成Python代码。在Python程序的最开始写上备注。

_备注_是代码的一部分,但是不会被执行,所以我们可以在我们的代码里面写一些文本让代码的可读性更好一些。Python不执行备注部分,备注以“#”开始。这就是如何在python里写一个备注。

 # comment starts with hash
 # this is a python comment, because it starts with a (#) symbol

Maths on python shell

在我们进入下一部分之前,在Python终端里面在练习一下。 通过在程序里面输入 exit() 关闭程序,然后再打开,我们练习一下如何在程序里面输入文本。

Writing String on python shell

安装 Visual Studio Code

Python终端适合测试小的脚本代码,但是对于大的工程却并不适用。在真实的工作环境中,开发者会使用不同的代码编辑器取写代码。在这个Python30天变成的挑战中,我们会使用Visual Studio Code。Visual Studio Code是一个非常流行的开源代码编辑器。我比较喜欢使用vscode,所以我会推荐大家下载 visual studio code,不过如果你喜欢其他的编辑器,也可以去使用你所喜欢的。

Visual Studio Code

如果你已经安装了visual studio code,我们看看如何去使用它。如果你需要一个视频,你可以看一下这个在Visual Studio Code上使用 Python的 视频教程

如何使用 visual studio code

打开visual studio code,看到这个界面,根据表示点击界面。

Visual studio Code

在不的桌面上创建一个文件夹30DaysOfPython。然后使用visual studio code打开它。

使用Visual studio打开工程

打开一个工程

打开以后你可以看到创建文件和目录的图标。正如你看到的,我已经创建了第一个文件 helloworld.py。你也试试。

创建一个 python 文件

在你写了一天的代码以后,你想关闭编辑器了,你可以这样操作。

关闭工程

恭喜你已经完成了开发环境的设置。我们开始写代码吧。

基础 Python

Python 语法

一个python脚本可以在python终端中写,也可以在代码编辑器里面写。Python文件的扩展名是 .py。

Python 缩进

一个缩进是一个空格。在和努懂开发语言中缩进是为了提高可读性,但是Python用缩进创建代码块。在其他语言里面,花括号用来创建代码块,而不是缩进。缩进错误是一个常见的python代码bug。

缩进错误

备注

备注对于让代码可读性非常重要。Python不执行备注部分的代码。 Python中任何以 #开头的代码都是备注。

示例: 单行备注

    # This is the first comment
    # This is the second comment
    # Python is eating the world

示例: 多行备注

当不赋值给变量时,3个双引号可以用于多行备注。

"""This is multiline comment
multiline comment takes multiple lines.
python is eating the world
"""

数据类型

Python有很多数据类型,我们从最常用的几个开始。不同的数据类型的详细介绍在其他章节会介绍道。在一开始,我们先熟悉一下数据类型,不需要非常清晰的去理解。

数字

  • Integer: 整型(负数,零 和 正数) 示例: ... -3, -2, -1, 0, 1, 2, 3 ...
  • Float: 小数 示例 ... -3.5, -2.25, -1.0, 0.0, 1.1, 2.2, 3.5 ...
  • 复数 示例 1 + j, 2 + 4j

字符串

通过单括号或者双括号括起来的一个或者多个字母集合。如果字符串超过一句话,我们用三括号括起来。

示例:

'Asabeneh'
'Finland'
'Python'
'I love teaching'
'I hope you are enjoying the first day of 30DaysOfPython Challenge'

布尔

布尔型boolean是 True 或者 False,T 和 F 必须是大写。

示例:

    True  #  Is the light on? If it is on, then the value is True
    False # Is the light on? If it is off, then the value is False

列表

Python List 是有序的,可以包含不同数据类型的元素的集合。List和JavaScript的 array类似。

示例:

[0, 1, 2, 3, 4, 5]  # all are the same data types - a list of numbers
['Banana', 'Orange', 'Mango', 'Avocado'] # all the same data types - a list of strings (fruits)
['Finland','Estonia', 'Sweden','Norway'] # all the same data types - a list of strings (countries)
['Banana', 10, False, 9.81] # different data types in the list - string, integer, boolean and float

字典

Python字典dictionary对象是无需的键值对合适的数据集合。

示例:

{
'first_name':'Asabeneh',
'last_name':'Yetayeh',
'country':'Finland', 
'age':250, 
'is_married':True,
'skills':['JS', 'React', 'Node', 'Python']
}

元组

元组tuple是与列表list相似的有序的数据集合,一旦创建无法修改。

示例:

('Asabeneh', 'Pawel', 'Brook', 'Abraham', 'Lidiya') # Names
('Earth', 'Jupiter', 'Neptune', 'Mars', 'Venus', 'Saturn', 'Uranus', 'Mercury') # planets

集合

集合set与列表list和元组tuple类似。但是与列表和元组不同的是,集合是无序的的数据集合。跟数学中的很像,Python的集合只存储不重复的元素。

在后面的部分,我们会更深入的了解Python的数据类型。

示例:

{2, 4, 3, 5}
{3.14, 9.81, 2.7} # order is not important in set

检查数据类型

通过 type 方法查看一个数据或者变量的数据类型。在下面的终端中你会看到不同的数据类型:

检查数据类型

Python 文件

首先打开你的项目文件夹30DaysOfPython。如果你没有这个文件夹,创建一个文件夹命名为30DaysOfPython。在这个文件夹里,创建一个文件命名为helloworld.py。现在我们用visual studio code去做之前在python终端中做的事情。

Python终端不用 print 就可以打印,但是在visual studio code中要看到输出内容需要使用内置方法 print() 。内置方法print() 可以接受一个或者多个参数 print(‘arument1', 'argument2', 'argument3’)。看一下下面的示例。

示例:

文件名是 helloworld.py

# 第1天 - 30DaysOfPython Challenge

print(2 + 3)             # addition(+)
print(3 - 1)             # subtraction(-)
print(2 * 3)             # multiplication(*)
print(3 / 2)             # division(/)
print(3 ** 2)            # exponential(**)
print(3 % 2)             # modulus(%)
print(3 // 2)            # Floor division operator(//)

# Checking data types
print(type(10))          # Int
print(type(3.14))        # Float
print(type(1 + 3j))      # Complex number
print(type('Asabeneh'))  # String
print(type([1, 2, 3]))   # List
print(type({'name':'Asabeneh'})) # Dictionary
print(type({9.8, 3.14, 2.7}))    # Set
print(type((9.8, 3.14, 2.7)))    # Tuple

要执行python文件,看一下下面的图片.你可以通过点击绿色按钮或者在终端中输入python helloworld.py来执行python文件。

执行python脚本

🌕 你很棒,你刚刚完成了第1天的挑战并且走在了通向成功的路上。现在我们活动一下你的大脑和肌肉。

💻 习题-第1天

习题:1

  1. 查看你使用的python版本
  2. 打开python终端然后执行下面的操作习题,执行的对象是3和4。
    • 加法(+)
    • 减法(-)
    • 乘法(*)
    • 取余数(%)
    • 除法(/)
    • 幂运算(**)
    • 向下去整除(//)
  3. 在python终端中写以下字符串:
    • 你的名字
    • 你的姓
    • 你的国家
    • 我很喜欢python30天
  4. 检查以下数据的数据类型:
    • 10
    • 9.8
    • 3.14
    • 4 - 4j
    • ['Asabeneh', 'Python', 'Finland']
    • 你的名字
    • 你的姓
    • 你的国家

习题:2

  1. 在30DaysOfPython文件夹中创建一个文件夹命名为day_1。在day_1文件夹中创建文件helloworld.py。然后重复上面的习题1,2,3,4。在python文件上工作时记得使用 print() 。定位到你保存文件的目录,然后去执行它。

习题:3

  1. 写个使用不同数据类型的例子 例如 Number(Integer, Float, Complex), String, Boolean, List, Tuple, Set , Dictionary.
  2. 计算2个点(2, 3) 和 (10, 8)之间的 欧氏距离

🎉 恭喜 ! 🎉

第2天 >>

木兰宽松许可证, 第2版 木兰宽松许可证, 第2版 2020年1月 http://license.coscl.org.cn/MulanPSL2 您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束: 0. 定义 “软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。 “贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。 “贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。 “法人实体”是指提交贡献的机构及其“关联实体”。 “关联实体”是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。 1. 授予版权许可 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。 2. 授予专利许可 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。 3. 无商标许可 “本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。 4. 分发限制 您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。 5. 免责声明与责任限制 “软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。 6. 语言 “本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。 条款结束 如何将木兰宽松许可证,第2版,应用到您的软件 如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步: 1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字; 2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中; 3, 请将如下声明文本放入每个源文件的头部注释中。 Copyright (c) [Year] [name of copyright holder] [Software Name] is licensed under Mulan PSL v2. You can use this software according to the terms and conditions of the Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. Mulan Permissive Software License,Version 2 Mulan Permissive Software License,Version 2 (Mulan PSL v2) January 2020 http://license.coscl.org.cn/MulanPSL2 Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions: 0. Definition Software means the program and related documents which are licensed under this License and comprise all Contribution(s). Contribution means the copyrightable work licensed by a particular Contributor under this License. Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. Legal Entity means the entity making a Contribution and all its Affiliates. Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. 1. Grant of Copyright License Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. 2. Grant of Patent License Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. 3. No Trademark License No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4. 4. Distribution Restriction You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. 5. Disclaimer of Warranty and Limitation of Liability THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 6. Language THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL. END OF THE TERMS AND CONDITIONS How to Apply the Mulan Permissive Software License,Version 2 (Mulan PSL v2) to Your Software To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps: i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner; ii Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package; iii Attach the statement to the appropriate annotated syntax at the beginning of each source file. Copyright (c) [Year] [name of copyright holder] [Software Name] is licensed under Mulan PSL v2. You can use this software according to the terms and conditions of the Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details.

简介

来自github的Python30天翻译 展开 收起
Python 等 3 种语言
MulanPSL-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/tonybearpan/Thirty-Days-Of-Python.git
git@gitee.com:tonybearpan/Thirty-Days-Of-Python.git
tonybearpan
Thirty-Days-Of-Python
30-Days-Of-Python
master

搜索帮助