Python pen question 42-69

Source: Internet
Author: User
Tags function prototype generator shallow copy python decorator

42. How to copy an object in Python and explain the difference between them

A: There are two ways to copy in Python, deep copy and shallow copy

Shallow copy: With the assignment symbol (=), the memory address is common, a copy of the object is changed

Deep copy: Using the Deepcopy method under the Copy module, each Copy object is a separate memory address

43. Talk about your understanding of the Python decorator

A: The adorner is an inline function

The ability to add decorative functions without changing the original function code

44. Match () in Python differs from search ()

A: Match is matched from the first bit of the string and cannot be found if the first bit is different

Search is to find a matching string in the string, the first one is different to find the next

45. Gets the number of elements of the list, and the method used to append the elements to the end is

Answer: Len (),. Append ()

46, Judge Dict there is a key method is

Answer: py2.7 (dict. Has_key(key)) py3 key in Dict

47, L=range (100), take the first to the third element (L[:3]), take the second-to-last element (L[-2]), take 10 (l[-11:-1])

Copy L to L1 (L1=copy.deepcopy (L)), non-reference pass

48, D = {' A ': 1, ' B ': 2, ' C ': 3} Please print Key,value

Answer: For k,v in D.values (): Print k,v

49, how to tell if a variable is a string?

Answer: Type (a) is str

50. What is the difference between list and touple?

A: The elements within the list are variable, the elements within the touple are immutable, and the same-length list and touple,list occupy large memory

51. What is the difference between xrange and range?

A: Range generates a list, Xrange generates a generator

52. How do you change to [' 1 ', ' 2 ', ' 3 ']? [' 1 ', ' 2 ', ' 3 '] how to become [a]?

Answer: ' Int,b '. Split (', ') map

53. Def add_end (l=[]):

L.append (' END ')

Return L

>>>add_end () output what---------------[' End ']

>>>add_end () Call output again what? Why--------------[' End ', ' end '] because the list is pointing to the original

54, [36,5,12,9,21] how to sort?

Answer: Sorted ([36,5,12,9,21])

55. def func (a,b,c=0,*args,**kw):

Pass

What is the role of *args,**kw?

Answer: Accept the additional parameters

56, is and = = difference?

Answer: Is compare memory address and value, = = Compare value only

57. How to generate [1,4,9,16,25,36,49,64,81,100]? Try to implement it in one line?

Answer: [I*i for I in Range (1,11)]

58. What is the generator? What's the effect? Please write a generator

A: The essence of the generator is an iterator that enables lazy generation, generating a data when a data is needed

Def gen ():

For I in range (10):

Yield I

59, Map (str,[1,2,3,4,5,6,7,8,9]) output what?

Answer: [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ']

60, please write the implementation of log (the main function of printing the name of the letter)

@log

def now ():

print ' 123455 '

>>>now ()

Output Call Now (): ' 123455 '

For:

def log (func):
def inner (*args,**kwargs):
print ' Call%s: '% func.__name__
ret = func (*args,**kwargs)
return ret
return inner

61. The following description is correct

The function of a, continue statement is to end the entire loop of execution B, can only use break in the loop body and within the switch statement

C, use continue in the loop and break function same D, exit from multi-layer loop nesting, only use goto statement

62. How Python defines a function

A, class<name> (<TYPE>ARG1,<TYPE>ARG2) B, function<name> (ARG1,ARG2)

   C, def <name> (arg1,arg2) D, Def <name> (<TYPE>ARG1,<TYPE>ARG2)

63. The following function can create sub-processes under Linux

A, Os.popenB, os.forkC, OS.SYSTEMD, Os.link

64, known x=43,ch= ' A ', y=1, then the expression (x>y and ch < ' B ' and y) value is

A, 0 B, 1 C, error D, True (' true ')

65, the following statement that will be infinite loop down

Where the Range function prototype is range ([Start],stop[,step])

A, for a in range: Time.sleep (Ten) B, while 1 < 10:time.sleep (Ten)

C, while True:break D, A=[3,-1, ', '] for I in a[:]: If a >10:break;

66, returns true in the following expression is

A, 3>2>2 B, ' abc ' > ' xyz ' C, 0x56<56 D, (3,2) < (' A ', ' B ')

67. Data types not supported by Python are

  A, Char B, int C, float D, list

68, the following function to convert which is the correct

A, int (' ABcDef ') B, float ("') C, BOOL ((3," ")) D, str (')

69, the following function, those will output three a number

A, for I in range (3):p rint i B, alist = [0,1,2] for i in Alist:print i+1

C, i = 1 while i <3:print i i=i+1 D, for I in range (3):p rint i+1

Python pen question 42-69

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.