Review Python BASICS (5)

Source: Internet
Author: User

This series of blogs takes notes from learn python the hard way.

1. Because the five chapters to be viewed today is a review of the nature of the content, the content in the front is integrated, so there is no new content

OnlyCodePaste it here

View code

 Print   "  Let's practice everything.  "  Print   "  You \ 'd need to know \ 'bout escapes with \ that do \ n newlines and \ t tabs.  "  Poem = """ \ T the lovely wordwith logic so firmly plantedcannot discern \ n the needs of lovenor comprehend passion from intuitionand requires an explanation \ n \ t \ twhere there is none.  """  Print   "  -------------------  "  Print  Poem  Print   "  -------------------  "  Five = 10-2 + 3-6 Print  "  This shoshould be five: % s  " % Five  Def  Secret_formula (started): jelly_beans = Started * 500 Jars = Jelly_beans/1000 Crates = Jar S/100 Return  Jelly_beans, jars, cratesstart_point = 10000 Beans, jars, crates = Secret_formula (start_point) Print   "  With a starting point of: % d  " % Start_point  Print   "  We 'd have % d beans, % d jars, and % d crates.  " % (Beans, jars, crates) start_point = Start_point/10 Print   "  We can also do that this way:  " Print   "  We 'd have % d beans, % d jars, and % d crates.  " % Secret_formula (start_point)

Output

View code

E: \ SkyDrive \ Python \ the hard way to learn Python> Python ex24.pylet  '  S practice everything. You '  D need to know  '  Bout escapes with \ that do newlines  And Tabs. ------------------- The lovely wordwith logic so firmly plantedcannot discern the needs of lovenor comprehend passion  From  Intuition  And  Requires an explanation where there  Is  None. ------------------- This shoshould be five: 5 With a starting point: 10000 We  ' D have 5000000 beans, 5000 jars, and 50 crates.  We can also do that this way: we  '  D have 500000 beans, 500 jars, and 5 crates. 

 

2. Put your own functions in a file, and then import the file to use these functions in the code.

Put the function in the ex25.py file.

View code

 Def  Break_words (stuff ):  """  This function will break up words for us.  """  Words = Stuff. Split ("   "  )  Return  Words  Def  Sort_words (words ):  """  Sorts the words.  """      Return  Sorted (words)  Def  Print_first_word (words ):  """  Prints the first word after popping it off. """  Word = Words. Pop (0)  Print  Word  Def  Print_last_word (words ):  """  Prints the last word after popping it off.  """  Word = Words. Pop (-1 )  Print  Word  Def Sort_sentence (sentence ):  """  Takes in a full sentence and returns the sorted words.  """  Words = Break_words (sentence)  Return  Sort_words (words)  Def  Print_first_and_last (sentence ):  """  Prints the first and last words of the sentence.  """  Words =Break_words (sentence) print_first_word (words) print_last_word (words)  Def  Print_first_and_last_sorted (sentence ):  """  Sorts the words then prints the first and last one.  """  Words = Sort_sentence (sentence) print_first_word (words) print_last_word (words) 

Run the following command in the python command line:

E: \ SkyDrive \ Python \ the hard way to learn Python> Python ex25.pye: \ SkyDrive \ Python \ the hard way to learn Python > Pythonpython 2.7 . 3 (Default, APR 10   2012 , 23 : 24 : 47 ) [MSC v. 1500   64  Bit (amd64)] on win  32  Type  "  Help  " ,"  Copyright  " , "  Credits  " Or "  License  "   For   More  Information. >>> Import ex25 >>> Sentence = "  All good things come to those who wait. " >>> Words = Ex25.break _ words (sentence) # Use the functions in ex25. If it is too troublesome, you can start from ex25 import *, so that this line can write words = break_words (sentence) >>> Words [  '  All  ' , '  Good  ' , '  Things  ' , '  Come ' , '  To  ' , '  Those  ' , '  Who  ' , '  Wait.  '  ] >>> Sorted_words = Ex25.sort _ words (words) >>>Sorted_words [  '  All  ' , '  Come  ' , '  Good  ' , '  Things  ' , '  Those  ' ,'  To  ' , '  Wait.  ' , '  Who  '  ] >>> Ex25.print _ first_word (words) All >>> Ex25.print _ last_word (words)  Wait  . >>> Wrodstraceback (most recent call Last  ): File  "  <Stdin>  " , Line 1 , In <Module> Nameerror: Name  '  Wrods  '  Is Not Defined >>> Words [  '  Good  ' , '  Things  ' , '  Come  ' , '  To  ' , '  Those  ' , '  Who  '  ] >>>Ex25.print _ first_word (sorted_words) All >>> Ex25.print _ last_word (sorted_words)  Who >>> Sorted_words [  '  Come  ' , '  Good  ' , '  Things  ' , '  Those ' , '  To  ' , '  Wait.  '  ] >>> Sorted_words = Ex25.sort _ sentence (sentence) >>> Sorted_words [  '  All  ' , '  Come ' , '  Good  ' , '  Things  ' , '  Those  ' , '  To  ' , '  Wait.  ' ,'  Who  '  ] >>> Ex25.print _ first_and_last (sentence) All  Wait .

Two important points are worth noting.

    • The difference between import ex25 and from ex25 import * is that the call function ex25.break _ words (sentence) and the direct call of break_words (sentence)
    • List can pop any location

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.