Python essay 4 (if statement)

Source: Internet
Author: User

4.1 Test checks for equality of items

The simplest condition test checks whether the value of a variable is equal to a specific value.

' Audi ' ' Audi ' True

The (= =) operator Returns true if the values on either side of it are equal, otherwise false.

Do not consider case when checking for equality

Case-sensitive when checking for equality in Python.

' Audi '>>>car = ='Audi'False

If the case is not important, just want to check the value of the variable, you can convert the value of the variable to lowercase, in comparison:

' Audi ' ' Audi ' True

The function lower () does not modify the value stored in the variable car. (The method of converting to uppercase is upper ())

Check for unequal

To determine whether two values are unequal, use (! =)

Check multiple conditions

Use and to check for multiple conditions: When all two conditions are true, the entire expression is true, one is false, and the entire expression is false.

Check multiple conditions with or: Two conditions have a true, and the expression is true.

Check if a specific value is included in the list

To determine whether a particular value is already included in the list, use the keyword in.

>>>items = ['a','b','c'  ]>>>'a' in items True>>> ' D ' inch Itemsfalse
Check if a specific value is not included in the list

You can use not in.

Banned_users = ['a','b','C']user='D'ifUser not inchbanned_users:Print(User +'Not in the list')
4.2 If statement

A simple if statement has only one test and one operation

Age = Allif age >=:     print("enough to vote! ")

In the IF statement, the indentation is the same as in the For loop.

If-else statements
Age = Allif age >=:     print("enough to vote! " )else:     print("too Young to vote! ")
If-elif-else statements

For example, a playground charged according to age, 4 years old and under, 4-18 years old, 5 yuan, 18 years old and over 10 yuan.

Age = Theif age < 4:    print(' cost 'is0. " )elif Age <:    print(' cost 'is5. " )else:    print("Cost is". ")
Using multiple Elif code blocks

Can use more than Elif code block, if the amusement park to give the elderly discount, for the elderly over 65 years old can be half price.

Age = Theif age < 4:    = 0elif Age <    := 5elif Age <     := tenelse:    = 5
Test multiple conditions

Sometimes you have to check all the conditions you care about.

name = ['Aaaz','Wf']if 'Aaaz' inchName:Print('Adding Aaaz')if 'PPP' inchName:Print('Adding PPP')if 'Wf' inchName:Print('Adding Wf')

Python essay 4 (if statement)

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.