Python note four (conditional judgment/cyclic/break and Continue)

Source: Internet
Author: User

A conditional judgment

  

if < condition judgment 1>:    < execute 1>elif < condition 2>    :< execute 2>elif < condition judgment 3>:    < execute 3>else:    < Execute 4>

ifThe judging conditions can also be abbreviated, such as writing:

if x:    print(‘True‘)

As long as a non- x 0 value, a non-empty string, a non-empty list, etc., it is judged True , otherwise False .

Two cycles

For...in cycle

# List of loops names = ['Michael'Bob'Tracy  ' for in names:    print(name)  # for-in range (5):     print(i)  

While loop

While loop, as long as the condition is satisfied, it keeps looping and exits the loop when the condition is not satisfied. For example, we want to calculate the sum of all the odd numbers within 100, which can be implemented with a while loop:

sum == while n > 0:    = sum + n    = n-2print(sum)

Continue

Jump out of this loop and execute the next loop.

Break

Jump out of the entire loop body.

Pay special attention to not abusing break and continue statements. breakand continue will cause the code to perform logical fork too much, error prone.

Python note four (conditional judgment/cyclic/break and Continue)

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.