初识Python魔法:你的第一个自动化小工具
你有没有过这样的经历:放学要整理20本课本、3本练习册还有文具盒,每次都要花10分钟,要是能有个小魔法帮我们把课本按课表自动摆进书包就好了?/ Have you ever had this experience: after school, you need to organize 20 textbooks, 3 workbooks and a pencil case, which takes 10 minutes every time. It would be great if there was a little magic to automatically put the textbooks into the schoolbag according to the timetable? 很多喜欢看《哈利波特》的小朋友都幻想过,要是自己也会魔法就好了:挥挥魔杖就能让书包自己整理好,念个咒语就能把手抄报的边框画得整整齐齐,甚至能让电脑帮自己统计图书角的借阅记录。其实啊,我们现实世界里真的有这样的「魔法棒」,它就是今天我们要认识的新朋友——Python。/ Many kids who like to watch "Harry Potter" have ever fantasized that it would be great if they could do magic: wave the wand to make the schoolbag organize itself, recite a spell to draw the border of the hand-copied newspaper neatly, and even let the computer help you count the borrowing records of the book corner. In fact, there is really such a "magic wand" in our real world, which is our new friend we are going to meet today——Python.
1.1 什么是Python?/ What is Python?
Python 这个名字其实不是指爬行动物蟒蛇哦,它的名字来自创作者们喜欢的英国喜剧团体「Monty Python」,是个超有活力的编程小工具~/ Actually, the name "Python" doesn't refer to the reptile python. Its name comes from "Monty Python", a British comedy group that the creators love. It's a super energetic programming tool. 简单来说,Python 就是一种人和电脑能互相听懂的语言/ Simply put, Python is a language that both humans and computers can understand。你只要按照Python的规则写出一行行指令,电脑就会乖乖按照你说的去做,帮你完成各种重复的、麻烦的任务,就像你给机器人写了一个操作手册,它就会按手册帮你干活啦~/ You just write lines of instructions according to the rules of Python, and the computer will obey what you say and help you complete all kinds of repetitive and troublesome tasks, just like you write an operation manual for the robot, and it will work for you according to the manual.
Python能做什么?/ What can Python do?
Python的能力超强,不管是大人还是小朋友都能用它做很多有意思的事,尤其是帮你搞定学习和生活里的各种小麻烦:/ Python's ability is super strong. Both adults and children can use it to do many interesting things, especially to help you deal with all kinds of small troubles in study and life:
- 自动整理你收集的奥特曼卡片、贴纸,按稀有度排序 / Automatically organize the Ultraman cards and stickers you collect and sort them by rarity
- 给小组作业的每个同学自动发提醒消息,不用你挨个敲字 / Automatically send reminder messages to every classmate of the group homework, you don't need to knock on each one
- 统计班费的使用情况,自动算出还剩多少钱,不用手动算半天 / Count the use of class fees, automatically calculate how much money is left, no need to calculate manually for a long time
- 做猜数字、打地鼠、飞机大战这样的小游戏,自己当游戏设计师 / Make small games like guessing numbers, whack-a-mole, and plane war, and be the game designer yourself
- 分析你考试成绩的变化趋势,画出超好看的成绩曲线图 / Analyze the change trend of your exam scores and draw a super beautiful score curve
> 💡 侧边栏提示(电子版)/ 💡 Sidebar Tip (Digital Edition): > 你知道吗?YouTube、豆瓣、甚至很多小朋友玩的《我的世界》小插件,都是用Python写出来的哦!/ Did you know? YouTube, Douban, and even many small plug-ins of "Minecraft" played by children are written in Python!
为什么我们第一个要学Python?/ Why do we learn Python first?
现在世界上有很多种编程语言,比如C++、Java、Scratch,为什么要选Python作为我们的第一个编程魔法棒呢?/ There are many kinds of programming languages in the world now, such as C++, Java, Scratch, why choose Python as our first programming magic wand? 1. 语法像英语,超简单 / Grammar is like English, super simple 写Python代码就像写简单的英语句子,比如我们要让电脑打印「你好」,只要写`print("你好")`就行,对比其他语言要写十几行才能实现同样的功能,Python真的对新手太友好啦~/ Writing Python code is like writing simple English sentences. For example, if we want the computer to print "Hello", we just need to write `print("你好")`. In contrast, other languages need to write more than a dozen lines to achieve the same function. Python is really friendly to beginners! 2. 超多功能,随便用 / Super many functions, use as you like Python有超多好用的「魔法工具包」,不管你是想做游戏、画图、处理表格、还是做人工智能,都能找到对应的工具,不用自己从零开始造轮子~/ Python has a lot of useful "magic tool kits". No matter you want to make games, draw pictures, process tables, or do artificial intelligence, you can find the corresponding tools, you don't need to build the wheel from scratch. 3. 全球都在用,学会了超有用 / Used all over the world, super useful to learn 现在很多小学的信息课、科技竞赛都已经把Python作为必修内容啦,学会了Python,不管是做课题还是参加比赛,都能帮上大忙~/ Now many primary school information classes and technology competitions have taken Python as a required course. Learning Python can help a lot in doing projects or participating in competitions.
1.2 安装Python魔法棒:运行环境搭建 / Install Python Magic Wand: Build the Operating Environment
要使用Python魔法,我们首先要给电脑装上Python的「魔法工具包」,也就是Python运行环境,跟着下面的步骤做,10分钟就能搞定啦~/ To use Python magic, we first need to install the "magic toolkit" of Python on the computer, that is, the Python operating environment. Follow the steps below, you can get it done in 10 minutes.
安装前的小准备 / Pre-installation Preparation
在安装之前,我们需要先知道两个小概念,帮你更好地理解安装过程:/ Before installation, we need to know two small concepts to help you better understand the installation process: 1. Python解释器 / Python Interpreter:就像一个小翻译官,你写的Python代码是「中文」(哦不,是Python语言的指令),解释器会把它们翻译成电脑能读懂的0和1,让电脑乖乖听话。/ It's like a little translator. The Python code you write is an instruction in Python language, and the interpreter will translate them into 0s and 1s that the computer can understand, so that the computer will obey. 2. PATH 环境变量 / PATH Environment Variable:就像你家的地址簿,你告诉电脑Python的「家庭住址」,以后你喊「Python,出来干活啦」,电脑就能直接找到它,不用挨个文件夹翻啦~/ It's like your home address book. You tell the computer the "home address" of Python. When you shout "Python, come out and work!" later, the computer can find it directly without rummaging through each folder.
分步安装指南 / Step-by-step Installation Guide
我们分别来看Windows和Mac电脑的安装方法哦~/ Let's look at the installation methods of Windows and Mac computers respectively.
##### Windows 电脑安装步骤 / Windows Computer Installation Steps ```mermaid graph LR A[打开Python官网] --> B[点击下载Python 3按钮] B --> C[运行下载好的安装包] C --> D[勾选Add Python to PATH] D --> E[点击Install Now] E --> F[安装完成!] ```
**步骤1:下载官方安装包 / Step
1:下载官方安装包,打开浏览器,在地址栏输入`python.org`,这就是Python的官方魔法商店,所有的官方工具都是这里免费提供的,不用担心有病毒哦。进入官网之后,你会看到一个超大的黄色按钮,写着「Download Python 3.x.x」(x是版本号,比如3.12.0),点击这个按钮,电脑就会自动下载安装包,就像你在网上买玩具,点击购买就会自动发货一样。如果你的电脑下载速度很慢,别着急,可以让爸爸妈妈帮你切换到国内的镜像源,比如清华大学的Python镜像站,地址是`https://mirrors.tuna.tsinghua.edu.cn/python/`,这里的下载速度和坐火箭一样快~ 步骤2:运行安装包,下载完成后,到你的「下载」文件夹里,找到那个叫`python-3.x.x-amd64.exe`(Windows电脑)或者`python-3.x.x-macos.pkg`(Mac电脑)的文件,双击打开,就像你双击打开游戏安装包一样。 这时候会弹出一个安装窗口,Windows用户一定要记得在窗口最下方找到「Add Python.exe to PATH」的复选框,把它勾上!这个步骤超级重要,就像你搬家之后要告诉同学你的新地址,不勾的话电脑就找不到Python在哪里,以后你喊它干活,它根本听不到哦。勾好之后,点击窗口上方的「Install Now」按钮,默认安装路径是C盘,如果你担心C盘空间不够,也可以点击「Customize installation」自己选安装路径,比如放在D盘的「Python魔法工具箱」文件夹里,就像你买新书包可以选放在衣柜里还是书桌上一样。 Mac用户就更简单啦,双击下载好的pkg文件,一路点击「继续」「安装」,输入电脑密码就可以啦,就像你安装平时用的学习软件一样简单。 步骤3:等待安装完成,你会看到一个进度条在跑,就像你
你有没有过这样的经历:放学要整理20本课本、3本练习册还有文具盒,每次都要花10分钟,要是能有个小魔法帮我们把课本按课表自动摆进书包就好了?/ Have you ever had this experience: after school, you need to organize 20 textbooks, 3 workbooks and a pencil case, which takes 10 minutes every time. It would be great if there was a little magic to automatically put the textbooks into the schoolbag according to the timetable? 很多喜欢看《哈利波特》的小朋友都幻想过,要是自己也会魔法就好了:挥挥魔杖就能让书包自己整理好,念个咒语就能把手抄报的边框画得整整齐齐,甚至能让电脑帮自己统计图书角的借阅记录。其实啊,我们现实世界里真的有这样的「魔法棒」,它就是今天我们要认识的新朋友——Python。/ Many kids who like to watch "Harry Potter" have ever fantasized that it would be great if they could do magic: wave the wand to make the schoolbag organize itself, recite a spell to draw the border of the hand-copied newspaper neatly, and even let the computer help you count the borrowing records of the book corner. In fact, there is really such a "magic wand" in our real world, which is our new friend we are going to meet today——Python.
1.1 什么是Python?/ What is Python?
Python 这个名字其实不是指爬行动物蟒蛇哦,它的名字来自创作者们喜欢的英国喜剧团体「Monty Python」,是个超有活力的编程小工具~/ Actually, the name "Python" doesn't refer to the reptile python. Its name comes from "Monty Python", a British comedy group that the creators love. It's a super energetic programming tool. 简单来说,Python 就是一种人和电脑能互相听懂的语言/ Simply put, Python is a **language that both humans and computers can understand*