Python six-core programming-conditions and loops

Source: Internet
Author: User
Tags iterable

1.if Declaration

Single If by using a Boolean operator that declares and, or, and not.

If-elif-else. Elif that is else if

If expression1:
Expr1_true_suite
Elif expression2:
Expr2_true_suite
Else
None_of_the_above_suite


2.while statements

While expression:
Suite_to_repeat

The while statement has an optional ELSE clause. That

While expression:
Suite_to_repeat

Else

Expression

3.for statements

For Iter_var in iterable:
Suite_to_repeat

The For statement has an optional ELSE clause. That

For Iter_var in iterable:
Suite_to_repeat

Else

Expression


4.range () built-in functions

Range (start, end, step =1) The default step is 1

>>> Range (2, 19, 3)
[2, 5, 8, 11, 14, 17]

>>> Range (3, 7)
[3, 4, 5, 6]


Range () has two abbreviated syntax formats:
Range (end)
Range (start, end)


5.break and Continue,pass statements

The break statement can end the current loop and then jump to the next statement

Pass is an empty statement


6. Iterators and ITER () functions

>>> mytuple = (123, ' xyz ', 45.67)
>>> i = iter (mytuple)
>>> I.next ()
123
>>>


7. List resolution

[Expr for Iter_var in iterable]

List parsing also provides the syntax for an extended version number:

[Expr for Iter_var in iterable if COND_EXPR]

The core of this statement is the for loop, which iterates through all the entries of the Iterable object. The preceding expr is applied to each member of the sequence, and the final result value is the list produced by the expression. Iterative variables do not have to be part of an expression.

>>> [x * * 2 for X in range (6)]
[0, 1, 4, 9, 16, 25]

>>> seq = [11, 10, 9, 9, 10, 10, 9, 8, 23, 9, 7, 18, 12, 11, 12]

>>> [x for x in seq if x% 2]
[11, 9, 9, 9, 23, 9, 7, 11]


8

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Python six-core programming-conditions and loops

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.