python0.6-----Conditional Control statements/looping statements and usage tips

Source: Internet
Author: User

Conditional control Statements: if Condition control statement:

Format:

An If expression:

  Statement 1 (the statement must be preceded by a tab, otherwise no statement is considered)

Logic: Expression is set, then statement 1 is executed, otherwise skipped

If-else Conditional control statements:

Format:

If expression 1:

Statement 1

Else

Statement 2

Logic: Expression 1 is established, then statement 1 is executed, otherwise statement 2 is executed

If-elif-else Conditional control statements:

Format:

If expression 1:

Statement 1

Elif Expression 2:

Statement 2

Elif Expression 3:

Statement 3

......

......

......

Elif Expression N:

Statement n

Else

Statement E

Logic: If the expression 1 is established, executes the statement 1, otherwise the expression 2 is established, if the execution is executed to execute the statement 2, otherwise the expression 3, if set up to execute Statement 3, ... Otherwise judge the statement N, if it is set to execute the statement n, if the above expression 1-n all is not true, then execute the statement e.

Note : When an expression is false, it can be in the following form:

0 0.0 "" "None False

The value of the final expression, regardless of how much, is implicitly converted to true or false.

Loop Control statement:

While loop control statement:

Format:

While expression 1:

Statement 1

Logic: If expression 1 has been established, then the statement 1 is executed, and when the expression is not true, jump out of the loop

Note: When using continue in the While Statement 1, the loop variable +1 will be preceded by the continue statement, otherwise the loop will die.

While-else statement:

Format:

While expression:

Statement 1

Else

Statement 2

Logic: If expression 1 has been established, then the statement 1 is executed, and when the expression is not true, jump out of the loop. Statement 2 in else is executed when the loop jumps out because the expression 1 is false.

Note : If a while loop is caused by a break, then statement 2 inside the Else branch is not executed.

For loop control statements:

Format:

For variable name in ' collection ':

Statement 1

Logic: Assign each element in order to each of the elements in the ' Set ' (' collection ' as long as it is an iterative data line) and EXECUTE statement 1, repeating it until the collection element has been taken out.

How to make a ' collection ':

Range ([Start,] stop [, step]): Generates an integer sequence with a range of [start,stop] and a step of step,start default to 0,step of 1. Note : The integer series generated by range () can only be changed by the for loop for each element

Two keywords related to loops:

Break statement: Jumps out of a for or while loop. You can only jump out of its nearest loop.

Continue statement: Skips the remaining statements in the secondary loop, making the next loop.

Principles for using Conditional statements/Loop statements:

① level nesting is best not to more than 3 layers, more than 3 layers are easy to read.

② When the function is reached, the less instructions the computer is expected to process, the better  

1 #determine whether a number is a palindrome number2Num=int (Input ('Please enter a five-digit number:'))3U=num%10#Digit4tt=num//10000#million-bit5ifu==TT:6T=num//10%10#10 Guests7th=num%10000//1000#thousand8ifT!=th:#此处为亮点: Because if the bits and the million are different, you do not need to judge the 10-bit and the million-bit, the computer will be less to execute a judgment instruction. 9       Print('%d is not a palindrome number'%num)TenElse: One     Print('%d is a palindrome number'%num) AeLSE: -       Print('%d is not a palindrome number'%num)

③: As long as the instructions are processed by the computer, you want the number of the code to be as low as possible

1 # determine the maximum of three numbers:(spend 6 lines of code, very concise, excellent!!!)  2 max=num13if num2>Max:4 max=num25 if num3>Max:6 max=num37print(' The maximum value is%d ' %max)
1 #find the maximum number of three numbers:(spend 10 lines of code, not concise enough, not good enough!!!) 2 ifNum1>num2:3     ifNum1>num3:4         Print(NUM1)5     Else:6         Print(num3)7 Else:8     ifNum2>num3:9         Print(num2)Ten     Else: One         Print(NUM3)

python0.6-----Conditional Control statements/looping statements and usage tips

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.