The conditional loop statement for Python basics

Source: Internet
Author: User

The first two articles are about data types and data operations, and this article is about conditional statements and loop statements.

0x00. Conditional statements

A conditional statement determines the code block that executes by executing the result of one or more statements (true or false).

You can easily understand the execution of conditional statements by:

Python interprets Non-zero values as True . and are None interpreted as 0 False .

Python determines that a value other than 0 is True, and none and 0 are considered False. Note that the true and false initials are capitalized, and Python is case sensitive.

The basic form of a conditional statement is:

If judgment condition:    Execute statement ... else:    EXECUTE statement ....
When judging a condition as multiple values, you can use the following form:

If judgment condition 1:    EXECUTE statement 1......elif judgment Condition 2:    EXECUTE statement 2......elif judgment Condition 3: Execute statement    3......else:    EXECUTE statement 4 ...

0x01. Looping statements

When we need to execute a statement or group of statements multiple times, it is not possible to write the same statement more than once, one is more cumbersome, and the other is not conducive to maintenance, when the circular statement came into being. Where the loop statement is divided for the for loop and while loop.

For Loop

A For loop can traverse any sequence of items, such as a list or a string. The process is:

The syntax format for the For loop is as follows:

For Iterating_var in sequence:   statements (s)
While loop

The while statement is used to loop the execution of a program that, under certain conditions, loops through a program to handle the same tasks that require repeated processing. The process is:

The syntax format for the while loop is as follows:
While judging condition:    Execute statement ...
0x02. Example
The following instance uses the prime number between the nested loop output 2~100:
#!/usr/bin/python#-*-Coding:utf-8-*-i = 2while (i <):   j = 2 while   (J <= (i/j)):      if Not (I%J): Break      J = j + 1   if (J > i/j): Print I, "is prime"   i = i + 1print "good bye!"
0x03. References
Variables and data types     https://www.programiz.com/python-programming/variables-datatypes
Conditional Statement Https://www.programiz.com/python-programming/if-elif-else
Loop statement Https://www.programiz.com/python-programming/for-loop
Https://www.programiz.com/python-programming/while-loop
W3cshool https://www.w3cschool.cn/python/python-tutorial.html

To be continued, the basic article to this end, the next, Python advanced object-oriented

The conditional loop statement for Python basics

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.