Python Learning-Basic syntax

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise operators logical operators

1. Constants/Variables

Constants: Once the assignment is immutable, the value cannot be re-assigned. There are no constants for Python.

Literal constants: A single occurrence of a quantity that is not assigned to any variable or constant.

Variable: is a name on the left side of the assignment symbol. This name can refer to the content to the right of the assignment symbol.

i = 3

Except for the space at the beginning of the line, the space is meaningless.

Basic types of variables

int: Theoretically no length limit

FLOAT: May lose intensive reading

Bool:true/false

None:none

Python is a strongly typed language: Different types cannot be counted against each other

Python is a dynamic type language: variables can be re-assigned to other types

2. Operators

Arithmetic operators: +-*///* *% for int and float operations

/python3 for nature except 3/2 = 1.5 result is float

/Python2 is divisible by 3/2 = 1 to convert any number to float. and natural divide.

Divide the Python3 evenly

0 cannot be a divisor

comparison operator:< > = = = <= >= return value is bool type

Logical operators: the and or not operands are either bool types or implicitly converted to bool types.

There is a short-circuit operation: From left to right execution, when there is already a result, stop the calculation, return early.

Member operators: associated with collection types

Identity operator: Object-oriented

Bitwise operators: Manipulating binary directly

Precedence of operators: arithmetic > Comparison > Logic can change precedence by parentheses.

Assignment operator: no result returned. I +=3 equivalent to i = i +3 can be used with arithmetic operators

3, the expression/statement, the expression has a return value, the statement is a keyword and an expression together to form a statement, there is no return value.

4. Program structure: The order in which statements and expressions are executed

Order: Execute from top to bottom

Branch: Always only one or 0 branches are executed, the condition must be of type bool, or the bool type is implicitly convertible.

Single branch:

Begin

If condition:

Operation

End

Dual Branch:

Begin

If condition:

Operation

Else

Operation

End

Cycle

Multi-branch:

Begin

If condition:

Operation

Elif Conditions:

Operation

............

Else

Operation

End

Loop: While/for in

While

Begin

While condition:

Operation

End

For in never modifying an iterator object

Begin

The for element in iterates over an object:

Operation

End

Terminate break prematurely

Skip the rest of the loop body continue

Break and continue can only be used in loops, only for the latest layer of loops

else when the loop does not exit prematurely, the ELSE clause is executed


Python Learning-basic syntax

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.