Home  >  Article  >  Backend Development  >  What are the basic syntax of python?

What are the basic syntax of python?

coldplay.xixi
coldplay.xixiOriginal
2020-06-10 14:48:2411019browse

What are the basic syntax of python?

What are the basic syntaxes of python?

Summary of basic python syntax:

1.Python identifier

In Python, identifiers are Composed of letters, numbers, and underscores.

In Python, all identifiers can include English, numbers, and underscores (_), but cannot start with a number.

Identifiers in Python are case-sensitive.

Identifiers starting with an underscore have special meaning. _foo starting with a single underscore represents class attributes that cannot be accessed directly. They need to be accessed through the interface provided by the class and cannot be imported using from xxx import *;

foo starting with a double underscore represents the private members of the class ; foo starting and ending with a double underscore represents a special method-specific identifier in Python, such as init__() representing the constructor of a class.

2. Python has five standard data types

Numbers (numbers)

String (strings)

List (List)

Tuple (Tuple)

Dictionary (Dictionary)

Python supports four different number types:

int (signed integer type)

long (long integer type [can also represent octal and hexadecimal])

float (floating point type)

complex (plural number)

Python's string list has two order of values:

Indexing from left to right starts from 0 by default, and the maximum range is 1 less than the length of the string

Indexing from right to left By default, it starts with -1, and the maximum range is the beginning of the string

List (list) is the most frequently used data type in Python.

Lists can complete the data structure implementation of most collection classes. It supports characters, numbers, strings and can even contain lists (i.e. nested).

Lists are marked with [ ] and are the most common composite data type in Python.

The variable [head subscript: tail subscript] can also be used to cut the values ​​in the list, and the corresponding list can be intercepted. The index from left to right defaults to 0, and the index from right to left defaults to -1. At the beginning, the subscript can be empty to indicate getting to the beginning or end.

The plus sign is the list connection operator, and the asterisk * is the repeat operation.

Tuple is another data type, similar to List (list).

Tuples are marked with "()". Internal elements are separated by commas. However, tuples cannot be assigned values ​​twice and are equivalent to read-only lists.

Dictionary is the most flexible built-in data structure type in python besides lists.

Lists are ordered combinations of objects, and dictionaries are unordered collections of objects. The difference between the two is that the elements in the dictionary are accessed by key, not by offset.

Dictionaries are marked with "{ }". A dictionary consists of an index (key) and its corresponding value.

3.Python data type conversion

Sometimes, we need to convert the built-in type of data. To convert the data type, you only need to use the data type as a function Just name.

The following built-in functions can perform conversion between data types. These functions return a new object representing the converted value.

Function description

int(x [,base])

Convert x to an integer

long(x [,base] )

Convert x to a long integer

float(x)

Convert x to a floating point number

complex(real [,imag])

Create a plural number

str(x)

Convert object x to string

repr(x)

Convert object x For expression string

eval(str)

is used to evaluate the valid Python expression in the string and returns an object

tuple(s)

Convert sequence s into a tuple

list(s)

Convert sequence s into a list

set(s)

Convert to a mutable collection

dict(d)

Create a dictionary. d must be a sequence (key, value) tuple.

frozenset(s)

Convert to an immutable set

chr(x)

Convert an integer to a character

unichr(x)

Convert an integer to a Unicode character

ord(x)

Convert a character to its integer value

hex( x)

Convert an integer to a hexadecimal string

oct(x)

Convert an integer to an octal string

4.Python operators

Arithmetic operators

Comparison (relational) operators

Assignment operators

Logical operations Operator

Bitwise operator

Membership operator

Identity operator

Operator precedence

python arithmetic operator

Operator description example

Add-add two objects a, output result 30

Subtract-get a negative number or subtract one number from another number a-b, output result-10

Multiplication - multiply two numbers or return a string a * b that is repeated several times. The output result is 200

/ Division - x is divided by y b/a and the output result is 2

% Modulo - Returns the remainder b % a of the division. The output result is 0

Power - Returns the y power of Take integer division - return the integer part of the quotient 9//2 output result 4, 9.0//2.0 output result 4.0

①: python comparison operator

