Get started with Python with C or C + + basics: Python Crash Course 5 if statement

Source: Internet
Author: User

This chapter is the Pyhon version of the IF statement. We all understand the principle, do not say a statement.

Notable two points:

1. The symbol ":" must be added at the end of each if class statement.

2. Notice whether indenting in Python represents the previous line of code.

Here are some pieces of code to analyze:

One, simple if statement:

1Requested_toppings = ['Mushrooms','Onions','Pineapple']2 if 'Mushrooms' inchrequested_toppings:3     Print('Yes')4 Else:5     Print("No")

Declares and assigns a list, with an If + in statement ( equivalent to an enumeration and compares list elements, equivalent to: for (int i = 0; i < n; ++i) if (a = = b) return ture; )

Two, If-else statement

Code explanation: If greater than and not equal to 18 years old pay ¥10, otherwise pay ¥5.

1 age =23if age <:4     print("  Your AdmissionCost is $")5Else:6     Print ("Your Admission COSRis $")

There should be no need to say more about C-base ... Same.

Three, If-elif-else statement

Code explanation: If less than and not equal to 4 years of age to pay ¥0, if more than 4 years of age and less than 18 years of age pay ¥5, otherwise pay ¥10.

1Age = 182 3 ifAge < 4:4     Print("yout Admission cost is $")5 elifAge < 18:6     Print("Your Admission cost is $")7 Else:8     Print("Your Admission COSR is $")

The only thing to note is that Python and Linux pay the shell the same, else if if is shrunk to elif.

Four, using the IF statement to process the list

  1. Check for special elements

  In the For loop with an if statement, the sample code is as follows:

1names = ['Peter','Mina','Mike']2 3  forNameinchnames:4     ifName = ='Peter':5         Print(Name.title () +"is the host!")    6     Else:7         Print(Name.title () +"is not the host")

2. Make sure the list is not empty

You know that if (x = = 0) The return is false. The same is true for empty. This is the use of this condition to judge.

The code is as follows:

Test = []if  test:    print("It is not empty")  Else:    print("It is empty")

3. Use multiple lists

In fact, it is: enumerate the total list and use the IF statement to determine whether the element is in a column table.

The code is as follows:

1names = ['Peter','Mina','Katherine','Mike']2 3names_in = ['Peter','Mina']4 5  forNameinchnames:6     ifNameinchnames_in:7         Print(Name.title () +"is in our lise!")8     Else:9         Print(Name.title () +"Is isn't in our list")

                              To be Continued ...

If there is any mistake, please comment!

Getting started with Python with C or C + +: Python Crash Course 5 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: 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.