Python basics-condition judgment and python basics judgment

Source: Internet
Author: User

Python basics-condition judgment and python basics judgment

  Python version: 3.6.2 Operating System: Windows Author: SmallWZQ

So far, the programs in the basic Python series are executed in a single statement sequence. In this chapter, I will focus on the method for the program to select whether to execute the statement block.

Why can a Python program implement the self-selection function? It can determine whether to execute the following statement block based on the conditions.

Python basics-the Boolean values mentioned in data types are bound with condition judgment.

Condition judgment Statement block 1.1

What is a statement block?

A statement block is a group of statements that are executed or executed multiple times when the condition is true (Condition Statement. Place a space before the code to indent the statement to create a statement block.

Indentation:

You can also use the tab key to indent statement blocks. Python interprets a tab character as moving to the next tab character position, while a tab character position is 8 spaces, but the standard and recommended method is to use only spaces, especially when each indent requires four spaces.

In Python, the English version of colon (:) is used to mark the beginning of the statement block. Each statement in the block is indented (with the same indentation ). When it is rolled back to the same indentation volume as the closed block, it indicates that the current block is over (you don't have to worry about it. Many program editors and integrated development environments know how to indent the statement block, this helps you easily understand indentation )!!!

 1.2 condition and condition statements

The legendary if statement is coming soon ~~~

Do not enter the colon (:) when using the condition statement, and it must be in the English version.

1. if statement

For if statements, if the condition is true, the subsequent statement blocks will be executed. If the condition is determined to be false, the statement block will be skipped and will not be executed.

1 # if Statement 2 age = 203 if age> = print ('your age is ', age) 5 print ('adresult ')

2. else statement

The else clause only adds an option (the clause is called because it is not an independent statement but a part of the if Statement ). If the if statement is False, the statement block under the if statement is not executed, but the statement block under the else statement is removed.

1 # if... else... statement 2 age = 33 if age> = print ('your age is ', age) 5 print ('Adult') 6 else: 7 print ('your age is ', age) 8 print ('teenager ')

3. elif statement

If you need to check multiple conditions, you can use elif. It is short for else if and is also used in combination with else statements, that is, use elif for more detailed judgment:

1 # if... elif... elif... else... statement 2 age = 33 if age> = print ('Adult ') 5 elif age> = print ('teenager') 7 else: 8 print ('kid ')

4. Summary of condition statements

If <condition judgment 1>: <execution 1> elif <condition Judgment 2>: <execution 2> elif <condition judgment 3 >:< execution 3> else: <execution 4>

5. Notes

IfStatement execution has the following characteristics: it is judged from top to bottom, if it isTrue, Ignore the remainingelifAndElse statementSo, test and explain why the following program printsteenager:

1 # features of conditional statement execution: Judging from top down 2 age = 203 if age> = print ('teenager ') 5 elif age> = print ('adresult') 7 else: 8 print ('kid ')

6. input () Application

Finally, let's look at a problematic condition judgment. Used by many studentsinput()Read user input, so that you can input your own data. The program is more interesting and user-friendly. In fact, errors often appear in unexpected places. Believe it or not, let's look at the code segment:

# In the if statement, use the input () method birth = input ('birth: ') if birth <2000: print ('00 before') else: print ('00 after ')

Enter 1993 and an error is returned:

Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: unorderable types: str() > int()

Why? When I first came into contact with Python, I used the error here for a long time. I did not understand it until later.

For more information about input (), I have already explained in "Python basics -- output [print ()] and input [input.

This is becauseinput()The returned data type isstr,strIt cannot be compared directly with an integer.strConvert to an integer. Python providesint()Function.

S = input ('birth: ') birth = int (s) if birth <2000: print (before '00') else: print (after '00 ')

Therefore, in programming, conditional judgment is very important. Conditional judgment allows computers to make their own choices. Python's if... elif... else is flexible. Condition judgment matches from top to bottom. When the condition is met, the corresponding block statement is executed, and subsequent elif and else statements are not executed.

1.3 conditional Operators

Condition determination: the if statement focuses on determining whether to execute the statement, but does not involve many conditions.

The following are common condition operators in Python:

Conditional operators in Python
Expression Description
X = y X equals to y
X <y X less than y
X> y X is greater than y
X> = y X greater than or equal to y
X <= y X less than or equal to y
X! = Y X is not equal to y
X is y X and y are the same object.
X is not y X and y are not the same object
X in y X is a member of the y container.
X not in y X is not a member of the y container.

In Python, comparison operations and assignment operations can be connected-several operators can be used together, for example, 0 <age <120.

Some operators are worth special attention:

  I. Equal Operators

If you want to know whether the two contents are equal, you should use the equal operator, that is, two equal signs =. Remember to have two equal signs !!! A single equal sign is assigned a value ~~~

1 # equal operators = 2 >>> "python" = "python" 3 True4 >>> "Python" = "python" 5 False6 >>> "python" = "python" 7 File "<stdin> ", line 18 SyntaxError: can't assign to literal9 # obviously, a single equal operator is a value assignment operator, which is used to change the value but not to compare

  Ii. is: identity Operator

  This definition is unfriendly. What is identity? The first time I came into contact, I was confused. Only by truly understanding the is operator can I know the true meaning of identity.

At first glance, the is operator is the same as =, but actually it is different.

The sample code is as follows:

1 # identity OPERATOR: the difference between is 2 # is and = 3 >>> x = y = [1, 2, 3] 4 >>> z = [1, 2, 3] 5 >>> x = y 6 True 7 >>> x = z 8 True 9 >>> x is y10 True11 >>> y is x12 True13 >>> x is z14 False15 >>> z is x16 False

Note: The = Operator compares two objects to determine whether they are equal. The is operator determines whether the two are equal (the same object ).

III. in: Membership Operators

4. boolean operators: and, or, and not

5. assert: After conditions, add a string to explain the assertions. Keyword: assert (set the checkpoint in the Program)

Summary

1. equal sign OPERATOR: =, a single equal sign = is a value assignment operator;

2. Conditional judgment allows computers to make their own choices. Python's if... elif... else is flexible;

3. The condition judgment is matched from top to bottom. When the condition is met, the corresponding block statement is executed, and subsequent elif and else statements are not executed.

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.