Python operators and Process Control

Source: Internet
Author: User
Tags bitwise builtin

operator and Process Controloperator
  • Assignment operations

    • Use ' = ' to indicate that the left side of ' = ' can only be a variable

  • Arithmetic operations

    • + 、-、 *: Add, subtract, multiply

    • /: Division operation, result of operation is floating point number

    • : Division operation, the result of the operation is an integer (quotient)

    • %: Seek redundancy

    • * *: exponentiation

  • Composite operations

    • + =,-=, *=,/=,//=,%=, **=

  • Relational operations (comparison operations)

    • >、>=、<、<=、==、!=

    • A formula consisting of operands and operators is called an expression

    • The relational expression is a value (Boolean value) that conforms to the mathematical logical value of true and does not conform to the mathematical logical value FALSE

  • Logical operations

      • With (and): the expression on both sides is true and the value is True

    # Determine if it's between 100~200
    Print ((and(200))
    # is equivalent to the upper type
    Print ($)
      • or (OR): the expression on either side is true and the value is True

     
    # determine if a leap year
    int (input ( Span class= "Cm-builtin" >print (((y%4 ==0) and (y %100!=0)) or (y%400==0) )
      • Non (not): used to take the inverse expression, true false, false change true

        • Logical operations are often accompanied by implicit type conversions.

        • Many times it is used to simplify expressions

  • Member operations

      • In, not in: Determines whether a variable is a member of another variable, returns True, not return false

      • Generally used in string, list, tuple, set, dictionary and other members of the judgment

      • If the dictionary is judging by the key

      l = [ ' Li Bai ',  "Ouyang Xiu" 
    print ( ' Li Bai ' in l)
    ?
    d = { ' a ': ' Apple ', ' B ': ' banana ', ' cat ', Span class= "cm-string" > ' d ': ' dog '}
    print ( ' AA ' not in d)
  • Bitwise operations (understanding)

    • &: Bitwise AND, the corresponding data is 1, the value is 1

    • |: bitwise OR, the corresponding data one is 1, the value is 1

    • ^: Bitwise XOR, the corresponding data has the same value of 0, the value is 1

    • ~: bitwise reversed, corresponding data bit 1 variable 0,0 1

    • <<: Shift left, equivalent to multiply by 2

    • : Bitwise right move, equivalent to except 2

  • Priority: No need to record operator precedence, add () when needed.

Process Control
  • According to the procedure, the program can be divided into three kinds of structure: sequential structure, branch structure, cyclic structure

  • The code hierarchy in Python is represented by indentation.

  • Sequential structure: code from top to bottom, execute in sequence, no syntax required

  • Branch structure: If-else

      • Format 1


    An expression:
    Statement block

    Execution process: When the program executes to the IF statement, first of all to determine the true or false expression; If the value of the expression is true (true), the statement block is executed, and if the value of the expression is False (false), the statement block is skipped and execution continues down.

      • Format 2


    An expression:
    Statement Block 1
    Else
    Statement Block 2

    Execution process: When the program executes to the IF-ELSE statement, first of all to determine the true or false expression; If the value of the expression is true, the statement block 1 is executed, and if the value of the expression is false, then the statement block 2 after the else is executed.

      • Format 3


    If expression 1:
    Statement Block 1
    Elif Expression 2:
    Statement Block 2
    ...

    Else
    Statement block N
    1. Unlimited number of Elif

    2. else can not write

      • Practice:


    1. Enter two integers to print a larger value
    2. Enter three integers to print in order from small to large
    3. Enter a three-digit number to print its digits, 10 bits, and hundreds
    4. Enter a year, to determine whether it is leap years, is to print a word, not print another sentence
    5. Enter an integer to determine whether it can be divisible by 3 and divisible by 5
  • Loop structure (while)

      • Format


    An expression:
    Statement block

    Execution process: When the program executes to the while statement, first of all to determine the true and false expression. When the value of the expression is true, the corresponding statement block is executed, then the while is returned while the expression is not true or false, and if the expression is true or false, the statement block is skipped.

    1. An expression is also called a loop condition

    2. A statement block is also called a loop body

    3. Expressions have been established as dead loops

    4. Break: Jump out of the loop

    5. Continue: End this cycle and go to the next cycle

    6. In conjunction with else use: loop exits normally, the statement block after the else is executed, and non-normal (break) exits without executing the ELSE statement block.


      1
      ?
      10:
      print (i)
      1
      Break
      Else
      print (' end ')
      • Practice:


    1. Calculates the and of all integers between 1~100
    2. Print character a~z
    3. Print character Z~a
    4. Loop input 10 characters, uppercase to lowercase, lowercase to uppercase, other characters not processed
    5. Think: Can the loop be nested, think about its application scenario and the execution process

    ?

Python operators and Process 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.