"python" 04, python basic syntax

Source: Internet
Author: User


1. Constants and variables

2. Basic Data type

3. Operators


first, constant and variable

1. Constants

constant : The amount whose value is constant

Once assigned, the value can no longer be changed;

That means no more re-assignment, much like a read-only variable in the shell,no constants in Python

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


2. variables

variable : The amount whose value is variable

is a name, on the left side of the assignment symbol, which can refer to the contents of the right of the copied symbol

python stores all data as memory objects, which are essentially references to memory objects, and variable names and memory objects are stored separately.


Variable naming rules

can contain only letters, numbers, and underscores, and cannot start with a number

Distinguish letter case, prohibit use of reserved word


Python strongly typed dynamic language:

strongly typed languages: objects between different types do not operate on each other


Dynamic Language: a language that determines the data type during run time

At any point in time, an object reference can re-reference a different object (with different data types) whenever necessary.

the Assignment Symbol "=" is used to bind a variable name to an object in memory : If the object exists beforehand, it is bound directly; otherwise, the referenced object is created by "="


II. basic data types for Python

1. Numeric type

int: Integral type, integer

in python, int is, in principle, infinitely large, no overflow , no length limit, only limited by the size of the memory

int and long (long integer) are distinguished in python2, no distinction in Python3


float: floating-point, decimal

float has a length limit, will overflow, will lose precision

There are no double types in Python

In [+]: print (12.0003) 12.0003In []: print (12.0000000000000000000000000000000000000000000000000003) 12.0

complex: plural

complex numbers are exactly the same as the plural concepts in mathematics. The complex number in Python has the following characteristics:

complex numbers are composed of real and imaginary parts , expressed as: real+imagj or REAL+IMAGJ

Real and imaginary parts of complex numbers are floating-point imag

Imaginary numbers cannot exist alone, they are always combined with a real part (the value can be 0.0) to form a complex number.

The imaginary part must have a suffix of J or J

When the real part of the two complex numbers is equal and the imaginary part is opposite to each other, the two complex numbers are called mutual common-rail complex numbers. The imaginary part is not equal to 0 of the two Common-rail complex numbers to do common-rail imaginary

