Python if else for while statement control [arrangement]

Source: Internet
Author: User

Python if else for while statement control [arrangement]
Python if else statement if statement is used to check a condition: if the condition is true (true), we run a statement block (You are an if block); otherwise (else ), we execute another statement block (called the else block ). The else substatement is optional. For example. py): number = 23 guess = int (input ('enter an integer: ') # Wait for the input integer if guess = number: print ('Congratulations, you guessed it. ') # Print the new block from here (' (but you have not received any prizes !) ') # Here, the new block ends elif guess <number: print ('no, you guess a little ') # Another block else: print (' No, you guessed a little big ') print ('complete') # After the if statement is executed, the final statement is always executed and output in the following three situations: enter an integer: 50 is incorrect. if you guess it is a little too big, enter an integer: 22 is incorrect. if you guess it is a little too small, enter an integer: 23. Congratulations, you guessed it. (But you have not received any prizes !) In this program, we get guesses from users and check whether this number is set. We set any integer we want for the variable number, such as 23. Then, we use the input () function to obtain the number of users' guesses. A function is a reusable program block. We will introduce more about them in later sections of this chapter. We provide a string for the built-in input function, which prints it to the screen and waits for user input. Once we enter something and press enter, the input () function returns our input as a string. Then, we use int to convert the string to an integer and store it in the variable guess. In fact, int Is a class, but now all you need to know is that you can use it to convert a string into an integer (assume that the string in the text contains a valid integer ). Next, we will compare the number of user guesses with the number we selected. If they are equal, we will print a successful message. Note: The indentation level is used to tell which block the Python statement belongs. This is why indentation P is so important in ython. I want you to stick to the "consistent indent" rule, OK? Note that the if statement has a colon at the end, indicating that a statement block in Python will follow it. Then, we check whether the number of guesses is smaller than this number. If yes, we notify users that the number of guesses must be slightly higher than that. Here we use the "elif" clause. In fact, we combine two related if else-if else statements into a statement if-elif-else, this makes the program simpler and reduces the indentation required. The elif and else statements must also have a colon at the end of the logical line, followed by the corresponding statement block (by appropriate indentation of course ). You can include another if statement in the if block of the if statement-this is called if statement nesting. Remember, the elif and else sections are optional. A minimum valid if statement is: python if True: print (yes, it is True). The complete if statement and related elif and else clauses are executed in Python, it moves to the next statement block of the if statement. In this example, it is the main block (where the program starts to execute), and the next statement is print (complete ). After that, Python will see the end of the program and complete it simply. Although this is a very simple program, you should note that I have pointed out many things. All of these are fairly straightforward (for those with a c/c ++ background, they are incredibly simple ). At first, you need to realize all these things, but after some exercises, you will feel comfortable with them, and naturally it will be all you feel. About the for loop control statement in Python # First: Evaluate the prime number between 50 and 100 import mathfor I in range (50,100 + 1): for j in range (2, int (math. sqrt (I) + 1): if I % j = 0: break else: print I # Second: place the else in the same indentation as if. Import mathfor I in range (50,100 + 1): for j in range (2, int (math. sqrt (I) + 1): if I % j = 0: break else: print I # Third: Add a break statement after else. Import mathfor I in range (50,100 + 1): for j in range (2, int (math. sqrt (I) + 1): if I % j = 0: break else: print I break ### idea ### for statement is a loop control statement in python. It can be used to traverse an object and has an optional else block that is used to process the break statements contained in the for statement. If the for loop is not terminated by break, the statement in the else block is executed. When necessary, break terminates the for loop. continue skips the statement located after it and starts the next loop. The format of the for statement is as follows: >>>> for <> in <object set> :... if <condition> :... break... if <condition> :... continue... <other statements>... else :... <>... # For the first program here, I will explain why the math module is imported: The math module is imported to the initiator. If you import the math module and then open the I, you can reduce the number of operations. Determines whether a number is a prime number. You only need to perform this operation on it: divide the number n, the cycle with the square of the 2 to the n, if all integers in this range cannot be divisible by n, then n is the prime number. In this way, the time consumed for the operation between the square that is greater than n and the square that is less than n is saved. Second, let me explain that '+ 1': int (math. sqrt (I) outputs the largest integer smaller than I's square. For example, math. sqrt (51) results are larger than 7, while int (math. sqrt (51) Outputs 7. In range (m, n), the range () function generates a list of integers from m to n-1, therefore, '+ 1' is required to complete the operation. By the way, the range () function. Range ([start,] stop [, step]) # start is an optional parameter, Starting number # stop termination number. If range has only one parameter x, A list of integers containing 0 to X-1 is generated # step optional parameter, step # the second program else that line is wrong, if else is placed in that place, once a certain number cannot be divided into its own number, it will output I, directly find a number whose division is equal to 0. In this way, this number is output continuously. For example, if I = 77, it is not a prime number, but it will output 77 consecutive times. Do you understand? However, you just don't understand how else runs when it and if are in the same indent. You have explained it in detail, but you can't say it in a word. Moreover, I must think that drawing is a very good way to understand the loop.

Related Article

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.