300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 学习Python编程的11个入门技巧

学习Python编程的11个入门技巧

时间:2019-08-26 01:02:57

相关推荐

学习Python编程的11个入门技巧

We are so excited that you have decided to embark on the journey of learning Python! One of the most common questions we receive from our readers is “What’s the best way to learn Python?”

我们非常激动,您决定开始学习Python的旅程! 我们从读者那里收到的最常见问题之一是“学习Python的最佳方法是什么?”

I believe that the first step in learning any programming language is making sure that you understand how to learn. Learning how to learn is arguably the most critical skill involved in computer programming.

我相信学习任何编程语言的第一步就是要确保您了解如何学习。 学习如何学习可以说是计算机编程中最关键的技能。

Why is knowing how to learn so important? The answer is simple: as languages evolve, libraries are created, and tools are upgraded. Knowing how to learn will be essential to keeping up with these changes and becoming a successful programmer.

为什么知道如何学习如此重要? 答案很简单:随着语言的发展,库的创建和工具的升级。 知道如何学习对于跟上这些变化并成为一名成功的程序员至关重要。

In this article, we will offer several learning strategies that will help jump start your journey of becoming a rockstar Python programmer!

在本文中,我们将提供几种学习策略,这些策略将帮助您快速开始成为摇滚明星Python程序员的旅程!

Free Bonus:Python Cheat Sheet

免费奖金:Python备忘单

Get aPython Cheat Sheet (PDF)and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions:

获取Python备忘单(PDF)并学习Python 3的基础知识,例如使用数据类型,字典,列表和Python函数:

坚持下去 (Make It Stick)

Here are some tips to help you make the new concepts you are learning as a beginner programmer really stick:

这里有一些技巧,可以帮助您使初学者程序员真正学习的新概念更加牢固:

