第1章 初识魔法棒:Python入门与基础语法
<!-- 纸书插图1-1:卡通小魔法师举着发光的Python logo魔法棒,旁边堆着写满作业的本子、乱糟糟的书包、滴答响的闹钟,背景是飘着的魔法星星 --> <!-- 电子版交互元素:点击Python logo可跳转至官网下载页 https://www.python.org/downloads/ ,点击闹钟可查看安装步骤动画 --> 周一早上,你慌慌张张收拾书包,发现昨天的数学作业找不到了,语文手抄报也皱巴巴的,还有今天要带的美术工具也忘了装……是不是特别想有个小助手,帮你把东西整理得整整齐齐,还能提醒你什么时候该做什么?今天,我们就来认识这个专属小助手——Python,它就是你要找的「魔法棒」! On Monday morning, you rush to pack your schoolbag, only to find that yesterday's math homework is missing, the Chinese hand-copied newspaper is crumpled, and you even forgot to pack the art tools you need today... Isn't it great to have a little assistant who can organize everything neatly and remind you what to do at what time? Today, we are going to meet this exclusive assistant - Python, which is the "magic wand" you've been looking for!
1.1 什么是Python?它能做什么?
1.1.1 认识你的魔法棒
Python是一种编程语言(Programming Language),你可以把它理解为一种能和电脑“对话”的特殊语言。只要你按照规则写出指令,Python就能帮你让电脑自动完成各种任务,不用你自己动手做重复、麻烦的事。 Python is a programming language, which you can think of as a special language that can "talk" to computers. As long as you write instructions according to the rules, Python can help you make the computer automatically complete various tasks, so you don't have to do repetitive and annoying things yourself. > 💡 小魔法师冷知识:Python的名字不是来自大蟒蛇哦,是来自作者喜欢的英国喜剧团体Monty Python,是不是很有意思? > 💡 Little Wizard Fun Fact: The name Python is not from the big python snake, but from the British comedy group Monty Python that the author likes, isn't it interesting?
<!-- 纸书插图1-2:4格漫画,分别展示Python帮小朋友整理作业文件夹、自动算零花钱、提醒喝水、给班级群发通知的场景 --> <!-- 电子版交互元素:点击每个漫画场景可以播放对应的15秒演示动画 --> 你是不是想问:Python到底能帮我做什么呀?来看看其他小魔法师已经用Python实现的魔法吧: Do you want to ask: What exactly can Python do for me? Let's see what magic other little wizards have already done with Python: 1. 自动整理作业魔法:把你的数学、语文、英语作业文件自动分类放到对应的文件夹里,再也不用在书包里翻半天啦。 Auto Homework Organization Magic: Automatically sort your math, Chinese, and English homework files into corresponding folders, no more rummaging in your schoolbag for half a day. 2. 零花钱记账魔法:输入每天花的钱,Python自动帮你算还剩多少钱,还会告诉你这个星期买零食花了多少,买文具花了多少。 Pocket Money Accounting Magic: Enter the money you spend every day, Python automatically calculates how much is left, and tells you how much you spent on snacks and stationery this week. 3. 提醒小管家魔法:设定好时间,Python会自动弹出提醒,告诉你该喝水了、该做作业了、该给植物浇水了,比闹钟还贴心。 Reminder Butler Magic: Set the time, Python will automatically pop up a reminder to tell you to drink water, do homework, or water plants, which is more considerate than an alarm clock. 4. 班级通知魔法:老师让你给全班同学发通知,不用你一个个发,Python可以自动把通知发给所有同学,还能自动加称呼,不会写错名字。 Class Notification Magic: If the teacher asks you to send a notification to all classmates, you don't need to send it one by one. Python can automatically send the notification to all students, and automatically add titles, so you won't write wrong names.
有没有你心动的魔法?只要学会了Python的基础指令,你也能实现这些魔法哦! Is there any magic you want? As long as you learn the basic instructions of Python, you can also achieve these magic!
1.1.2 核心术语小卡片(中英对照)
| 术语(英文) | 中文释义 | 小例子 | 发音提示 | | --- | --- | --- | --- | | Programming Language | 编程语言:和电脑对话的特殊语言 | Python是一种编程语言 | /ˈprəʊɡræmɪŋ ˈlæŋɡwɪdʒ/ | | Python | Python:我们要学的编程语言,也叫“魔法棒” | 用Python写代码 | /ˈpaɪθən/ | | Code | 代码:用编程语言写出来的指令 | print("你好") 是一行代码 | /kəʊd/ | | Function | 函数:能完成特定任务的代码块 | print() 是打印函数 | /ˈfʌŋkʃn/ |
1.2 安装你的魔法棒
要使用Python,首先要把魔法棒安装到你的电脑里哦。安装过程非常简单,跟着步骤做就可以啦。 To use Python, you first need to install the magic wand into your computer. The installation process is very simple, just follow the steps. ⚠️ 小魔法师安全提醒:下载Python一定要去官方网站(python.org),不要点搜索结果里的「高速下载」「破解版」哦,不然会下到奇怪的东西,就像吃了不认识的糖果会肚子疼一样。 ⚠️ Little Wizard Safety Reminder: Be sure to download Python from the official website (python.org), don't click "high-speed download" or "cracked version" in the search results, otherwise you will download strange things, just like eating unknown candies will cause stomachache.
1.2.1 Windows系统安装步骤
1. 打开浏览器,输入 `python.org` 进入官网,点击「Downloads」下面的「Download Python X.X.X」(X.X.X是最新版本号)。 Open the browser, enter `python.org` to enter the official website, and click "Download Python X.X.X" under "Downloads" (X.X.X is the latest version number). 2. 下载完成后,双击安装包,一定要勾选最下面的「Add Python to PATH」,这个选项会把Python加到电脑的系统路径里,不然你 later 用的时候会找不到。 After the download is complete, double-click the installation package, be sure to check "Add Python to PATH" at the bottom, this option will add Python to the computer's system path, otherwise you won't find it when you use it later. 3. 点击「Install Now」,等待安装完成,然后点击「Close」就OK啦。 Click "Install Now", wait for the installation to complete, and then click "Close" to finish.
<!-- 纸书插图1-3:Windows安装界面的卡通示意图,用红色圈出「Add Python to PATH」的勾选框 --> <!-- 电子版交互元素:点击安装界面图可以放大查看细节 -->
1.2.2 Mac系统安装步骤
1. 同样打开 `python.org` 官网,点击「Downloads」下载Mac版本的安装包。 Open the `python.org` official website, click "Downloads" to download the Mac version of the installation package. 2. 双击下载好的 `.pkg` 文件,按照提示一步步点击「继续」直到安装完成。 Double-click the downloaded `.pkg` file, follow the prompts to click "Continue" step by step until the installation is complete. 3. 安装完成后,打开「终端」(Terminal)应用程序,输入 `python3 --version`,如果显示出版本号(比如 `Python 3.12.0`),就说明安装成功啦。 After the installation is complete, open the "Terminal" application, enter `python3 --version`, if the version number is displayed (such as `Python 3.12.0`), it means the installation is successful.
1.2.3 验证安装成功
不管你是Windows还是Mac,都可以通过下面的方法验证安装有没有成功: No matter if you are using Windows or Mac, you can verify whether the installation is successful by the following method: 1. 按 `Win + R`(Windows)或者打开「终端」(Mac),输入 `python --version`(Windows)或者 `python3 --version`(Mac),按回车。 Press `Win + R` (Windows) or open "Terminal" (Mac), enter `python --version` (Windows) or `python3 --version` (Mac), and press Enter. 2. 如果显示出Python的版本号,比如 `Python 3.12.0`,就说明安装成功啦!如果出现红色错误提示,大概率是没勾选「Add Python to PATH」,重新安装一次就可以啦。 If the Python version number is displayed, such as `Python 3.12.0`, it means the installation is successful! If there is a red error prompt, it is likely that "Add Python to PATH" was not checked, just reinstall it.
1.3 选择你的编程工具
安装好Python之后,我们还需要一个「编程工具」来写代码,就像写字需要笔和本子一样。给大家推荐两个适合小魔法师的工具: After installing Python, we also need a "programming tool" to write code, just like we need a pen and notebook to write. Here are two tools recommended for little wizards: 1. IDLE(自带工具):Python安装完之后会自动自带,不用额外下载,打开就能用,非常适合新手。 IDLE (Built-in Tool): It comes automatically with Python, no need to download it extra, you can open it and use it directly, which is very suitable for novices. 2. Thonny(儿童友好工具):专门为儿童和编程新手设计的工具,界面特别简单,还有 debug(调试)功能,代码写错了会直接告诉你哪里错了,还有中文界面,非常好用。 Thonny (Children-friendly Tool): Specially designed for children and programming novices, the interface is very simple, and it also has a debug function, which will directly tell you where the code is wrong, and it also has a Chinese interface, which is very easy to use.
<!-- 纸书插图1-4:IDLE和Thonny的界面卡通示意图,标注出「新建文件」「运行按钮」的位置 --> <!-- 电子版交互元素:点击工具名称可以跳转到对应的下载页面 --> 如果你不想在电脑上装软件,也可以用在线工具 Replit,打开浏览器就能写代码,不用安装任何东西,特别方便。 If you don't want to install software on your computer, you can also use the online tool Replit, you can write code by opening the browser, no need to install anything, which is very convenient.
1.4 Python基础语法入门
现在我们已经拿到了魔法棒,接下来就要学习基础的魔法指令啦!只要学会这几个指令,你就能写出第一个小程序哦。 Now that we have the magic wand, let's learn the basic magic instructions! As long as you learn these instructions, you can write your first small program.
1.4.1 变量:带标签的存储盒子
变量(Variable)就像是你书包里的格子,每个格子都有名字,里面可以放课本、文具、零食,需要的时候拿出来用。在Python里,我们可以给变量起名字,然后把数据放进去。 A variable is like a compartment in your schoolbag, each with a name, where you can store textbooks, stationery, or snacks, and take it out when needed. In Python, we can name variables and put data in them.
变量的使用方法
写变量的格式是:`变量名 = 要存储的内容` The format of writing a variable is: `variable name = content to be stored` 比如: For example: ```python #