The following assumes that variable a is 10 and variable b For 20:

Operator description instance

==Equal-Compare whether the objects are equal (a==b) Return False.

!=Not equal to - compares two objects to see if they are not equal (a !=b) and returns true.

<>Not equal to - Compares whether two objects are not equal (a<>b) returns true. This operator is similar to != .

Greater than - Returns whether x is greater than y (a > b) Returns False.

= Greater than or equal to - Returns whether x is greater than or equal to y. (a >= b) returns False.

<= Less than or equal to - Returns whether x is less than or equal to y. (a <= b) returns true.

②: Python assignment operator

The following assumes that variable a is 10 and variable b is 20:

Operator description example

=Simple assignment Operator c= a b assigns the operation result of a b to c

=Additional assignment operator c =a is equivalent to c=c a

-=Subtractive assignment operator c -=a is equivalent In c=c - a

=The multiplication assignment operator c=a is equivalent to c=c * a

/=The division assignment operator c=a is equivalent to c=c / a

%=modulo assignment operator c%=a is equivalent to c = c % a

= power assignment operator c=a is equivalent to c=c ** a

//= Take the integer division assignment operator c//=a is equivalent to c=c // a

③: Python bit operator

In the following table, the variable a is 60 and b is 13 , the binary format is as follows:

a=00111100

b=00001101

a&b=00001100

a|b=00111101

a ^b=00110001

~a=11000011

Operator description example

& Bitwise AND operator: Two values ​​participating in the operation, if the two corresponding bits are both is 1, then the result of this bit is 1, otherwise it is 0 (a & b) The output result is 12, binary interpretation: 0000 1100

| Bitwise OR operator: as long as there is one of the two corresponding binary bits When it is 1, the result bit is 1. (a | b) Output result 61, binary interpretation: 00111101

^ Bitwise XOR operator: When the two corresponding binary bits are different, the result is 1 (a ^ b) Output result 49, binary Explanation: 00110001

~ Bitwise negation operator: negate each binary bit of the data, that is, change 1 to 0, change 0 to 1 (~a), the output result is -61, binary interpretation : 11000011, in the two's complement form of a signed binary number.

<< Left shift operator: All binary bits of the operand are shifted to the left by a certain number of bits. The number on the right of "<<" specifies the number of bits to move. The high bits are discarded and the low bits are filled with 0. a << 2 output result 240, binary interpretation: 11110000

right shift operator: shift all the binary digits of the operand to the left of ">>" to the right by a certain number of bits, " >>" The number on the right specifies the number of digits to move a >> 2 The output result is 15, binary interpretation: 0000 1111

④: Python logical operator

Python The language supports logical operators. The following assumes that variable a is 10 and b is 20:

Operator logical expression description example

and x and y Boolean "AND" - If x is False, x and y returns False, otherwise it returns the calculated value of y. (a and b) returns 20.

or x or y boolean or - if x is non-zero, it returns the value of x, otherwise it returns the calculated value of y. (a or b) returns 10.

not not xBoolean "not" - If x is True, returns False. If x is False, it returns True. not(a and b) returns False

⑤: Python member operator

In addition to some of the above operators, Python also supports member operators. The test example contains a series of Members, including strings, lists, or tuples.

Operator Description Example

in Returns True if the value is found in the specified sequence, False otherwise. x is in the y sequence, returns True if x is in the y sequence.

not in Returns True if the value is not found in the specified sequence, otherwise returns False. x is not in the y sequence, returns True if x is not in the y sequence.

⑥: Python identity operator

Identity operator is used to compare the storage unit of two objects

Operator description instance

is It is to determine whether two identifiers refer to an object x is y, similar to id(x) == id(y). If they refer to the same object, it returns True, otherwise it returns False

is not is not is to determine whether two identifiers refer to different objects x is not y, similar to id(a) != id(b). If the reference is not the same object, the result is True, otherwise it is False.

Note: The difference between is and ==:

is is used to determine whether the objects referenced by two variables are the same, and == is used to determine whether the values ​​of the referenced variables are equal.

Recommended tutorial: "python video tutorial"

The above is the detailed content of What are the basic syntax of python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn