Stupid way to learn Python (25)

Source: Internet
Author: User

Exercise 25: More and more practice

We will do some exercises on functions and variables to make sure that you are really mastering this knowledge. This exercise can be said to be a way for you: Write a program, do a line-by-row study, and get to know it.

But this exercise is a little different, you don't need to run it, instead, you need to import it into Python and run it in a way that executes its own function.

First, run the python ex25.py in the normal way, find the errors inside and correct them all. Then you need to follow the next section of the answer to complete this exercise.

The results you should see

This session will be used to communicate with your . PY in an interactive way, in the python compiler you used to do arithmetic.

This is what I made out to look like:

Let's analyze each step by line:

  • In line 5th you have executed your ex25.py import, just like any other import you have done. You do not need to add the . py suffix when you import. In this process, you use ex25.py as a "module", and the functions you define in this module can be called directly.
  • Line 6th you create a "sentence (sentence)" to handle.
  • Line 7th you use ex25 to call your first function ex25.break_words. One of them . The (dot, period) symbol tells Python: "Hey, I'm going to run a function called break_words in ex25 !" ”
  • Line 8th We just enter words, and Python will print out what's inside this variable on line 9th. It may seem strange, but it's actually a list, and you'll learn it in later chapters.
  • 10-11 rows We use ex25.sort_words to get a sorted sentence.
  • 13-16 rows We use Ex25.print_first_word and Ex25.print_last_word to print out the first and last words.
  • The 17th line is more interesting. I wrote the words variable incorrectly into a wrods, so Python gave an error message in line 18-20.
  • 21-22 lines We have printed out a list of modified words. We have printed the first and last words, so we don't print them again.

The rest of the line you need to analyze yourself, and leave it as your bonus points exercise.

Bonus points Exercise
  1. Study the lines that are not analyzed in the answers, and find out the ins and outs of them. Make sure you understand that you are using the functions defined in module ex25 .
  2. Try to do help (ex25) and help (ex25.break_words) . This is how you get the module help documentation. The so-called help document is what you put in the "" "when you define a function, and they are also known as documentation comments (document annotations), and you'll see something more like that later.
  3. Type ex25 repeatedly. It's a very annoying thing. A shortcut is to import the module using the from ex25 import * method. It's equivalent to saying, "I'm going to import everything in the ex25." "Programmers like to say such a flip sentence, open a new session and see if all your functions are already there."
  4. Follow the lines of your script through the python compiler to see what it looks like. You can execute ctrl-d (under Windows ctrl-z) to close the compiler.

Exercise Exercises

Stupid way to learn Python (25)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.