提示1:每天编码 (Tip #1: Code Everyday)

Consistency is very important when you are learning a new language. We recommend making a commitment to code every day. It may be hard to believe, but muscle memory plays a large part in programming. Committing to coding everyday will really help develop that muscle memory. Though it may seem daunting at first, consider starting small with 25 minutes everyday and working your way up from there.

学习新语言时,一致性非常重要。 我们建议每天做出代码承诺。 可能难以置信,但是肌肉记忆在编程中起着很大的作用。 每天致力于编码将真正帮助发展肌肉记忆。 尽管乍一看似乎令人生畏,但考虑每天从25分钟开始,然后从那里开始。

Check out the First Steps With Python Guide for information on setup as well as exercises to get you started.

查阅“ Python入门指南”以获取有关设置和入门的信息。

提示2:写出来 (Tip #2: Write It Out)

As you progress on your journey as a new programmer, you may wonder if you should be taking notes. Yes, you should! In fact, research suggests that taking notes by hand is most beneficial for long-term retention. This will be especially beneficial for those working towards the goal of becoming a full-time developer, as many interviews will involve writing code on a whiteboard.

在您成为新程序员的过程中,您可能想知道是否应该做笔记。 是的你应该! 实际上,研究表明,手工记笔记对长期保留最有利。 这对于那些致力于成为一名全职开发人员的目标特别有益,因为许多访谈都涉及在白板上编写代码。

Once you start working on small projects and programs, writing by hand can also help you plan your code before you move to the computer. You can save a lot of time if you write out which functions and classes you will need, as well as how they will interact.

一旦开始处理小型项目和程序,手工编写还可以帮助您在移至计算机之前计划代码。 如果写出所需的函数和类以及它们之间的交互方式,则可以节省大量时间。

提示3:互动! (Tip #3: Go Interactive!)

Whether you are learning about basic Python data structures (strings, lists, dictionaries, etc.) for the first time, or you are debugging an application, the interactive Python shell will be one of your best learning tools. We use it a lot on this site too!

无论您是初次学习基本的Python数据结构(字符串,列表,字典等),还是调试应用程序,交互式Python Shell都是您最佳的学习工具之一。 我们也在本网站上大量使用它!

To use the interactive Python shell (also sometimes called a “Python REPL”), first make sure Python is installed on your computer. We’ve got a step-by-step tutorial to help you do that. To activate the interactive Python shell, simply open your terminal and runpythonorpython3depending on your installation. You can find more specific directions here.

要使用交互式Python Shell(有时也称为“ Python REPL”),请首先确保在您的计算机上安装了Python。 我们有一个分步教程可以帮助您做到这一点。 要激活交互式Python Shell,只需打开终端并根据安装运行pythonpython3。 您可以在此处找到更多具体说明。

Now that you know how to start the shell, here are a few examples of how you can use the shell when you are learning:

现在您已经知道如何启动外壳程序,下面是一些学习过程中如何使用外壳程序的示例:

Learn what operations can be performed on an element by using dir():

通过使用dir()了解可以对元素执行哪些操作:

>>> >>> my_string my_string = = 'I am a string''I am a string'>>> >>> dirdir (( my_stringmy_string ))['__add__', ..., 'upper', 'zfill'] # Truncated for readability['__add__', ..., 'upper', 'zfill'] # Truncated for readability

The elements returned fromdir()are all of the methods (i.e. actions) that you can apply to the element. For example:

dir()返回的元素是可以应用于该元素的所有方法(即操作)。 例如:

Notice that we called theupper()method. Can you see what it does? It makes all of the letters in the string uppercase! Learn more about these built-in methods under “Manipulating strings” in this tutorial.

注意,我们调用了upper()方法。 您能看到它的作用吗? 它会使字符串中的所有字母都大写! 在本教程的“操纵字符串”下了解有关这些内置方法的更多信息。

Learn the type of an element:

了解元素的类型:

>>> >>> typetype (( my_stringmy_string ))>>> >>> strstr

Use the built-in help system to get full documentation:

使用内置的帮助系统获取完整的文档:

Import libraries and play with them:

导入库并使用它们:

>>> >>> from from datetime datetime import import datetimedatetime>>> >>> dirdir (( datetimedatetime ))['__add__', ..., 'weekday', 'year'] # Truncated for readability['__add__', ..., 'weekday', 'year'] # Truncated for readability>>> >>> datetimedatetime .. nownow ()()datetime.datetime(, 3, 14, 23, 44, 50, 851904)datetime.datetime(, 3, 14, 23, 44, 50, 851904)

Run shell commands:

运行shell命令:

提示4:休息一下 (Tip #4: Take Breaks)

When you are learning, it is important to step away and absorb the concepts. The Pomodoro Technique is widely used and can help: you work for 25 minutes, take a short break, and then repeat the process. Taking breaks is critical to having an effective study session, particularly when you are taking in a lot of new information.

在学习时,重要的是要走开并吸收这些概念。 Pomodoro技术得到了广泛的使用,可以提供帮助:您工作25分钟,稍事休息,然后重复该过程。 休息对进行有效的学习至关重要,特别是在您吸收大量新信息时。

Breaks are especially important when you are debugging. If you hit a bug and can’t quite figure out what is going wrong, take a break. Step away from your computer, go for a walk, or chat with a friend. In programming, your code must follow the rules of a language and logic exactly, so even missing a quotation mark will break everything. Fresh eyes make a big difference.

在调试时,中断尤为重要。 如果您遇到了一个错误并且无法完全弄清楚问题出在哪里,请稍事休息。 离开计算机,散步或与朋友聊天。 在编程中,您的代码必须完全遵循一种语言和逻辑规则,因此即使缺少引号也会破坏所有内容。 新鲜的眼睛有很大的不同。

提示5:成为漏洞赏金猎人 (Tip #5: Become a Bug Bounty Hunter)

Speaking of hitting a bug, it is inevitable once you start writing complex programs that you will run into bugs in your code. It happens to all of us! Don’t let bugs frustrate you. Instead, embrace these moments with pride and think of yourself as a bug bounty hunter.

说到遇到错误,一旦开始编写复杂的程序,就将不可避免地遇到代码中的错误。 它发生在我们所有人身上! 不要让错误使您沮丧。 取而代之的是,自豪地拥抱这些时刻,并把自己当作一个漏洞赏金猎人。

When debugging, it is important to have a methodological approach to help you find where things are breaking down. Going through your code in the order in which it is executed and making sure each part works is a great way to do this. Once you have an idea of where things might be breaking down, insert the following line of code into your scriptimport pdb; pdb.set_trace()and run it. This is the Python debugger and will drop you into interactive mode. The debugger can also be run from the command line withpython -m pdb <my_file.py>.

调试时,重要的是要有一种方法论方法来帮助您找到问题所在。 按照执行顺序遍历您的代码,并确保每个部分都能正常工作,这是一种很好的方法。 一旦知道了可能发生故障的地方,请将以下代码行插入脚本import pdb; pdb.set_trace()import pdb; pdb.set_trace()并运行它。 这是Python调试器,它将使您进入交互模式。 也可以使用python -m pdb <my_file.py>从命令行运行调试器。

使其协作 (Make It Collaborative)

Once things start to stick, expedite your learning through collaboration. Here are some strategies to help you get the most out of working with others.

一旦一切开始,请通过协作加快学习速度。 以下是一些策略,可帮助您从与他人的协作中获得最大收益。

提示6:与正在学习的其他人在一起 (Tip #6: Surround Yourself With Others Who Are Learning)

Though coding may seem like a solitary activity, it actually works best when you work together. It is extremely important when you are learning to code in Python that you surround yourself with other people who are learning as well. This will allow you to share the tips and tricks you learn along the way.

尽管编码似乎是一种单独的活动,但实际上,当您一起工作时,编码效果最佳。 当您学习使用Python进行编程并使自己也与正在学习的其他人在一起时,这非常重要。 这将使您可以分享在此过程中学习的技巧和窍门。

Don’t worry if you don’t know anyone. There are plenty of ways to meet others who are passionate about learning Python! Find local events or Meetups or join PythonistaCafe, a peer-to-peer learning community for Python enthusiasts like you!

如果您不认识任何人,请不要担心。 有很多方法可以结识其他热衷于学习Python的人! 查找本地事件或聚会,或加入PythonistaCafe ,这是一个像您这样的Python爱好者的对等学习社区!

提示7:教学 (Tip #7: Teach)

It is said that the best way to learn something is to teach it. This is true when you are learning Python. There are many ways to do this: whiteboarding with other Python lovers, writing blog posts explaining newly learned concepts, recording videos in which you explain something you learned, or simply talking to yourself at your computer. Each of these strategies will solidify your understanding as well as expose any gaps in your understanding.

有人说,学习某物的最好方法就是教它。 当您学习Python时,这是正确的。 有许多方法可以做到这一点:与其他Python爱好者一起白板,写博客文章解释新近学习的概念,录制视频以解释自己学到的东西或在计算机上与自己交谈。 这些策略中的每一个都会巩固您的理解,并揭露您理解中的任何空白。

提示8:结对程序 (Tip #8: Pair Program)

Pair programming is a technique that involves two developers working at one workstation to complete a task. The two developers switch between being the “driver” and the “navigator.” The “driver” writes the code, while the “navigator” helps guide the problem solving and reviews the code as it is written. Switch frequently to get the benefit of both sides.

结对编程是一种涉及两名开发人员在一个工作站上工作以完成一项任务的技术。 这两个开发人员在“驱动程序”和“导航程序”之间切换。 “驱动程序”编写代码,而“导航程序”帮助指导解决问题并在编写代码时对其进行检查。 经常切换以获得双方的利益。

Pair programming has many benefits: it gives you a chance to not only have someone review your code, but also see how someone else might be thinking about a problem. Being exposed to multiple ideas and ways of thinking will help you in problem solving when you got back to coding on your own.

结对编程有很多好处:它使您不仅有机会让某人检查您的代码,而且还使别人有机会思考问题。 面对多种想法和思维方式,当您自己回到编码时,将帮助您解决问题。

提示9:询问“良好”问题 (Tip #9: Ask “GOOD” Questions)

People always say there is no such thing as a bad question, but when it comes to programming, it is possible to ask a question badly. When you are asking for help from someone who has little or no context on the problem you are trying to solve, its best to ask GOOD questions by following this acronym:

人们总是说没有一个坏问题之类的东西,但是当涉及到编程时,可能会提出一个很糟糕的问题。 当您寻求对您要解决的问题了解甚少或没有上下文的人的帮助时,最好遵循以下缩写来提出良好的问题:

G: Give context on what you are trying to do, clearly describing the problem.O: Outline the things you have already tried to fix the issue.O: Offer your best guess as to what the problem might be. This helps the person helping you not only know what you are thinking, but also know that you have does some thinking on your own.D: Demo what is happening. Include the code, a traceback error message, and an explanation of the steps you executed that resulted in the error. This way, the person helping does not have to try to recreate the issue.G:给出您要尝试做的事情的上下文,清楚地描述问题。O:概述您已尝试解决的问题。O:就问题可能出在您的最佳猜测上。 这可以帮助帮助您的人不仅知道您在想什么,而且还知道您自己进行一些思考。D:演示正在发生的事情。 包括代码,回溯错误消息以及对导致错误的执行步骤的说明。 这样,帮助人员不必尝试重新创建问题。

Good questions can save a lot of time. Skipping any of these steps can result in back-and-forth conversations that can cause conflict. As a beginner, you want to make sure you ask good questions so that you practice communicating your thought process, and so that people who help you will be happy to continue helping you.

好的问题可以节省很多时间。 跳过这些步骤中的任何一个都可能导致来回对话,从而可能导致冲突。 作为一个初学者,您要确保提出好的问题,以便练习交流思想过程,以便帮助您的人很乐意继续帮助您。

做点什么 (Make Something)

Most, if not all, Python developers you speak to will tell you that in order to learn Python, you must learn by doing. Doing exercises can only take you so far: you learn the most by building.

与您交谈的大多数(如果不是全部)Python开发人员都会告诉您,要学习Python,您必须边做边学。 做运动只能带您走远:您可以通过锻炼学习最多的知识。

提示10:构建任何东西 (Tip #10: Build Something, Anything)

For beginners, there are many small exercises that will really help you become confident with Python, as well as develop the muscle memory that we spoke about above. Once you have a solid grasp on basic data structures (strings, lists, dictionaries, sets), object-oriented programming, and writing classes, it’s time to start building!

对于初学者来说,有许多小练习可以真正帮助您对Python充满信心,并增强我们上面提到的肌肉记忆。 一旦您对基本数据结构(字符串,列表,字典,集合), 面向对象的编程和编写类有了充分的了解,就该开始构建了!

What you build is not as important as how you build it. The journey of building is truly what will teach you the most. You can only learn so much from reading Real Python articles and courses. Most of your learning will come from using Python to build something. The problems you will solve will teach you a lot.

构建的内容不如构建的方式重要。 真正的建设之旅将带给您最大的启发。 通过阅读Real Python文章和课程,您只能学到很多。 您的大部分学习将来自使用Python进行构建。 您将解决的问题将教给您很多东西。

There are many lists out there with ideas for beginner Python projects. Here are some ideas to get you started:

那里有很多列表,其中包含针对Python初学者的想法。 这里有一些想法可以帮助您入门:

Number guessing gameSimple calculator appDice roll simulatorBitcoin Price Notification Service 猜数字游戏 简单的计算器应用程序 骰子翻滚模拟器 比特币价格通知服务

If you find it difficult to come up with Python practice projects to work on, watch this video. It lays out a strategy you can use to generate thousands of project ideas whenever you feel stuck.

如果您发现很难提出Python实践项目,请观看此视频 。 它列出了一种策略,您可以在遇到困难时使用它生成数千个项目创意。

提示#11:为开源做贡献 (Tip #11: Contribute to Open Source)

In the open-source model, software source code is available publicly, and anyone can collaborate. There are many Python libraries that are open-source projects and take contributions. Additionally, many companies publish open-source projects. This means you can work with code written and produced by the engineers working in these companies.

在开源模型中,软件源代码是公开可用的,任何人都可以协作。 有许多Python库是开放源代码项目,需要做出贡献。 此外,许多公司都发布开源项目。 这意味着您可以使用由这些公司的工程师编写和生产的代码。

Contributing to an open-source Python project is a great way to create extremely valuable learning experiences. Let’s say you decide to submit a bug fix request: you submit a “pull request” for your fix to be patched into the code.

为开源Python项目做贡献是创造极有价值的学习体验的好方法。 假设您决定提交一个错误修复请求:您提交一个“拉取请求”,以将修复程序修补到代码中。

Next, the project managers will review your work, providing comments and suggestions. This will enable you to learn best practices for Python programming, as well as practice communicating with other developers.

接下来,项目经理将审查您的工作,并提供评论和建议。 这将使您学习Python编程的最佳实践,以及与其他开发人员进行交流的实践。

For additional tips and tactics that will help you break into the open-source world, check out the video embedded below:

有关可帮助您闯入开源世界的其他提示和策略,请查看下面嵌入的视频:

前进并学习! (Go Forth and Learn!)

Now that you have these strategies for learning, you are ready to begin your Python journey! Find Real Python’s Beginners Roadmap for Learning here! We also offer a beginner’s level Python course, which uses interesting examples to help you learn programming and web development.

既然您已经有了这些学习策略,就可以开始Python之旅了! 在此处查找Real Python的初学者学习路线图! 我们还提供初学者级别的Python课程 ,其中使用了有趣的示例来帮助您学习编程和Web开发。

Happy Coding!

编码愉快!

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are: Krishelle Hardson-Hurley (Author) and Joanna Boyer (Editor).

Real Python上的每个教程都是由一组开发人员创建的,因此符合我们的高质量标准。 从事本教程工作的团队成员是: Krishelle Hardson-Hurley (作者)和Joanna Boyer (编辑)。

翻译自: //04/11-beginner-tips-for-learning-python-programming/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。