Analysis of conditions and cyclic control of Python3 Foundation _python

Source: Internet
Author: User
Tags case statement

This paper illustrates the PYTHON3 condition and circulation control statement and its usage, which is the important knowledge that Python must master, and is now shared for everyone's reference. Specifically as follows:

Generally speaking, Python's process Control statements include: if conditional statements, while loop statements, for loop statements, range functions, and break, continue, pass control statements. The semantics of these statements in Python are essentially the same as those in other languages, so here's how they are used.

One, if statement

If statements are the most commonly used conditional control statements, the general form in Python is:

If condition one:
 statements
elif condition Two:
 statements
else:
 statements

Python uses elif instead of else if, so the keyword for the IF statement is: If–elif–else.

Attention:

1. After each condition, use a colon (:) to indicate the block of statements to be executed after the condition is met.
2, the use of indentation to divide the statement block, the same number of indented statements together to form a statement block.
3, there is no switch–case statement in Python.

The sample code is as follows:

x = Int (input ("Please enter an integer:"))
if x < 0:
 print (' Negative. '
) elif x = = 0:
 print (' Zero. ')
else:
 print (' Positive. ')

Two, while statements

The general form of a while statement in Python:

While judgment condition:
  statements

Also pay attention to colons and indents. In addition, there is no do in Python. While loop.

The sample code is as follows:

A, b = 0, 1 while
B < 10: # loop output Fibonacci sequence
 print (b)
 A, B = B, a+b

Third, for statement

The For statement in Python is somewhat different from the for statement in C language: The For statement in the C language allows the user to customize the iteration step and the termination condition, while the Python for statement iterates through any sequence (sequence) and iterates through the order in which the elements appear in the sequence. The general form is:

For variable in sequence:
 statements
else:
 statements

The sample code is as follows:

words = [' cat ', ' love ', ' apple ', ' python ', ' friends '] for the
item in words:
 print (item, len (item))

If you need to modify the sequence of your iteration in the loop, you'd better make a copy, and the slicing tag is useful:

words = [' cat ', ' love ', ' apple ', ' python ', ' friends '] for the
item in words[:]:  # Make a slice copy of the entire list
 if Len (item) >= 6:
 Words.insert (0, item)
print (words)

We notice that the ELSE clause can also be used in the loop statement, as mentioned in the 5th below.

Four, Range function

If you want to traverse a sequence of numbers, the built-in range () function can be useful. The function range () is often used in a for loop to produce an arithmetic sequence:

>>> list (range)  # Default starting from 0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list (range (1, 11)) # from 1 to 11  , the front closed and then opened
[1, 2, 3, 4, 5, 6, 7, 8, 9, ten]
>>> list (range (0, 30, 5)) # 5 indicates step size, every 5 takes a number
[0, 5, 10, 15, 20, 25]

The sample code is as follows:

For I in range (2, one):
 print (i)

V, break, continue, pass and ELSE clause

①.break

The break statement, like the C language, jumps out of the most recent for or while loop.

②.continue

The continue statement is also borrowed from the C language, which terminates the current iteration and loops through the next iteration.

③.pass

The pass statement does nothing, it only needs a statement syntactically, but the program does not require any action. The pass statement is designed to preserve the integrity of the program's structure.

④.else clause

You can also use the ELSE clause in a loop statement, or the ELSE clause executes when the sequence traversal ends (for statement) or when the loop condition is a false (while statement), but the loop is terminated by a break. As shown below:

# Loop End Execution Else clause for
I in range (2, one):
 print (i)
else:
 print (' for statement are over. ')

# The ELSE clause is not executed for the
I in range (5):
 if (i = = 4): Break
 ;
 else:
 print (i)
else:
 print (' For statement are over ') # does not output

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.