Syntax and case of the IF condition statement for Python

Source: Internet
Author: User

1. Conditional statements

Indent with 4 spaces

If condition:

#条件成功,

Else

#条件不成功

If condition: {

#条件成功,

#条件成功,

}else{

#条件不成功,

#条件不成功,

}

The IF syntax is this or is used: or with {}

2. If for a single condition

[[email protected] python]#python3.5 if.py>>>>franksuccess[[email protected] python]#python3.5 if.py>>>>aaaerror[[email protected] python]#Cat if.py#!/usr/bin/env python#_*_ Coding:utf8 _*_#when there is input, wait forever until the user enters a value, the input value will be assigned to N,n on behalf of the user input contentN1= Input ('>>>>')ifN1 = ='Frank':      Print("Success")Else:      Print("Error"when defining a variable assignment, use an equals sign=The character is enclosed in double quotation marks to use the 2 equals sign when the If comparison, the character resulting single quotation marks.3If  statement [[email protected] python]#python3.5 if.py First>>>Franksecond>>>Yesfrank Niubi First judgment [[email protected] python]#python3.5 if.py First>>>Franksecond>>>Nnnnfrank Error A second judgment [[email protected] python]#python3.5 if.py First>>>Oooerror third judgment [[email protected] python]#Cat if.py#!/usr/bin/env python#_*_ Coding:utf8 _*_#when there is input, wait forever until the user enters a value, the input value will be assigned to N,n on behalf of the user input contentN1= Input ('first>>>')ifN1 = ='Frank': N2= Input ('second>>>')    ifN2 = ="Yes":        Print('Frank Niubi The first judgment of')    Else:        Print("Frank Error A second judgment")Else:    Print("error A third judgment")

4, multiple conditions of the IF condition judgment of the syntax, pass represents nothing to execute, Python can not be left blank, or error

If condition 1:

Pass

Elif Condition 2:

Pass

Elif Condition 3:

Pass

Else

Pass

5, and and OR

[[email protected] python]#python3.5 if2.pyUser name>>>Frank Password>>>frank123 input correct [[email protected] python]#python3.5 if2.pyUser name>>>Frank Password>>>aaerroe[[email protected] python]#Cat if2.py#!/usr/bin/env python#_*_ Coding:utf8 _*_#when there is input, wait forever until the user enters a value, the input value will be assigned to N,n on behalf of the user input contentN1= Input ('User name >>>') N2= Input ('Password >>>')ifN1 = ="Frank"  andN2 = ="frank123":                               Print('Enter the correct')Else:        Print('Erroe')

Or is not tested, anyway is and is 2 satisfied to be true, or is a casual satisfaction is true

6.if Judging if the number is even

[[email protected] python]#Cat if3.py#!/usr/bin/env python#_*_ Coding:utf8 _*_#when there is input, wait forever until the user enters a value, the input value will be assigned to N,n on behalf of the user input contenta=0 whileA < 20: Temp= A% 2iftemp = =0:PrintaElse:                                        Passa= A + 1[[email protected] python]#python if3.py024681012141618

Syntax and case of the IF condition statement for Python

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.