Python face questions

(1) how to convert a string to an integer?

      Int int function may be used, such as ( '3') results from the string '3' to 3 Integer

(2) python built-in data types are there?

      int 、bool、 str、list、 ruple、dict

(3) how to output a digital format is 10 bits wide, less than leading zeros?

      Can '% 010d'% 56 or '{: 010}'. Format (56)

(4) '4567' [3:] What operating result?

      Slicing operation, index starting from 0 0 1 2 3 is the fourth number of the results of '4567' is 7

(5) What is the negative index?

      A negative index is retrieved from the right side, the same operation can be used to slice a [: - 1]

(6) deep copy and shallow copy of the difference?

      Deep Copy: Copy the object itself to another object, with DeepCopy () function

      Shallow copy: copying the object reference to another object copy ()

(7) explained in python join () and split () function?

      join () is added to the specified string string

      Segmentation is used to split the string

(8) python identifier naming?

      Only underscore or AZ / az letters beginning

      The rest can only use AZ / az / 0-9

      case-sensitive identifier python

      Keywords can not be used as identifiers. The read / for / in / true / def etc.

(9) How to delete a string of leading spaces?

      ISTRIP () ----- leading spaces for deleting a character string; The rstrip () ----- trailing space for deleting a string

(10) How to convert a string to lowercase?

      Lower () ----- string for all lowercase letters converted; upper () ------ for all letters in the string is converted to uppercase

(11) How to declare a number of variables and assignment?

      Declare variables and assign the plurality of two ways: ① a, b, c = 1,2,3

                                                 ② a = b = 3

What (12) the difference between tuple and list mainly?

      The list is variable and can add modify delete elements; and tuples are immutable, you can not add modify delete elements.

      Tuple access and processing speed faster than a list of

      List can not be dictionary keys, and tuples can.

(13) listed in the python variable data types and data types immutable?

      Variable data types: lists, dictionaries

      Immutable data types: numeric, string, a tuple

How (14) dictionaries delete key? How to merge two dictionaries?

      Delete key ----- del dict [ 'name']

      Merge two dictionaries dict1.update (dict2)

(15) How to achieve python list deduplication?

      A first set of de-emphasis, since the set is ordered and only a = set (list)

      Is then converted to a list of b = [x for x in a] for loop back to the list type

(16) to open the file, use with open ... as ... in this way what are the benefits?

      In this way you do not call the close method to close the file handle. If the direct use of open () in this way, forget to close the file handle, the consequences will file has been opened.

(17) read, readline, what is the difference between the three readlines?

      read ----- reads the entire file fo.read (2) does not fill the entire 2 shows two characters read parameter indicates a read

      readline ---- read the next line fo.readlin ()

      readlines ----- read all the lines fo.readlines ()

(18) when performing an import statement, the interpreter is how to find the module file?

      By sys.pat final decision. Usually we can PYTHONPATH to add your own module search path by modifying environment variables

(19) Under what circumstances, python object will be cleared interpreter?

      When the object has no references to it (reference count is 0)

(20) To become a catalog of what Python package needs?

      Init.py need to add files in the following directory

(21) python pass statement what's the use?

      In order to ensure the correct syntax check must enter something similar to break out of the loop statement. Skip to the next round is to continue the cycle.

Which method is to call an external program (22) python in?

      Os.system subprocess module can be used inside or functions (such check_output)

(23) python function parameters are defined inside * ** What does it mean?

      * Indicates with variable parameters; ** denotes a variable with the keyword parameters.

      The def func (a, * inList, b = 100, ** c): ** c keyword can only be placed behind a required parameter, b is the default parameters

What is the difference (24) range (10000) and xrange (10000) is?

      range and xrange are used in a loop, as the output.

      range returns a list object, and returns a xrange generator object.

      Generally larger circulation, it is recommended to use xrange, from performance considerations will be faster.

(25) python code if you have Chinese, code files should be how to deal with?

      At the top of the comments, add something like # coding = utf8 so stated, but be careful what Chinese code file encoding format, or is gbk utf8, declared encoding format can match.

      In python3, the default support Chinese, there is no need to declare a separate encoding format.

(26) how to encode unicode string to a string of utf8?

      The 'abc'.encode (' utf8 ') of course to use the decoded decode ()

(27) the difference between process and thread?

      The process is running the program, and the thread is in the process of instruction execution units. Code instructions must be executed in multithreaded operating system allocation.

      A process that contains at least one thread.

Room (28) What method of communication process?

       Shared memory, session communications can be.

(29) two threads update the contents inside a dict, how to set the meter?

      Before the code to access a shared object method to invoke acquire Lock objects to lock.

      When multiple threads execute Lock.acquire (), only one thread can successfully acquires the lock, and then continue to execute code, other threads continue to wait until you get a lock up.

      After the visit, be sure to call relese method Lock objects, unlock operation. Otherwise, other threads wait for a lock will wait forever, become a dead thread.

(30) In object-oriented, the difference new__ __ and the __init__?

       When initialization method __init__, after creating an object, it was immediately called by default, and can receive parameters

      CLS __new__ at least one parameter, representative of the current class, sub-parameters are automatically recognized by the python interpreter at instantiation.

(31) lists five python standard library?

      os ----- offers a lot of functions associated with the operating system linked

      SYS ----- commonly used command line parameters

      re ----- regular match

      math math -----

      datetime ---- processing date and time

(32) implemented by multiplying the number of two lambda functions

      sum = lambda a,b:a*b

      print(sum)

(33) How to use the multi-value digital in python?

      A binary 0 and 1, the prefix typically represent binary number 0B or ob

      Octal consisting of 0-7, the prefix is ​​generally 0o or 00 octal number

      0-15 composed of hexadecimal, prefix generally 0x or 0X hexadecimal number

The method of random integers between decimal, fractional random, 0-1 (34) python generated?

      import random

      import numpy as np

      print (random.random ()) # randomly generated Decimal 0-1

      print (random.randint (1, 10)) # randomly generated integer between 1 and 10

      print (np.randn (5)) # range 0-1, 5 decimal randomly generated

Guess you like

Origin www.cnblogs.com/peipei-Study/p/11993969.html