Python Process Control Instance Code

Source: Internet
Author: User
First, the If-else condition statement is introduced. The IF statement is used to execute a specific block of programs based on the authenticity of the expression and the procedure of the control program. Use the same language as Java. For else if, there is a shorthand for elif.
For example:
Copy CodeThe code is as follows:


If x > 3:
Print ("Greater")
elif x = = 3:
Print ("EQ")
Else
Print ("small")


Next, the while statement is introduced. The function of the while statement is to repeatedly execute a particular block of blocks when the conditional expression is true.
First look at a sample program and then give instructions:
Copy CodeThe code is as follows:


x = Int (Input ("Enter a Integer:"))
While x! =-1:
Print (x)
x = Int (input ("Next Number:"))
Else
Print ("End")
Print (' over ')


In this code, as long as the value of x is not equal to-1, it is executed repeatedly. In contrast to C/c++/java, the more special one is the else statement. Here, else is an optional statement. When the conditional expression is false, the program block under the Else statement is executed after jumping out of the while loop.
Finally, look at the for loop. For: In is another type of looping statement in Python. The main purpose is to iterate through the sequence of objects. Here's how to use it:
Copy CodeThe code is as follows:


For x in range (1,5):
Print (x)
Print (' over ')


For loop statements, you need to have a way out of the current loop and jump out of the loop, in Python, using the continue and break statements. For the use of these two grammars, the same c/c++/java, so it is not much to retell.
With these three methods above, you can complete all the process control work in Python!
  • 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.