Python learning notes-Example of if statement usage, python learning notes

Source: Internet
Author: User

Python learning notes-Example of if statement usage, python learning notes

Preface

We have used the Condition Statement several times in actual development. Here we need to introduce it again with great attention. We will not talk much about it below. Let's take a look at the detailed introduction.

If statement

As the name suggests, this statement is a judgment statement. Let's take a simple example.

Cars = ['audi ', 'bmw', 'subaru', toyota] for car in cars: if car = 'audi ': print (car. upper) else: print (car. title) # --> AUDI Bmw Subaru Toyota # It can be seen that the first element is all capitalized, while other elements are only capitalized.

Conditional Test

The core of each if statement is True or False.

Check whether the two elements are completely equal.

Car = 'bmw 'print (car = 'bmw') # --> True check whether print (car = 'bmw ') is completely equal # --> False is case sensitive, therefore, print (car. title () = 'bmw ') # --> True: This will be equal. The first letter of the car should be capitalized for the title ().

Check whether two elements do not want to wait.

car = 'bmw'print(car != 'audi')#-->True 

Compare two numbers:

Age = 18 print (age = 18) # --> True equals to print (age! = 18) # --> False is not equal to print (age = 30) # --> False is equal to print (age <30) # --> True is less than print (age <= 30) # --> True is less than or equal to print (age> 30) # --> False is greater than print (age> = 30) # --> False is greater than or equal

Check

Age_0 = 18age_1 = 30 print (age_0 = 18 and age_1 = 30) # --> True both values are Trueprint (age_0! = 18 and age_1 = 30) # --> a Flase statement returns True, and a Flase statement returns Falseprint (age_0! = 18 and age_1! = 30) # --> Flase both judgments are Falseprint (age_0 = 18 or age_1 = 30) # --> True both judgments are Trueprint (age_0! = 18 or age_1 = 30) # --> True: Both True and Falseprint (age_0! = 18 and age_1! = 30) # --> Flase both judgments are False # conclusion: # and: both sides must be True. True # or: True if one side is True.

Determine whether a specific value is included in the list:

age=[12,13,14,15,16,17]print(12 in age)#-->Trueprint(0 in age)#-->False

Determine whether a specific value package is not included in the list:

age=[12,13,14,15,16,17]print(12 not in age)#-->Falseprint(0 not in age)#-->True

Bool expression

isShow=TrueisGood=Falseprint(isShow)#-->Trueprint(isGood)#-->False

Combined judgment:

Car = 'bmw 'if car = 'bmw': print ("Good") # --> Good else: # Run the following non-statement car = 'bmw ', that is, car! = 'Bmw 'print ("Bad") if car = 'audi': print ("Good") else: # Run the following non-statement car = 'bmw ', that is, car! = 'Bmw 'print ("Bad") # --> Bad

If statement

(1) if

Sample Code

isShow=Trueif isShow: print("It's showing")#-->It's showing

(2) if-else

Sample Code

isShow=Falseif isShow: print("It's showing")else: print("It's not showing")#-->It's not howing

(3) if-elif-else

Sample Code

Age = 18if age <22: print ("You cannot get married") elif age <30: print ("You are not married yet") else: print ("single ") # --> you cannot get married
Age = 28if age <22: print ("You cannot get married") elif age <30: # age not met <22 but age <30 print ("You are not married yet") else: print ("single") # --> you are not married yet
Age = 50if age <22: print ("You cannot get married") elif age <30: print ("You are not married yet") else: # age <22 and age <30 print ("single") # --> single
# Sometimes it is clearer to replace else with elif: if age <22: print ("You cannot get married") elif age <30: print ("You are not married yet ") elif age> = 30: print ("single ")

Use if to process the list

Request_toppings = ['mushrooms', 'extra chees'] topings_none = ['mushrooms'] for logs in request_toppings: if logs in topings_none: print ('no such pizza') else: print ("Adding" + request_topping + ". ") print (" Finish making your pizza! ") # --> No such pizza # --> Adding extra cheese. # --> Finish making your pizza!

Confirm that the list is not empty:

Request_toppings = [] if request_toppings: # if the list contains at least one element, True is returned. Otherwise, False print ('have ') else: print ('not Have') is returned ') # --> Not Have

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

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.