Python3 condition Control

Source: Internet
Author: User

Python3 Conditional Control If statement

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

if condition_1: statement_block_1 elif condition_2: statement_block_2 Else: 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. Use a colon (:) after each condition to indicate 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 block of statements.
  • 3. There are no switch–case statements in Python. Python3 Loop Statements

    This section will introduce you to the use of the Python loop statement.

    The looping statements in Python have for and while.

    The control structure diagram for the Python loop statement is as follows:

    While loop

    The general form of a while statement in Python:

    While  judgment condition:     statement  
    For statement

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

    Break and continue statements and ELSE clauses in loops

    The break statement can jump out of a for and while loop body. If you terminate from a for or while loop, any corresponding loop else blocks will not execute; The continue statement is used to tell Python to skip the remaining statements in the current loop block and then proceed to the next round of loops.

    Pass Statement

    The Python Pass is an empty statement in order to maintain the integrity of the program structure.

    Pass does not do anything, generally used as a placeholder statement

    #for loop 1-100 of all integers and
    Sum=0
    For I in Range (1,101):
    Sum=sum+i
    Print (sum)


    Use for To print 99 multiplication table
    For I in Range (1,10):
    For j in Range (1,i+1):
    #print (' {}x{}={}\t '. Format (j, I, I*j), end= "")
    Print ("%d*%d=%d"% (j,i,i*j), end= "")
    Print ()

Python3 condition 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.