2 Built-in objects for operator expressions

2.1 Python commonly used built-in objects

  • Everything in Python is an object,
  • Integers, real complex numbers, strings, lists, tuples, dictionaries, collections,
  • Objects such as zip, map, enumerate, filter, and functions and classes are also objects.
  • Python built-in objects are shown in Table 2-1

Insert picture description here

Insert picture description here

2.1.1 Constants and variables

  • Table 2-1, column 3 is a valid Python constant except the last 4 rows
  • A constant refers to a literal value that does not need to be changed and cannot be changed. For example, a number 3 and a list [1,2,3] are constants.
  • Variable values ​​can be changed
  • Python does not need to declare variable names and types in advance,
    • Direct assignment can create any type of object variable.
    • Variable value is variable, type can also be changed
  • The statement creates an integer variable x and assigns a value of 3.

Insert picture description here

  • Create string variable x, assign value
    • The previous integer variable x no longer exists.

Insert picture description here

  • Create a list object x, assign [1,2,3], the previous string variable x x no longer exists
  • This also applies to tuples, dictionaries, collections and other objects of any type in Python, including objects of custom types.

Insert picture description here

  • Python uses a value-based memory management model.
  • The execution process of the assignment statement:
    • First calculate the value of the expression on the right side of the equal sign
    • Find a location in memory to store the value in,
    • Finally, create a variable and point to this memory address.
  • Variables do not store values ​​directly,
    • And the memory address or reference where the value is stored,
    • This is why the variable type can be changed at any time

  • No need to explicitly declare variables and their types before use,
    • But Python is an out-of-the-box, strongly typed programming language,
    • The Python interpreter will automatically infer the variable type based on the value of the expression on the right side of the assignment operator.
  • The working mode is similar to "state machine", after the variable is created, unless the variable type is explicitly modified or the variable is deleted, the variable will keep the previous type

  • If the variable appears to the left of the assignment operator or compound assignment operator (such as + =, * =, etc.)
  • It means to create a variable or modify the value of the variable,
  • Otherwise it means referencing the value of the variable,
    • This also applies to using subscripts to access variable sequences such as lists and dictionaries
    • And elements in custom objects

Insert picture description here

A lot did not write

2.1.3 Strings and byte strings

  • In Python, there are no character constants and variables, only string type constants and variables, and a single character is also a string.
  • Single quotes, double quotes, triple single quotes, triple double quotes as delimiters to represent strings,
    • And different delimiters can be nested between
  • Python3.x supports Chinese, Chinese and English letters are treated as one character,
    • You can even use Chinese as a measurement name
  • Plus sign connection string, Python string also provides a lot of methods
    • Find, replace, typeset
    • Many built-in functions and standard library objects also support string operations. Chapter 7 introduces them in detail.

here

2.4 Essentials of Python's commonly used built-in functions

  • Built-in functions are one of Python's built-in object types. They can be used directly without the need to import any additional modules. These built-in objects are encapsulated in the built-in module _ builtins__, implemented in C language
    and optimized a lot, very fast running speed, Recommend to use first.
  • Built-in function dir () can view all built-in functions and built-in objects

Insert picture description here

  • help (function name) can view the usage of a function.
  • You can also use help (module name) to view the help documentation of the module without importing the module, such as help ('math ").
  • In Table 2-5, the parameters in square brackets can be omitted.

Insert picture description here

Insert picture description here

Insert picture description here

  • If you encounter unfamiliar functions, you can view the help through the built-in function help ().
  • Prioritize the use of built-in functions when programming,
    • The built-in functions are not only mature and stable, but also relatively fast.

2.4.1 Type conversion and type judgment

  • (1) bin (), oct (), hex () convert integers to binary, octal and hexadecimal forms, the parameters must be integers

Insert picture description here

  • int converts other forms of numbers to integers,
    • The parameters are integers, real numbers, fractions, or legal numeric strings,
    • When the parameter is a numeric string, the second parameter base is also allowed to specify the base of the numeric string.
    • base takes an integer of 0 or 2 ~ 36,
    • 0 means conversion by hexadecimal implied by numeric string.

Insert picture description here

  • float () converts other types of data to real numbers,
  • complex () is used to generate complex numbers.

Insert picture description here

  • (2) The functions of ord () and chr () are opposite,
  • ord () is used to return the Unicode code of a single character,
  • chr () returns the character corresponding to Unicode-encoding,
  • str () directly converts any type parameter into a string.

Insert picture description here

  • The built-in class asci converts objects to ASCII representation, using escape characters to represent specific characters when necessary.

Insert picture description here

  • Built-in classes to generate byte strings,
    • Or convert the specified object to a specific encoded byte string.

Insert picture description here

  • (3) list (), tuple (), dict (), set (), frozenset () convert other types of data into list tuples, dictionaries, variable sets and immutable sets, or create empty lists and empty elements Groups, empty dictionaries and empty collections.

Insert picture description here

  • (4) Built-in function to judge the data type,
    • Check the function parameters,
    • You can avoid the wrong parameter type causing the function to crash or return unexpected results.

Insert picture description here

2.4.2 Maximum value and summation

  • Compute lists, tuples, or other iterable objects that contain a finite number of elements
  • sum () by default (can be changed by the start parameter) supports sequences or iterable objects containing numeric elements,
  • max () and min () require comparable sizes between elements in a sequence or iterable

Insert picture description here

2.4.3 Basic input output

Insert picture description here

Insert picture description here

2.4.4 Sorting and reverse order

Published 589 original articles · 300 praises · 80,000 + views

Guess you like

Origin blog.csdn.net/zhoutianzi12/article/details/105556592