00 variable nature —- object

Python uses an object model internally to store variables and their corresponding data. Any type of variable in Python is translated into an object, which is the essence of a variable.

Python’s internal object model consists of three parts: identity (function ID), type (type), and value (the variable itself).

  • Identity: A unique identifier used to identify an object, which can be obtained by calling the function ID. It may be an ID value of the corresponding stored memory address index, which can be viewed by calling the function id().
  • Type: Indicates the type of the object that can be stored. The specific type limits the content that the object can store, the operations that can be performed, and the rules that can be followed. You can call type() to check the type.
  • Value: The specific value that the object stores, and the variable itself is its value.

Python has two built-in help functions:

  • Dir: used to query all attributes of a class or type, such as dir(list)
  • Help: used to query the description of a class or type, for example: help(int)

01 Six standard data types for PYTHon3

  • Number (Number)
  • String(String)
  • The List (List)
  • Set (Set)
  • The Tuple (a Tuple)
  • The Dictionary (Dictionary)

Of the six standard data types for PYTHon3:

  • Immutable data (3): Number, String, Tuple
  • Mutable data (3): List(Dictionary), Set(Set)

02 Number Type

Integer (int), float (complex), bool (bool)

  • Python3 has only one integer type, int, which is represented as a Long integer. Python2 has no Long
  • Floating-point accuracy problem
0.1+0.2 = 0.30000000000000004
0.1+0.7 = 0.7999999999999999
Copy the code

Modules can be imported into Decimal

'import decimal' a = decimal. decimal ('0.1') b = decimal. decimal ('0.2') print(a+b) // 0.3Copy the code
  • Complex: A value with real and imaginary parts, both of which are floating point numbers
X = 1 + 2j // 1.0 is the value of the real part and 2.0 is the value of the imaginary part. Real obtains the value of the real part. X. imag Obtains the value of the imaginary partCopy the code

On complex numbers: z = a + bj

  • The modulus of a complex number:| z | a ^ 2 + b ^ 2 =)
  • Complex conjugate:
  1. The real parts are equal and the imaginary parts are negative;
  2. On the complex plane, the points representing the complex conjugate are symmetric about the x axis;
  3. If the imaginary part is 0, then the complex conjugate is itself.

Fraction(x,y): the numerator is x and the denominator is y

I have a list of books.Slice the usage

  • List.append () // Add objects
  • List.extend () // Adds an iterable object

The extend() method argument must be an iterable, and the new content is appended to the last element of the original list

  • List.insert () // Two arguments, the first is the position of the insert, the second is the content of the insert
  • List.remove () // Removes the specified element
  1. If there are more than one element in the list, only the first matching element is deleted
  2. If the specified element does not exist, an error is reported
  • List.pop () // unstack, returns the unstack element
  • List.clear () // Clear the list
  • List. The sort () / / order
  • List.reverse () // Reverses the list
  • List.count () // Find the number of occurrences of an element
  • List.index () // Find the index value of an element

2. There are also two arguments: start and end, which can specify the start and end positions of the search

  • List.copy () // Copy (shallow copy) lists can also be sliced: list[:]

04 Special algorithms for Python

x == ( x // y ) * y + ( x % y )

  • X // y: the result of x divided by y (floor division: an integer result is obtained after dividing, or rounded down if the result is not an integer)
  • Abs (x): The absolute value of x, and returns the magnitude of the complex number if x is complex
  • Int (x),float(x): converts to the corresponding data type
  • Complex (re,im): Returns a complex number where re is the real part and im is the imaginary part
  • C. conjugate(): Returns the complex conjugate of C
  • Pow (x,y): Returns x to the y power
  • X ** y: returns x to the y power
  • Divmod (x, y) : returns (y/x/y, x %)

Operator priority (reference): Sort from high to low

  1. Binding or tuple display, list display, dictionary display, collection display
  2. X [index]\ x(args)\ x. arttribute: subscript, slice, function call, attribute reference
  3. Await x: await expression
  4. * * : index
  5. +x, -x ~x: positive sign, negative sign, reversed by bit
  6. *, @, /, //, %: multiplication, matrix multiplication, division, floor division, mod
  7. +, -: addition, subtraction
  8. <<, >>: shift
  9. & : the bitwise and
  10. ^: bitwise xOR
  11. | : or by location
  12. in, not in, is, is not, < , <=, >, >=, ! = = = : comparison
  13. -Blair: Not a Boolean.
  14. And — and — and — and —
  15. Or: Boolean “or”
  16. If-else: conditional expression
  17. Lambda: a lambda expression

05 Loops and branches

Python loop and branch execution blocks are judged by indentation, so be careful with indentation

If-else branch judgment:

For normal if-else,python also has a weird (and tiring) way of writing it: ‘a’ if True else ‘b’ if the condition is True, otherwise b

The while loop:

If the conditions are met, the loop continues. If the conditions are not met,break and continue can be controlled to jump out of the loop.

The difference between break and continue:

Break breaks out of the loop and terminates the loop. Continue breaks out of the loop and continues with the next qualified loop. If there is an else after the while loop,break skips it, while continue does not.

For loop: Can also be used with break and continue

Syntax: for variable in Iterable: statement(s)Copy the code

Iterable: Objects that can loop, such as STR,list,dict, etc. can be used in conjunction with the range() function to loop over integers

06 Python cache reuse mechanism

  • If it has been created in the program before, it will be directly stored in the cache, and will not be created later. The effect scope is global
    1. A small integer in the range [-5,256]
    2. String object
  • Only created in the code block on the direct cache, no further creation, effective scope: this code block
    1. An integer greater than 256
    2. A floating point object greater than 0
  • No caching, extra creation is required each time
    1. A floating-point object less than 0
    2. An integer less than -5