Python 3 basic operations and functions

Source: Internet
Author: User

Recently, for some activity reasons, you need to take a look at python. python visual testing has two mainstream versions: 2.7 and 3.3, but it seems that 3.3 is booming, so you are ready to learn about it. However, there are few tutorials, both of which are 2.7. There are also good tutorials on the official website .. English .. Here we assume that the installation method for c or c # programming is available on windows. download the latest version of Python Windows Installer from the official website or webmaster's download point and download the .exe Installation File. Http://www.python.org/vs2012 can install plug-in http://pytools.codeplex.com/releases/view/82130 after the installation is complete to start IDLE or vs2012 can also be nonsense not to say print () Basic output input () Basic input so-called declaration... A = 1 when there is a var input before a and the basic if copy code >>> x = int (input ("Please enter an integer:") Please enter an integer: 42 >>> if x <0: x = 0 print ('negative changed to zero ') elif x = 0: print ('zero') elif x = 1: print ('single ') else: print ('more') More Copy code for statement basic copy code >># Measure some strings: a = ['cat ', 'window', 'deletestrate'] >>> for x in a: print (x, len (x) cat 3 window 6 defenestrate 12 copy the code as to break and con Tinue or the original usage len () can both test the length of the string and calculate the number of elements in the array or set. This is also a very big topic since it involves strings, you can use ''" to include strings, of course, there is also a ">>>" wangjidahaoren "'wangji \ ndahaoren'. In the future, we will study in detail this function rangerange (end value) Starting with 0, step Size: one range (start value, end value) Step Size: one range (start value, end value, step size) >>> for I in range (-10,-100,-30 ): print (I)-10-40-70pass .. (True. But it may be useful to the experts) def actually declares the meaning of a function and uses a method similar to the method without return values first -- actually returns the null copy code >>> def fib (n ): "Print aFibonacci series up to n" a, B = 0, while a <n: print (a, end = '') a, B = B, a + B print () >>> fib (2000) 011235813213455891442333776109871597 copying code def actually declares the meaning of a function. This will return a value, returns a chain table copy code >>> def fiber 2 (n): result = [] a, B = 0, while a <n: result. append (a) a, B = B, a + B return result> fsda = fiber 2 (200)> fsda [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144] The copy Code also supports copying the Code with the default value of the form parameter >>> def ask_ OK (prompt, retries = 4, complaint = 'yes or no, please! '): While True: OK = input (prompt) if OK in ('y', 'Ye', 'yes'): return True if OK in ('n ', 'No', 'nop', 'nope'): return False retries = retries-1 if retries <0: raise IOError ('refusenik user') print (complaint) copy the code for the first experiment >>> ask_ OK ('Are you a doubi? ') Are you a doubi? NoFalse second lab copy code >>> ask_ OK ('Are you a doubi? ', 2) Are you a doubi? DoubihaoYes or no, please! Are you a doubi? WobushidoubiYes or no, please! Are you a doubi? MinashangohayouTraceback (most recent call last): File "<pyshell #81>", line 1, in <module> ask_ OK ('Are you a doubi? ', 2) File "<pyshell #74>", line 10, in ask_ OK raise IOError ('refusenik user') OSError: refusenik user: copy the code in keyword to determine whether the sequence contains a value ********* important: the default value is assigned only once, we recommend that you copy the code using the linked list >>> def tryy1 (a, listt = []): listt. append (a) return listt >>> tryy1 (1) [1] >>> tryy1 (2) [1, 2] >>> tryy1 (3) [1, 2, 3] copying code is a static local variable .. There is also a method for passing in the parameter keyword input method, to put it bluntly, we can change the input parameter order in the function or take the test just now as an example> tryy1 (listt = ['doubi', 'dadoubi'], a = 5) ['doubi', 'dadoubi', 5] >>> tryy1 (listt = ['doubi', 'dadoubi'], 1) SyntaxError: non-keyword arg after keyword arg requires the keyword to be passed in after it is used (you do not need the keyword, and the computer does not know what is next to you, keywords are used to disrupt the original order (even if you haven't disrupted the order, the computer uses another method to correspond to the parameters) (two principles should be taken at any time: 1. Do not initialize numeric values. 2. Make it clear. Do not die if it is not done, in addition, it is difficult to determine the order of the corresponding parameters after the keyword appears, so the keyword should also be used later) Some ideas should be adapted to a language with the intent of the author, instead of setting rules, you need to understand the meaning of each rule rather than blindly matching it. In this way, you can get twice the result with half the effort by learning a language, and many languages are common.) >>> def make_incrementor (n): return lambda x: x + n >>> f = make_incrementor (1) >>> f (1) 2 >>> f (2) 3

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.