A picture of Python (with basic grammar summary)

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

A picture takes you to learn more about Python, getting started faster,

Video introduction >>>> [simple learn python security]

Python Basic Syntax Summary:

1.Python identifiers

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

In Python, all identifiers can include English, numeric, and underscore (_), but cannot begin with a number.

Identifiers in Python are case-sensitive.

Identifiers that begin with an underscore are of special significance. A class attribute that begins with a single underscore, which is not directly accessible by the _foo, must be accessed through the interface provided by the class and cannot be imported with the From XXX import *;

Foo, which starts with a double underscore, represents the private member of the class; Foo, which begins with a double underscore and the end of the init__ , represents a special method-specific identifier for Python, such as the constructor for a class.

2.Python has five standard data types

Numbers (digital)
String (String)
List (lists)
Tuple (tuple)
Dictionary (dictionary)
Python supports four different types of numbers:

int (signed integral type)
Long (longer integer [can also represent octal and hexadecimal])
Float (float type)
Complex (plural)
The Python string list has 2 order of values:

Left-to-right index starts at default 0, with a maximum range of 1 less string lengths
Right-to-left index starts with default-1, the maximum range is the beginning of the string
The list is the most frequently used data type in Python.

A list can accomplish the data structure implementation of most collection classes. It supports characters, numbers, and strings that can even contain lists (that is, nesting).
The list is identified by [], which is the most common type of composite data for Python.
The cut of the value in the list can also be used to the variable [head subscript: Tail subscript], you can intercept the corresponding list, from left to right index default 0, starting from right to left index default-1, subscript can be empty to take the head or tail.
The plus + is the list join operator, and the asterisk * is a repeating operation.
A tuple is another data type, similar to a list.

The tuple is identified with a "()". The inner elements are separated by commas. However, tuples cannot be assigned two times, which is equivalent to a read-only list.
The Dictionary (dictionary) is the most flexible built-in data structure type in Python, except for lists.

A list is an ordered combination of objects, and a dictionary is a collection of unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.
The dictionary is identified with "{}". A dictionary consists of an index (key) and a value corresponding to it.
3.Python Data type Conversions

Sometimes, we need to convert the data-built type into the data type, and you just need to use the data type as the function name.

The following several built-in functions can perform conversions between data types. These functions return a new object that represents the value of the transformation.

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 complex number

STR (x)

Convert an object x to a string

REPR (x)

Convert an object x to an expression string

eval (str)

Used to evaluate a valid Python expression in a string and return an object

Tuple (s)

Converting a sequence s to a tuple

List (s)

Convert the sequence s to a list

Set (s)

Convert to mutable Collection

Dict (d)

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

Frozenset (s)

Convert to immutable Collection

Chr (x)

Converts an integer to one character

UNICHR (x)

Converts an integer to a Unicode character

Ord (x)

Converts a character to its integer value

Hex (x)

Converts an integer to a hexadecimal string

Oct (x)

Converts an integer to an octal string

4.Python operator

Arithmetic operators
Compare (relational) operators
Assignment operators
logical operators
Bitwise operators
Member operators
Identity operator
Operator Precedence
Python arithmetic operators

Operator Description Instance

    • Add-two objects add a + B output result 30
    • Minus-get negative numbers or one number minus another number A-B output result-10
    • Multiply by two number or return a string that is repeated several times a * b output result 200
      /divide-X divided by y b/a output 2
      % modulo-Returns the remainder of division B% A output result 0
      power-Returns the y power of X aB is 10 20 times, the output 100000000000000000000
      Divide-Returns the integer portion of the quotient 9//2 output result 4, 9.0//2.0 output 4.0

①:python comparison Operators

The following assumes that the variable A is 10 and the variable B is 20:

Operator Description Instance
= = equals-The comparison object is equal (a = = B) returns FALSE.
! = does not equal-compares two objects that are not equal (a! = B) returns TRUE.
<> not equals-compares two objects for unequal (a <> B) returns True. This operator is similar to! =.

Greater than-returns whether x is greater than Y (a > B) returns FALSE.
< less-Returns whether x is less than Y. All comparison operators return 1 for true, and return 0 for false. This distinction is equivalent to the special variable true and false. Note that these variable names are capitalized. (A < B) returns TRUE.
= Greater than or equal-returns whether X is greater than or equal to Y. (a >= B) returns FALSE.
<= is less than or equal-returns whether X is less than or equal to Y. (a <= B) returns True.

②:python assignment operator

The following assumes that the variable A is 10 and the variable B is 20:

Operator Description Instance
= Simple assignment Operator C = A + B assigns the result of the operation of A + B to c
+ = addition Assignment operator C + = A is equivalent to C = C + A
-= Subtraction Assignment operator C-= A is equivalent to C = c-a
= multiplication assignment operator C = A is equivalent to C = c * A
/= 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 divisible assignment operator C//= A is equivalent to C = c//A

③:python Bitwise operators

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

A = 0011 1100
b = 0000 1101

a&b = 0000 1100
a|b = 0011 1101
A^b = 0011 0001
~a = 1100 0011
Operator Description Instance
& Bitwise AND Operator: Two values that participate in the operation, if two corresponding bits are 1, the result of that bit is 1, otherwise 0 (A & B) outputs 12, binary interpretation: 0000 1100
| Bitwise OR operator: As long as the corresponding two binary has one for 1 o'clock, the result bit is 1. (A | b) output result 61, Binary interpretation: 0011 1101
^ Bitwise XOR Operator: When two corresponding binary differences, the result is 1 (a ^ b) output result 49, binary interpretation: 0011 0001
~ Bitwise inverse Operator: bits for each of the data, i.e. 1 to 0, 0 to 1 (~a) output-61, Binary interpretation: 1100 0011, in the complement form of a signed binary number.
<< left move operator: Each binary of the operand all shifts left several bits, the number of "<<" to the right to specify the number of bits moved, high drop, low 0. A << 2 output results 240, binary interpretation: 1111 0000

> Right Move operator: All the binary of the left operand of ">>" is shifted to the right of a number of bits, the number to the right of ">>" specifies the number of bits moved a >> 2 output result 15, binary interpretation: 0000 1111

④:python logical operators

The Python language supports logical operators, with the following assumption that the variable A is ten and B is 20:

Operator logical expression describes an instance
and X and Y Boolean "and"-if x is False,x and y returns FALSE, otherwise it returns the computed value of Y. (A and B) returns 20.
or X or y boolean "or"-if x is 0, it returns the value of x, otherwise it returns the computed value of Y. (A or B) returns 10.
Not not X boolean "non"-returns False if X is True. If X is False, it returns TRUE. Not (A and B) returns False

⑤:python member Operators

In addition to some of the above operators, Python also supports member operators, which contain a series of members, including strings, lists, or tuples.

Operator Description Instance
In if the value found in the specified sequence returns TRUE, False is returned. x in the y sequence, if X returns True in the y sequence.
Not if no value is found in the specified sequence returns True, otherwise False is returned. X is not in the Y sequence if x does not return True in the y sequence.

⑥:python identity operator

The identity operator is used to compare the storage units of two objects

Operator Description Instance
IS is to determine whether two identifiers are referenced from an object x is Y, similar to ID (x) = = ID (y), and returns True if the reference is the same object, otherwise False
Is isn't not a reference to a two identifier that is not referenced from a different object x is not y, similar to ID (a)! = ID (b). Returns True if the reference is not the same object, otherwise False is returned.

Method Original Address: https://www.cnblogs.com/shanlizi/p/7015852.html

Note: IS and = = difference:

is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。

A picture of Python (with basic grammar summary)

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.