Python3.5 Introductory Learning record-conditional control

Source: Internet
Author: User

Python's conditional control, like C #, is determined by the execution result of one or more statements (True or False) to determine the code block to execute.

If statement

The general form of the IF statement in Python is as follows:

if condition_1:    statement_block_1elif  condition_2:    statement_block_2Else  :    statement_block_3
    • If "condition_1" is True, the "statement_block_1" block statement is executed
    • If "condition_1" is false, the "condition_2" will be judged
    • If "Condition_2" is True, the "statement_block_2" block statement is executed
    • If "Condition_2" is false, the "STATEMENT_BLOCK_3" block statement is executed

Python replaces the else ifwith elif , so the keyword for the IF statement is:if–elif–else.

Attention:

    • 1, followed by a colon (:) after each condition, indicates the next block of statements to be executed after the condition is met.
    • 2, using indentation to divide the statement block, the same indentation number of statements together to form a statement block.
    • 3, there are no switch–case statements in Python.
 var1 = 100if  var1 > 0:  print  ("  if expression True   " )  print   (var1) var2  = 0  if  var2 > 0:  print  ( " 2-if expression condition is true   )  print   (VAR2)  print  ( "  white  "  )

The result of the above code execution is

age = Int (input ("Please enter your dog's age:"))Print("")ifAge <0:Print("your dog hasn't been born yet!")elifAge = = 1:    Print("the equivalent of 14-year-old person. ")elifAge = = 2:    Print("the equivalent of 22-year-old person. ")elifAge > 2: Human= + (age-2)Print("corresponding to human age:", human)## # Exit TipsInput'Click the ENTER key to exit')

The following are the operations operators commonly used in the IF:

Operator
Describe

<
Less than

<=
Less than or equal to

>
Greater than

>=
Greater than or equal to

==
Equals, compares objects for equality

!=
Not equal to

If nesting

if expression 1:    statement if  expression 2:        statement     elif  expression 3:        statement      else         statement elif  expression 4:    statement Else:    statement

Let's look at an example:

Num=int (Input ("Enter a number:"))ifnum%2==0:ifnum%3==0:Print("The numbers you enter can be divisible by 2 and 3.")    Else:        Print("The number you enter can be divisible by 2, but not divisible by 3 .")Else:    ifnum%3==0:Print("The number you enter can be divisible by 3, but not divisible by 2 .")    Else:        Print("the number you entered cannot be divisible by 2 and 3.")

While loop

A while loop has been used in the figure guessing above, and the basic form of the while loop in Python is:

 while Judging Condition:    Execute statement ...

The execution statement can be a single statement or a block of statements. The judging condition can be any expression, and any value other than 0, or non-null (NULL), is true.

The loop ends when the condition false false is judged.

Count = 0 while (Count < 9):   print'thecount is:'  , Count   = count + 1print"good bye! "

The output of running the code changes is:

While statement there are two other important command Continue,break to skip the loop, continue used to skip the cycle, break is used to exit the loop, in addition, "judging condition" can also be a constant value, indicating that the loop must be set up, the following:

Count =0 while(Count < 9):    if(count==3):        Print("count=3, skip the cycle .") Count=count+1Continue    if(count==6):        Print("count=6, end loop")         Break; Print('The count is:', Count) Count= Count + 1Print("Good bye!")

The result of this code is:

While....else can also be used in Python, where statements in the while are not distinguished from ordinary statements, and the statements in else are executed when the loop is executed normally (that is, not by break), For...else is the same

Count = 0 while count < 5:   print" was less  than 5" /c8>)   = count + 1else:   print" is not less than 5
     ")

The result of this code is:

For Loop statement

A Python for loop can traverse any sequence of items, such as a list or a string.

Grammar:

The syntax format for the For loop is as follows:

 for inch list:   statements (s)

The following example:

 forLetterinch 'Python':#Loop A string   Print('Current Letter:', letter) Fruits= ['Banana','Apple','Mango'] forFruitinchFruits#Loop A list   Print('Current Word:', fruit)Print("Good bye!")

The output is:

Using For....else in A For loop

Fruits = [' banana ', ' apple ', '  Mango ']
For fruit in fruits: # Loop A list
For letters in fruit: # Loops each word
if (Letters = = ' L '): #当单词中含有字母l时跳出循环
Print (' The current word is: {0}, the current letter is: L '. Format (fruit))
Break
else: #当当前单词的所有字母正常结束循环时打印
Print (' The current word is: {0}, the last letter of the current word is: {1} '. Format (fruit,letters))

The result of the execution is:

Python3.5 Introductory Learning record-conditional control

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.