After learning Python yesterday, it was really neat and easy to learn. In yesterday's study we learned about the use of the while loop and the If...else statement in Python, and the use of the while statement is this:while Express:, remember not to forget the colon. The IF statement and the use of the while statement are similar. Today we start with yesterday's study, understand the for loop in Python, break statement and continue statement, and then simply touch the use of the function. Needless to say, I think the learning of programming sentences is best to learn from the code, so let's take a look at the following code:
1 forIinchRange (1,10,2):#use format for a for loop2 Print(i)3 ifi = = 7 :4 Break5 Else:6 Print("The For loop is over!")#This statement is printed at the end of the for loop, unless you encounter a break
In the first line of the code above, we see the use of the For loop in use. The format is: for ... in.. : First, explain the function of the first line of code: We declare a variable I, his value is determined by the rang () function, which indicates that I from 1 to 10 end, its step is 2, but not including 10. It's kind of like we're using a for loop in Java like this: 1 for (int i = 1;i < 10;i+=2) . Then enter the loop body and print out each I value, but when i=7, jump out of the loop. One thing to note here is that if you terminate from a for or while loop, then any corresponding loop else blocks will not be executed, that is, when the loop is out, the sixth row will not be executed. The results are as follows:
If we comment out the 3rd, 4 lines using #, the result is as follows:
In addition to remembering the different results of the break statement as described above, it should also be remembered that the else part is optional and, if the Else statement is included, it is always executed once at the end of the loop. Let's look at the continue statement, the continue statement in Python, and other languages (such as java,c ...). The Continue statement function is the same, so it's good to understand. It does this by skipping the remaining statements in the current loop block and then continuing with the next round of loops. Take a look at the following code:
1 whileTrue:2s = input (" "Enter something:" ")3 ifs = ='Q':4 Break5 ifLen (s) < 3:6 Print("string length too short.try again.")7 Continue8 Print("The string length is:", Len (s))#Len () is used to find the length of S9 Print("done!")
In the above code, the user is prompted to type something, if you type the character ' Q ', then will jump out of the loop, if not, then the user will determine the length of the string, if the length is less than 3, the user will be prompted and end the loop, will not print out the length of the string, if the length is greater than 3 Then the user will get the length of the string. The results of the operation are as follows:
Here we have learned about the three control flows in Python: if,while,for, and the break and continue statements associated with them. They need to be mastered not only in other languages, but also in Python. You do not need to memorize, and you are familiar with any of the language associated with the memory, remember that the difference is well mastered.
Let's start by understanding the functions in Python. define a function in Python using the keyword Def, followed by a function name, and then a pair of parentheses, which can have parameters in parentheses, and then end with a colon. first, a more complex code, its role is to convert the user input decimal number into a corresponding binary representation. If you do not understand this algorithm, you can see my previous blog, "Decimal to binary system." Here's the Python code:
1 #Defining Functions2 deftobinary (number):3n = number% 24 ifNumber >= 2:5Tobinary (number >> 1)6out = Chr (Ord ('0')+N)7 #chr () converts the ASCII code to the corresponding character8 #Ord () converts the character to the corresponding ASCII code9 Print("%c"% (out), end="')#End Control Print intervalTen One whileTrue: As = input (" "Enter A number: (' Q ' to quit)" ") - ifs = ='Q': - Break the elifs.isdigit (): - tobinary (int (s)) - Else: - Print("your input is not correct!") + Print(end ='\ n') - + Print('Game over!')
In the second line of the above code we define a function tobinary with the keyword Def, which takes a parameter. The function is to print out the binary number of this parameter. See note for the Chr () and Ord () functions, as well as the end in print (). Next go into the while loop, we determine whether the user input is ' q ', and if so, exit the program. If not, we can judge whether the string entered by the user is a number (using the IsDigit () function in string), and if it is a number, then enter the function we defined and print the binary representation. If the user input is not a number, the user is prompted for an illegal entry. The results of the operation are as follows:
The code above demonstrates how to define and use a function. is still relatively simple. Let's talk about the function parameters. In Python, we not only have the default values for the parameters, we can also specify key parameters. The so-called default parameter value is that when we call the function, we can not assign a value to it, and let it use the default value we set. Take a look at the following code:
1 def Say (message,times = 1):2 print(message * times )3 4 Say ('Hello,')5 Say ('Hello, ', 5)
In the above function, we set the Times default value to 1, when we call the function in line fourth, we do not pass the time value, so Python uses the default value we set to 1, and on line fifth, we set the times=5, the result is as follows:
For the key parameter, however, it means that if we define a function that has more than one parameter and we want to specify only a subset of them, we can assign values to those parameters by naming them. The advantage of this is that we don't have to worry about the order of the parameters, and assuming that the other parameters are using default values, we can only assign values to the parameters we want. Take a look at the following code:
1 def func (A,b=5,c=10 2 print ( ' a is ' , A,
In the above function, we set the default value for B,c, and A is not set. In the code line 4th, we passed two parameters, parameter a will get 3,b will get 7, parameter C uses the default value 10; In line 5th, the variable a gets 25,b using the default value 5,c the position of the argument, and the 6th line of code, we use the key parameter to fully specify the parameter value, Note: Although in the function definition, a is defined before C, we can still specify the value of the parameter C before a. This is the key parameter designation. The results of the operation are as follows:
Finally, say a word about the document string docstrings. It makes it easier to understand the documentation that helps our program, so use it as much as possible. Take a look at the following code:
1 defPrintmax (x, y):2 " "Prints The maximum of the numbers.3 4 The values must be integers." "5x =int (x)6y =Int (y)7 8 ifX >y:9 Print(X,'is maximum')Ten Else: One Print(Y,'is maximum') A -Printmax (10,20) - Print(Printmax.__doc__)
The string in the first logical line of the function above is the document string for this function. We run the results and can get the following results:
That's what I learned today. Welcome to Bo Friends A lot of guidance.
Python Notes (ii)