Python basic syntax

Source: Internet
Author: User
Tags arithmetic arithmetic operators iterable

I. Constants and Variable constants

There are no command constants in Python, only literal constants. Once created, he will be stored in a fixed memory area and will not change. Common literal constants are integer, float, string, and so on.

Variable

What is a variable?
Variables in Python are references, so variables are aliases that point to a chunk of memory. Below is var= "python" Simple memory graph

Where OXAABBCCDD is the address that points to the store "python" string. The Var variable is the alias that points to the address where the OXAABBCCDD is stored.
If we modify Var= "Java", the figure is:

Python Variable Properties

    • Dynamic languages: You do not need to specify a variable type, only the variable type is checked when the program runs, and the variable type is recorded in memory.
    • Strongly typed: After the first assignment of a variable, the variable type is determined to be no more, unless the transformation is displayed by the display.

Naming rules for variables

    • Can only be made up of underscores, numbers, characters
    • Variable names cannot start with a number
    • You cannot use reserved words in Python, because reserved words have special meanings for python, such as if, Def, while, and so on.
Two. Operators and expression operators

Arithmetic operations
Common arithmetic operators

+、-、*、**(乘方)、/(除)、//(整除) 、%(模)

The difference between 2.X and 3.X:
Divisible by "/" in 2.X
3.X "/" for natural except, if you want to get divisible can use "//".

Note: Operands of arithmetic operators can only be integers or floating-point numbers.

Comparison operation
Comparison operators

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

Where the ==,!= operand type can be different, while the rest of the operand types need to be the same

The result of the comparison operation will be only a Boolean type, True or false.

Logical operations
Operands can only be of type bool. However, some data types that you need to be aware of are implicitly converted to bool types. For example, 0, empty string, empty built-in structure will be converted to false, non-0, non-empty string, non-empty built-in structure will be converted to true.

and:与运算符;只有左右的操作数都为Trueor:或运算符;只要有一个操作数为True,表达式结构就为Truenot:非运算符;对操作数取反,操作数如果为True,则返回False;如果操作数为False,则返回True

Short-circuit phenomenon: with and or logical operations require two bool operands, if the result of a logical expression can be judged by the bool operand on the left, then the right side of the expression will not be calculated.

Precedence of Operators
Arithmetic operations > Comparison operations > Logical Operations

It is recommended to use parentheses to indicate the priority of the operation.

An expression

The combination of a variable and an operator is an expression, for example: 32+45. Expressions tend to put back a value. So expressions are often combined with assignment operations. For example: Var=true and False

Three. Program Control structure

There are only three program control structures in the large number of programming languages. The code blocks in Python are different from C, or the Java language is enclosed in curly braces, and it is a way of using indentation to indicate a block of code.

Sequential structure

A code block written by a programmer that runs from top to bottom at run time.

Conditional structure

* * Single Branch structure

if cond:    block或if cond:        blockelse        block

Executes the block of code when the condition cond is true. Note that the ":" After cond is not omitted, it indicates the beginning of the code block

* * Multi-branch structure

if cond:     blockelif cond:        block...else:        block

Loop structure

While loop

while cond:        block

The block code is executed when the cond condition is true. To avoid a dead loop, you typically modify the cond in the block so that it exits.

where num-= 1 is a shorthand for num = num-1.

For In loop

for var in iterable:        block

The For In loop is a variable that gets one element at a time from the Iterable iterator object. The block code is then executed until the elements in the iterator object are finished, and then the loop exits.

where the range (0,10) function returns an object that can be iterated. It is important to note that you do not modify an iterative object in the loop body, because it is easy to cause a dead loop.

Break and continue in the loop
Break to jump out of a loop

Continue is used to jump out of the current loop body.

You can see that only 3 is not printed, because the statement after 3 o'clock is skipped.

Else clause in the For In loop

for var in iterable:        blockelse:        block

An else code block is executed when the for in loop does not exit prematurely

Reference

Https://www.cnblogs.com/bluestorm/archive/2012/08/28/2660277.html

Python 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.