Basic Python Basics: Learn python basic questions together

Source: Internet
Author: User

Python recently old fire, universal development language, a lot of small partners are either beginner, or from other development language, if you can put the following several basic problems are not laborious write, the foundation should be able to play 80 points, you can carry out the study of advanced, if the following topics if you do not come out, please do not delay time, Quickly lay the groundwork, do not build a plateau in the floating sand.

Topic out to the new bird laying the groundwork, the way to achieve the answer thousands, if the veteran has a more powerful answer do not spray, first thank you.

There are new birds do not look at the answer, do not look at the answer, do not look at the answer, (important things say three times) to solve their own first, with their own simplest idea to achieve, can use Python's own method do not build wheels.

All right, let's go!

First, usually, the title of the article (thesis, post), all require the English initial capitalization, this is a common writing standard.
Now, the requirement to implement a "handler function" satisfies the following requirements:
1. Ability to receive arbitrary strings
2. Ability to convert the first letter of the string received into uppercase
3. Ability to return the converted String
For example: Receive ' This is Python. ', output ' this is Python '.

1 defCapital (*args):2Ws_list = []3     ifargs:4          forArginchargs:5Words = Arg.split (' ')6W_list = []7              forWinchwords:8W =w.capitalize ()9 W_list.append (W)TenNew_word =' '. Join (w_list) One ws_list.append (New_word) A         returnWs_list
" " -------------Run Results-------------->>> Capital (' This is Python ', ' it\ 's My apple ') [' This is Python ', ' it ' s my Apple "]"

Second, sometimes we will need a series of equal spacing numbers (which is particularly useful in data analysis and progress control, etc.), for example: [0, 2, 4, 6, 8]
For the above example, using list (range (0, 10)) can be very convenient to complete.
But range can only be integers, so join us needs [0, 0.1, 0.2, ..., 0.9], then range will not meet our needs.
Therefore, write a function like range to support the step size of the floating-point number.
(I use a list to derive the build, of course, there are small bugs, the new bird may read hard to see if you can find the bug)

1 def Float_range (Start, End, step): 2     return  for inch if  and Round ((X*step), 1) > start]
" " -------------Running Results-------------->>> float_range (0,1,0.2) [0.0, 0.2, 0.4, 0.6, 0.8]>>> float_  Range (0,2,0.2) [0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8]>>> Float_range (0,2,0.3) [0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8]>>> Float_range (1,3,0.5) [0.5, 1.0, 1.5, 2.0, 2.5]"

Third, encryption and decryption is one of the most common requirements.
One of the simplest methods of encryption is to transform existing characters into another according to some rule. For example: A change to B, B to C ... z to a ....
According to the above rules, ' list ' should become ' mjtu '.
Now, it is required to complete the two functions according to the above rules:
1. Cryptographic functions
2. Decryption function

Once you have completed both of these functions, try customizing a string of your own. Add both of these methods to your custom string class.
Tip: "Custom" uses "inheritance" to implement

1 classfather_security:2     #Parent Encryption Class3     defEncrypt (self,s):#Cryptographic Functions4SELF.S =s5         return "'. Join ([Chr (Ord (x) +1) forXinchSelf.s])6 7     defDecrypt (Self,s):#decryption Function8SELF.S =s9         return "'. Join ([Chr (Ord (x)-1) forXinchSelf.s])Ten  One  A classchild_security (father_security): -     #Sub-cryptographic classes -     def __init__(self, s): theSELF.S =s -  -     defChild_encrypt (self):#Sub-cryptographic functions -         returnsuper (). Encrypt (SELF.S) +  -     defChild_decrypt (self):#Sub-decryption function +         returnSuper (). Decrypt (super (). Encrypt (SELF.S))
"" " 

Four, write an adorner, test function run time, now ask you to do the following test:
1. You can test when the function is running
2. You can output the time the function is running to the screen
3. The runtime is recorded in the specified file (as a log) in the form of a log.

1 Import Time2 3 defRun_time (func):4     defNew_func (*args, * *Kwargs):5Path = R'D:\test\pythonlog.txt'6Tmp_time =time.time ()7Start_time = Time.strftime ('%x%x', Time.localtime ())8         Print('start time: {}'. Format (start_time))9result = Func (*args)TenEnd_time = Time.strftime ('%x%x', Time.localtime ()) One         Print('end time: {}'. Format (end_time)) ARun_time = Round (Time.time ()-tmp_time,4) -         Print('run time: {}'. Format (run_time)) -With open (path,'a', encoding='UTF8') as F: theF.writelines (['Start time:'+str (start_time) +'\ n','End Time:'+str (End_time) +'\ n','Run Time:'+str (run_time) +'\ n']) - F.flush () -         returnresult -     returnNew_func +  -  + @run_time A defFab (max): atN, a, b = 0, 0, 1 -L = [] -      whileN <Max: - L.append (b) -         Print(b) -A, B = B, A +b inn = n + 1 -     returnL to  +  - @run_time the defFab_yield (max): *N, a, b = 0, 0, 1 $      whileN <Max:Panax Notoginseng         yieldb -         #Print B theA, B = B, A +b +n = n + 1 A  thex = 10 +L = [i forIinchFab_yield (x)]
"" " 

Remember to go to the test directory under the D disk to see if the log is written

The above is the basic question of Python, check if you are really getting started, if you have not quickly refueling it!

I wish you a speedy success!

Basic Python Basics: Learn python basic questions together

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.