A simple summary of the judgment statement and the circular statement in Python (attached example)

Source: Internet
Author: User
Tags sorts
This article brings the content is about Python in the judgment statement and the circular statement simple summary (with the example), has certain reference value, has the need friend can refer to, hoped to be helpful to you.

Today, I would like to introduce the If judgment and loop control in Python.
Originally written before the title name is "Python Process Control", but think, process control is not limited to conditional judgment and loop control. So, honestly, ... aha.

First, if condition judgment

1. Grammatical form

The conditional judgment syntax for if is as follows. According to the conditions of each branch to determine the corresponding execution action.

If < condition judgment -1>:    < operation -1>elif < conditional judgment -2>:    < operation -2>elif < conditional judgment -3>:    < Operation -3> else:    < operation -4>
    • If Branch: If the < condition judgment -1> judgment is true, then transferred to the execution < operation -1> otherwise transferred to ELIF statement;

    • elif: the abbreviation for "else if". The Elif statement can exist, or there can be one or more. When the branch is entered, if the judgment < condition is -2> true, the execution < operation -2> otherwise, the next Elif statement or else statement is transferred.

    • else: When all of the IF and elif branches above have gone, if all are judged to be false, then finally go to the Else branch, execute < operation -4>.

For example: value = x if x<y else y

2. A special form
If judgment also has a special form. as shown below.
It depends on the value of X to determine whether to execute. where x is a non-0 value, a non-empty string, a non-empty list, and so on, the judgment is true, then the execution <actions>

If x:    <actions>

Second, for Loop

Before the introduction of data types, there are introduced to this part, so simply make a summary!
As follows:

For x in iteration sequence:    <actions>
    • The loop statement, X, as a temporary variable, iterates through each element of an iterative object (string, list, tuple, and so on) in turn.

As a simple example, how can I print out the elements when there are different types of elements in the list data type X?

The_count = [1,2,3,4,5]                           #%dfruits = [' Apples ', ' oranges ', ' pears ', ' apricots ']  #%schange = [1, ' Pennies ', 2, ' Dimes ', 3, ' quarters ']     # mixed list:%rfor number in The_count:     print ("It's count%d"% number) for fruit in fruit S:     print ("A fruit of type:%s"% fruit) # mixed Lists:notice We have to use%r since we don ' t know what's in itfor I in the change:  #%r    print ("I got%r"% i)

"Supplementary" Knowledge points AH ~ ~ is actually suddenly remembered by oneself ^_^

    • Enumerate ()
      Takes the index and value of the sequence out. Just go to the example to illustrate it ~

>>> L = [' A ', ' B ', ' C ']>>> for IND, Val in enumerate (l):        print ("%d%s"% (Ind,val)) ... 0 A1 B2 C
    • Sorted ()
      form: sorted ([Sequence],key= function, reverse=true or False)
      Parse: receives a sequence and sorts it. You can also sort by the specified key form. Parameter reverse is the direction of the order, true when it takes effect.
      For example:

# sort the list. >>> sorted ([36,5,-12,9,-21]) [-21,-12, 5, 9, 36]# Higher order function # You can receive a key function to implement a custom sort, for example, sort by absolute size:>>> sorted ([36,5,-12,9,-21],key=abs) [5, 9,-12,-21, 36]# sorts the strings by default, sorted by the ASCII size of the first letter. >>> sorted ([' Bob ', ' about ', ' zoo ', ' credits ']) [' Credits ', ' Zoo ', ' about ', ' Bob ']# sort by ignoring the case: (All lowercase) >>> Sorted ([' Bob ', ' about ', ' zoo ', ' credits '], key=str.lower) [' About ', ' Bob ', ' credits ', ' Zoo ']# ignore case, and sort in direction:>>> Sorted ([' Bob ', ' about ', ' zoo ', ' credits '], key=str.lower,reverse= True) [' Zoo ', ' credits ', ' Bob ', ' about ']

Third, while loop

    • Grammatical form:

While <expression>:    <actions>
    • Parsing: As long as the condition <expression> satisfies, the loop body <actions> is continuously executed, and the loop is exited until the condition is not satisfied.

For example:

i = 0numbers = []while i < 6:    print ("At the top I was%d"% i)    numbers.append (i)      i = i + 1    print ("Numb ERS now: ", numbers)    print (" At the bottom I was%d "%i)    # when i=6 exits loop print (" The Numbers: ")

Iv. break

The break keyword, whose function is to exit prematurely when the layer loops.
For example:

While x<10:    if < Judge -1>: #例如 x==3        break    x + = 1
    • When < determines that -1> is true, the break statement is executed to exit the loop.

Wu, continue

Continue keyword, its role is in the loop process, you can pass the continue statement, immediately skip this cycle, back to the top of the loop, directly start the next cycle.
For example:

while x<10:if < judgment -1>: #例如 x==3 break Print (x) x + = 1 
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.