In [out[73]: type (0.0j): complexin []: 3j.conjugateout[67]: <function complex.conjugate>in [68]: 3j.conjugate () # Common rail complex out[68]: -3jin [3j.realout[70]: 3j.imagout[69]: 3.0In []:]: 0.0

You can use the type () function to view the type of an object:

In [+]: type (123) out[59]: intin [[]: type ("123") out[60]: strin [max]: type ( -123) out[61]: intin [+]: type (1.23) out[62]: F Loat

ABS () can get the absolute value of a number:

In [all]: abs ( -123) out[65]: 123


2. Boolean type

BOOL Type

Boolean is actually the subtype of the integer type, the boolean data has only two values:True and False, respectively, corresponding to the integer type of 1 and 0.

Each Python object is inherently Boolean (true or False) and can then be used for Boolean testing (as in if, while).

The Boolean value of the following objects is False:

    • None

    • False (boolean)

    • 0 (integral Type 0)

    • 0.0 (float Type 0)

    • 0.0+0.0J (plural 0)

    • "(empty String)

    • [] (empty List)

    • () (empty Tuple)

    • {} (empty Dictionary)

    • A user-defined class instance that defines a method __nonzero__() or __len__() , and these methods return 0 or False

The Boolean value of all other objects except the above object is True.

In [all]: type (true) out[33]: boolin [+]: type (true)---------------------------------------------------------------- -----------nameerror Traceback (most Recent) <ipython-input-34-83ab3fb73e0b> ; In <module> ()----> 1 type (true) nameerror:name ' true ' was not Definedin []: type ("true") out[35]: str


You can use the bool () function to view the bool value of an object:

In [max]: bool () out[40]: falsein [+]: bool ("") out[56]: falsein [[]: bool ("") out[57]: True


3. Type None

The none type has only one none, indicating nothing, no point to any memory area

In [all]: type (None) out[74]: nonetype


4. Type

In [[]: type (str) out[75]: typein []: type (int) out[76]: typein [all]: type (float) out[77]: type


third, operator

1. Arithmetic operators

arithmetic operators can only operate on numeric types

+,-,*,/,

Division is a natural division in python3, and the result must be float; in python2, it is divisible, not//

In [108]: 3/2out[108]: 1.5In [109]: 4/2out[109]: 2.0In []: 4/3out[112]: 1.3333333333333333In []: 3//2out[110 ]: 1In [111]: 4//2out[111]: 2In [113]: 4//3out[113]: 1

//(divide evenly),* *(exponentiation),%(modulo)

Usually modulo operations are called take-rest operations, and their return results are the remainder, and their only difference is:
x% Y: When the sign of X and Y is the same, two function results are equal, and when the symbols of x and Y are not the same, the sign of the remainder result is the same as the x, and the modulo is the same as Y.

In the C language, the% symbol represents the remainder operation, and in the Python script,% represents the Modulo. (generally, B is not allowed to be negative in the modulo operation, but in Python 2.5.1 can be followed by a negative number in%, because the result of division in the Python language is rounded to 0, so the result is modulo!) )

In []: 5 -2out[91]: -1in []:-5% 2out[92]: 1


2. Comparison operators

>,<,= =,!=,>=,<=

The return value of the comparison operator must be of type bool

In []: 3 > 2out[114]: truein [[+]: 3 > 5out[115]: False


3. Logical operators

the operands of the logical operators are either bool types or types that can be implicitly converted to type bool, and the return value is also of type bool

and,no,Tor

In [124]: 1 and 0out[124]: 0In [all]: True and falseout[125]: falsein [126]: 1 or 0out[126]: 1

Short-circuit operation of a logical operation:

Execution from left to right, when there is already a result, will stop the calculation and return in advance

In [127]: 3 and 4out[127]: 4In [+]: 3 or 4out[128]: 3In [129]: "abc" and 1out[129]: 1In [141]: "abc" or 1out[141]: ' abc ' In [142]: print (3) and print (4) 3In [143]: print (3) or print (4) 34


4. Identity Operator

is : Determines whether the Left-side object reference is the same as the less right Operand-side object reference

In [149]: name = ' Xxj ' in [max]: man = ' xj ' in [151]: name was manout[151]: falsein [153]: name is "xxj" out[153]: True


5. Member Operators

inch or not in: test member relationship

In [156]: xj, in "xxj" out[156]: Truein [157]: xj, not in "xxj" out[157]: False


6, Bitwise operators

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/87/29/wKiom1fWJSiRYS2AAABmTJWj82Y353.png "title=" qm@} ' Uy5]ajm2gyp7[c4erq.png "style=" float:none; "alt=" wkiom1fwjsirys2aaabmtjwj82y353.png "width=" 650 "/>

bitwise operations are operated at the binary level; python has very few bit operations and can not focus on

In [7]: 3<<3 # is in binary notation to move left 3 bits, plus 3 0out[7]: [8]: 24>>3 # is in the binary representation to move 3 bits, remove the first 3 bits, the remaining 2 is 2** 0+2**1=3out[8]: 3 in [9]: 9<<3out[9]: [ten]: 9>>3out[10]: 1 in [all]: 10>>3out[11]: 1 in []: 20> ; >3out[12]: 2 in []: 56>>3out[14]: 7


7. Precedence of Operators

Arithmetic operations > Comparison operations > Logical Operations

But you can change the precedence by the parentheses class

When in doubt or when expressions are more complex, use parentheses to determine precedence


8. Assignment Operators

Assignment operators can be used with arithmetic operators, bitwise operators

In [163]: i = 3In [164]: i + = 5In [165]: iout[165]: 8


Iv. Program Control statements

1. Expressions and statements

An expression:

constants/variables and operators form together

Expressions typically have return values (in addition to copy Expressions)

Statement:

keywords and expressions form statements together

The statement generally does not have a return value (the return value is None)


Attention:

there is no meaning in python except for the spaces at the beginning of the LINE. Add a space just to increase the readability of your code



"python" 04, python basic syntax

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.