The seventh lesson of Python Learning--basic conditional statement if

Source: Internet
Author: User
Tags ranges

    • Conditional statements

If we want to respond effectively to the user's input, the code needs to be able to judge. Can let the program to judge the structure becomes the condition, the condition Judgment statement returns the Boolean value TRUE or FALSE, actually executes one line, false executes another line

Note If the IF judge what, otherwise do not do any action must write a pass or will error

such as :

# !/usr/bin/env python # -*-coding:utf-8-*-a=3if a>2:    print("3>2"  )else:    Pass

    • If condition judgment statement

If so, how else?

= = usage (if equal to execution of first line)

such as:

# !/usr/bin/env python # -*-coding:utf-8-*-a=input ("Please enter user name")if a=="Luqiang":    print ("Luqiang username correct")  Else:    print ("User name error")

as listed above, if the user input is Luqiang then Judge a variable equals luqiang is correct, print the first line, if the user input is not Luqiang then it's wrong to execute the second line

if so, how else?

! = Usage (if not equal to execution of first line)

such as:

# !/usr/bin/env python # -*-coding:utf-8-*-a=input ("Enter user name")if a!="Luqiang":    print ("User name error") Else :    print ("User name is correct")

as listed above,! = (if not equal to the meaning), that is, the user input if not equal to Luqiang to execute the first line user name error , the user input if equal to Luqiang to execute the second line user name is correct

Summary := = with! = The judging condition is the opposite (emphasis)

    • usage of and

(It is to add a judgment condition, and before and after the first line must be in line with the judgment condition, even if there is a judgment condition does not conform to execute the second line)

# !/usr/bin/env python # -*-coding:utf-8-*-a=input ("Please enter user name") b= input("Please enter a verification code")if a== "Luqiang" and n2== "123":    print ("username correct")else:    print ("User name or CAPTCHA error")

    • or(or)

the use of or ( also adds a judgment condition, or or the meaning, that is, before and after the condition to determine that one conforms to the execution of the first line, otherwise execute the second line )

Such as:

# !/usr/bin/env python # -*-coding:utf-8-*-a= input("Please enter user name") b= input("Please enter a verification code")if a== "Luqiang"or n2== "123":    print ("username or captcha is correct")else:    print ("User name and Captcha error")

As listed above, that is, the user name and verification code to meet the criteria for the first line will be executed, there is a non-compliance with the conditions of judgment, will execute the second line

such as:

# !/usr/bin/env python # -*-coding:utf-8-*-a= input("Please enter user name") b= input("Please enter a verification code")if and n2== "123 ":    Print ("username or captcha is correct")else:    print ("User name and Captcha error")

as listed above,or OR, that is, two to judge one of them in line with the judgment, the first line is executed, of course, two are in line with the implementation of the first line, two are not in line with the implementation of the second line

    • < (less than) > (greater than) = (equals) multi-way judgment

Set multiple ranges in the judgment, different ranges to perform different lines

Such as:

# !/usr/bin/env python # -*-coding:utf-8-*-a=25if a<=10:    Print ("Child")elif and A<=18 :    print ("Teen")elif and a<=30:    print ("The Bucket Up")Else  :    print ("You are mature")

1-10 years old is: child

  More than 10 to 20 years old is: Green less years

  20 years old to 30 years old is: the bucket.

  Over 30 years old: you're mature.

The seventh lesson of Python Learning--basic conditional statement if

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.