Python if for else elif while break loop pass control flow tool __python

Source: Internet
Author: User
Tags iterable


If statement:
1, if ... elif elif ... else, where else is optional, if and elif have the same indentation, at the end of a line there is a colon

For statement:
2. Unlike C and Pascal, Python's for statement iterates through all the item in the sequence by item

>>> # Measure Some strings:
... words = [' cat ', ' window ', ' defenestrate '] >>> for
W in Words:
  ...     Print (W, Len (w))
...
Cat 3
window 6
defenestrate 12

3. If you need to modify the sequence you are traversing, it is recommended that you first make a copy of the sequence
>>> for W in words[:]:  # Loop over a slice copy of the entire list.
...     If Len (w) > 6:
...         Words.insert (0, W)
...
>>> words
[' defenestrate ', ' Cat ', ' window ', ' defenestrate ']

4, Range () function

>>> for i in Range (5):
.     . Print (i) ...
0
1
2
3
4

The given endpoint end point is never part of the resulting sequence, and range (10) produces 0~9

5, you can specify the starting number of the sequence, tolerance defaults to 1, you can also specify arithmetic progression tolerance
Range (5, 10)
5 through 9

Range (0, 10, 3)
0, 3, 6, 9.

Range (-10,-100,-30)
-10,-40,-70

6, in order to traverse the sequence of the index, you can combine the range () and Len () function:
>>> a = [' Mary ', ' had ', ' a ', ' little ', ' lamb ']
>>> for-I in range (Len (a)):
...     Print (I, a[i])
...
0 Mary
1 had
2 a
3 little
4 lamb

7, Range () return is not a list, but an object, when you traverse it, he will be in order to return to save the space. So when you enter the following statement, there is a strange result:
>>> Print (range (10))
Range (0,10)

8, we say an object is a iterable, that is, an object that needs to get the continuous item to the last function. We see that the for statement is a iterator, and the function list () is another one that is created from iterable lists
>>> List (range (5))
[0, 1, 2, 3, 4]

9. In Python, the ELSE clause is used in conjunction with a loop statement, or a try statement. The front is used to indicate that no break execution is used with a try statement, indicating that no exception occurred
An algorithm to find prime numbers:
>>> for N in range (2):
...     For x in range (2, N):
....         If n% x = = 0:
...             Print (n, ' equals ', X, ' * ', n//x)
...             Break
...     else: ...         # loop fell through without finding a factor
...         Print (n, ' is a prime number ')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 are a prime number
6 equals 2 * 3
7 is a Prime number
8 equals 2 * 4
9 equals 3 * 3

10, the use of the Continue clause is exactly the same as in the C statement

11, the Pass statement does nothing, for a block in syntax requires a statement, but there is no need to perform any action
>>> while True:
...     Pass  # busy-wait for keyboard interrupt (CTRL + C) ...

12, pass can also be used as a placeholder, allowing you to keep thinking more abstract level
>>> def initlog (*args):
...     Pass   # Remember to implement this!
...

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.