python--if judgments, functions, and list derivation

Source: Internet
Author: User

1.if Judging Simple if judgment
If Exp:          
EXECUTE statement

Where exp can be an expression or an arbitrary element, when Exp is true, the code after the indentation is executed.

In Python, non-0 and non-null are both true (true), and only 0 and Null are False (FALSE).

if2>1:#at this point, the 2>1 is an expression that returns the true    Print('a')#because the expression 2>1 is true, print (' a ') is executed, output ' a 'if1&LT;2:#1<2 is an expression, but returns a false    Print('a')#so I won't execute this code .if1:Print('a')#since non-0 and non-null are true in Python, 1 is true and code is executedif0:Print('a')#0 is false, so do not execute this code
If...else ...
if Exp:        code1Else:       //code2

If...else ... is an upgrade version of If, meaning that when exp is true, execute code code1, if not true, execute code code2, so Code1 or code2, there must be 1 statements executed.

if 1<2: #因为1 <2 is true, execution code print (       1)print(1)  Else: 
    Print(2)
If...elif. else ...
score=85if exp1:        //code1
elif exp2: //code2...
elif ExpN: //coden
Else : //code

If...else ... Is the if of the enhanced version, meaning when the EXP1 is true when executing code CODE1, if not true, Judge Exp2, if EXP2 is true, execute code2, and so on, if all is not true, execute code.

Score=85if 90<=score<=100:        print (' A ') elif 80<=score<90:        print (' B ') elif 70< =score<80:        print (' C ') elif 60<=score<70: Print (' D ') Else: print (' E ') #由于score = 85, so 80<=score<90 is true, so output ' B '        
2. Functions
def func_name ([param]):         Function code Group

Where Func_name is the function name, we can customize, Param is passed to the function of the formal parameter, can not give, depending on function function, but the parentheses must have. When we call this function, we execute the function code group.

In Python, a function has a return value, and if the function does not explicitly return a value, the function returns a none type. When you need to use some code more than once, you can define the code in a function and then call it multiple times.

3. List-derived

The list derivation is the use of lists to create new lists. (There is also the dictionary derivation and the set deduction formula, the space to say ~)

is to iterate over a list with a For loop and then filter out eligible data into a new list with the IF condition

 for  in range [0,1,2,3,4,5,6,7,8,9] #利用range (10) to generate a new list
>>>[x for x in range (ten) if x%3==0] #利用range (10) and if condition (divisible by 3) to generate a new list [0,3,6,9]
>>>[[x,y] for x in range (2) for Y in range (2)] #x, Y uses range (2) to generate the respective elements to be combined [[0,0],[0,1],[1,0],[1,1]]

python--if judgments, functions, and list derivation

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.