「 Learning notes-Python 」 Python Process Control

Source: Internet
Author: User

Table of Contents1 if Statement 2 for Statement 3 range function 4 break and continue statement, use else5 pass Statement 6 to define the function 7 programming style 1 if statement >>> x = int (raw_input ("Please input a number:") Please input a number: 3 >>> x 3 >>> if x <0 :... x = 0... print 'negative number '... elif x = 0 :... print 'zero '... elif x = 1 :... print 'singles '... else :... print 'more '... the for statement in the More2 for statement python is different from the for statement in C or Pascal. In c, the for statement is usually determined by the judgment statement and condition, The value change consists of three parts. In python, The for statement structure is traversed on a sequence: >>># Measure some string... words = ['cat', 'windows', 'linux '] >>> for w in words :... print w, len (w )... cat 3 windows 7 linux 53 range Function If You Want To iterate over a digital sequence, the range function may be useful to you, the following example shows the meanings of different expressions of range: >>> range (5) [0, 1, 2, 3, 4] >>> range) [2, 3, 4] >>> range (5, 10, 1) [5, 6, 7, 8, 9] >>>> range (5, 10, 2) [5, 7, 9] >>> range (, 3) [5, 8] if you want to use subscript to iterate on a sequence, you can use the range Function >>> word S ['cat', 'windows', 'linux '] >>> for I in range (len (words )):... print I, words [I]... 0 cat1 windows2 in most cases, the enumerate function is more convenient. We will introduce it in later chapters. 4. for the break and continue statements, elseelse can be used for the for loop. When the loop ends naturally, that is, it is not aborted by break, the statements in else >>> for n in range ):... for x in range (2, n ):... if n % x = 0 :... print n, 'eques', x, '*', n/x... break... else :... print n, 'is a prime number '... 2 is a prime number3 is a prime number4 equals 2*25 is a prime number6 equals 2*37 is a prime number8 equals 2*49 equals 3*3 when used for loops, else is more like the else in the try statement. It is executed without exceptions. Like the else. continue statement in if-else, it is referenced from the C language and directly transferred to the next iteration of the loop. >>> For num in range (2, 10 ):... if num % 2 = 0 :... print num, 'is an even number '... continue... print num, 'is not an even number '... 2 is an even number3 is not an even number4 is an even number5 is not an even number6 is an even number7 is not an even number8 is an even number9 is not an even number5 pass statement pass the statement does not do anything, when you need a statement to conform to the syntax, but you do not need to do anything, you can also use it to write an empty function >>> while True :... pass # busy-wait for keyb Oard interrupt (Ctrl + c )... ^ CTraceback (most recent call last): File "<stdin>", line 1, in <module> KeyboardInterrupt> class myEmptyClass :... pass...> def initlog (* args ):... pass # Remember to implement this... 6. Define a function. We can write a function to generate a Fibonacci Series> def fib (n ):... "used to print fibonacci sequence which is less than n """... a, B = 0, 1... while a <n :... print ,... a, B = B, a + B...> fib (2 000) 0 1 1 2 3 5 8 13 21 34 55 144 233 377 610 987 1597def is used to define a function, followed by the function name and parameter list, and the function body must be indented. The document information of the function can be added to the first line of the function body, called docstring, which facilitates the extraction of function information in the future. >>> Print fib. _ doc _ Used to print fibonacci sequence which is less than n 7 programming style PEP 8 provides four spaces for the programming style wizard that most projects follow, do not use tab to separate functions and classes from 79 characters in the same line, and use larger blocks in the same function if possible, write the comments and the corresponding code in the same line. Use docstring to use spaces on both sides of the operator and semicolons, but there is no need to maintain the consistency of the function name and Class Name Style in the brackets: CamelCase, do not use special encoding for lower_case_with_underscore. ASCII encoding can work well in any situation

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.