Python One Step Further

来源:互联网 发布:男神节淘宝 编辑:程序博客网 时间:2024/05/16 05:23

Break It Down


When you start a big project , it's important to take some time to break the problem into individual steps. Then you can tackle (and test) one step at a time rather than trying to write a huge program all at once!

当你着手一个大项目时,花些时间去把问题分解成几步是很重要的。然后你就可以每次处理(和测试)一步而不是尝试一下子写完整个工程。


Let's think about the PygLatin problem. Pig Latin is a language where we take the first letter of a word and put it on the end while also adding a vowel sound. So dog becomes "ogday". What are the steps we need to take?

拿PygLatin问题为例。我们把一个单词的第一个字母放在它的末尾然后加上一个 vowel sound 。所以dog就变成了 “ogday" 我们需要走哪几步呢?


Ask the user to input a word in English
Check to make sure the user entered a valid word
Convert the word from English to Pig Latin
Display the translation result
Notice that some of the steps can themselves be broken down into individual steps. For example, we'll want to think through the algorithm for step #3 before we start coding.

A little bit of time invested in thinking through the decomposition of and algorithms for your program can save you a LOT of frustration down the road!

Get a piece of paper and work out an algorithm for step #3 of the project.


Documentation: a PSA


Codecademy is a great educational tool—you can think of it as an online classroom for learning a programming language. It's important to remember, however, that the emphasis should be on your approach to problems and learning to think like a programmer, and not on memorizing every single method or nuance of syntax to be found in a given programming language.


Think of it this way: if you're going to Germany, you wouldn't say to yourself, "Hey, I took a year of German in college, I don't need my dictionary or translation app!" You would totally bring those things to help remind you of vocabulary and syntax in case you were to get stuck; even professional translators keep dictionaries and grammar guides handy for unusual words and tricky constructions.

Much like professional translators, professional programmers refer to documentation when they're not clear on best practices, forget how a certain method works, or need to look up syntax. Python's documentation can be found here. We encourage you to read through it on your own!

What Good are Functions?


A function is a reusable section of code written to perform a specific task in a program. You might be wondering why you need to separate your code into functions, rather than just writing everything out in one giant block. You might have guessed the answer(s) already, but here are some of the big ones:


If something goes wrong in your code, it's much easier to find and fix bugs if you've organized your program well. Assigning specific tasks to separate functions helps with this organization.

By assigning specific tasks to separate functions (an idea computer scientists call separation of concerns), you make your program less redundant and your code more reusable—not only can you repeatedly use the same function in a single program without rewriting it each time, but you can even use that function in another program.

When we learn more about objects, you'll find out there are a lot of interesting things we can do with functions that belong to those objects (called methods).

D.R.Y. stands for Dont Repeat Yourself. Using functions we can avoid repeating ourselves more easily.

Recursion is a powerful tool. It allows us to call a piece of code multiple times and call itself. There are some programming problems that cannot be solved without recursion! Functions make it possible to do recursion.

Some Useful Buid-in Function

enumerate(list) works by supplying a corresponding index to each element in the list that you pass it. Each time you go through the loop, index will be one greater, and item will be the next item in the sequence. It's very similar to using a normal for loop with a list, except this gives us an easy way to count how many items we've seen so far.

zip(list1, list2) will create pairs of elements when passed two lists, and will stop at the end of the shorter list.zip can handle three or more lists as well!


More About Control Flow

for-else:

the else statement is executed after the for, but only if the for ends normally—that is, not with a break. 

The break is a one-line statement that means "exit the current loop." An alternate way to make our counting loop exit and stop executing is with the break statement.

while-break:

First, create a while with a condition that is always true. The simplest way is shown.
Using an if statement, you define the stopping condition. Inside the if, you write break, meaning "exit the loop."
The difference here is that this loop is guaranteed to run at least once.

while-else:

Something completely different about Python is the while/else construction. while/else is similar to if/else, but there is a difference: the else block will execute anytime the loop condition is evaluated to False. This means that it will execute if the loop is never entered or if the loop exits normally. If the loop exits as the result of a break, the else will not be executed.


NOT COMPLETELY

原创粉丝点击