Little Turtle 52nd: Think Like a geek.

Source: Internet
Author: User
Tags integer numbers

What does the following code print?
0.
[code]>>> def func ():
Pass

>>> Print (Type (func ())) [/code]
Of course the type of the print function, because nothing is returned, that is, "Nonetype"
1.
[code]>>> print (Type (1J)) [/code]
elimination method, no U-open question, so not "Unicode", not integer, nor string, that is "mixed type"
2.
[code]>>> print (Type (lambda:none)) [/code]
At first I thought it was nonetype, and it turned out to be a function, and as a result, help (lambda) found this sentence:
The expression ' lambda arguments:expression ' yields a function object.
That is, no matter how the lambda is defined, the return is a function object
3.
[code]>>> a = [1, 2, 3, "FISHC", (' A ', ' B ', ' C '), [], None]
>>> print (Len (a)) [/code]
The length of the list A is the number of the outermost comma-separated things there are several, the number of the university has 7, respectively, 3 integer numbers, string one, Ganso one, empty list one plus a null value one.
4.
[Code]class A:
def __init__ (self, x):
x = x + 1
SELF.V1 = X

Class B (A):
def __init__ (self, x):
x = x + 1
Self.v2 = X

>>> B = B (8)
>>> print ("%d%d"% b.v1, b.v2) [/code]
First, the turtle does not pay attention to the indentation problem, in the initialization function of Class A (if I do not understand the error).
If this is the case, then B as an instance of Class B object, should be unrelated to a, otherwise, how to Leia properties V1? Should you throw an exception? And there is a syntax error in the PRINT statement there, should be% (B.V1, b.v2) or just print the former meaning, unless Py3 changed the grammar, because I use Py2 ha.

5.
[Code]class A:
def __init__ (self, x):
self.x = X
x = 666

>>> a = A ()
>>> A = a (888)
>>> A.x[/code]
First, the Class A at the time of definition is required to pass the parameter x, so, A=a () will be error, and the definition of the time x=666 this is often dozens of meaningless, because X is only a parameter, when the call to a, the passed parameter will overwrite the X, so 666 of the value of the constant is meaningless, the result will only be 888.
6.
[Code]values = [1, 1, 2, 3, 5]
Nums = Set (values)

def check (num):
If num in nums:
Return True
Else
Return False

For I in filter (Checkit, values):
Print (I, end= ") [/code]
Well, the little turtle tired, inside the code has been misspelled, the small turtle is really the millennium of the workers, in this allowed under the burning of a moxa incense, in fact, it must be printed 1 1 2 3 5, first, set is a set, which is generally a list of such an iterative object; second, the filter (function or None, Sequence) This function returns the sequence of "function" as true. This is the return of all the elements in the values list, where the elements are separated by a space.
7.
[Code]values = [1, 1, 2, 3, 5]
def transform (num):
Return num * * 2

For I in map (transform, values):
Print (I, end= ") [/code]
The map function is used to tell the argument list that each element in the "values" function as the parameter "transform" returns a list, so it returns 1 1 4 9 25
8.
[Code]class A:
def __init__ (self, x):
self.x = X

A = a (100)
a.__dict__[' y '] = 50
Print (A.y + len (a.__dict__)) [/code]
First, __dict__ is used to define class properties and for the values that the class properties and corresponding values are stored in the class in a dictionary, so a.y is 50, and there are two properties, Y and X, respectively, so the result is 52.
9.
[Code]class A:
def __init__ (self):
Pass
def get (self):
Print (__name__)

>>> a = A ()
>>> a.get () [/code]
The function itself is executed, so the __main__ is printed, and when the module is called, the __name__== module name
10.
[Code]country_counter = {}

def addone (country):
If country in Country_counter:
Country_counter[country] + = 1
Else
Country_counter[country] = 1

AddOne (' China ')
AddOne (' Japan ')
AddOne (' China ')
AddOne ("American")

Print (len (country_counter)) [/code]
The length of the printed dictionary is 3, and the key value pairs of the dictionary are the number of arguments and parameters of the AddOne function respectively, so the length is 3 different countries.
11.
[Code]dict1 = {}
DICT1[1] = 1
dict1[' 1 '] = 2
dict1[1.0] = 3

result = 0
For each in Dict1:
Result + = Dict1[each]

Print (result) [/code]
Since the assignment of the element duplicate key in the field is overwritten, the 1 is re-copied to 3 and the result is result=3+2=5
12.
[Code]def Dostuff (param1, *param2):
Print type (param2)

Dostuff (' apples ', ' bananas ', ' cherry ', ' dates ') [/code]
Dostuff There are two parameters, one is param1= ' apples ', the other parameter with a * is a sequence type parameter, *params2= (' Bananas ', ' cherry ', ' dates '), so the second parameter is a tuple type.
13.
[Code]class A:
def __init__ (self, A, B, c):
self.x = a + B + C

A = A (A/b)
b = GetAttr (A, ' X ')
SetAttr (A, ' X ', b+1)
Print A.x[/code]
First, GetAttr actually returns the value of the instance object property x B=1+2+3=6,setattr is the value of the attribute x that redefined the instance object is 7, so the result is 7.
14.
[Code]list1 = [1, 2]
List2 = [3, 4]

Dict1 = {' 1 ': List1, ' 2 ': List2}
Dict2 = Dict1.copy ()

dict1[' 1 '][0] = 5

result = dict1[' 1 '][0] + dict2[' 1 '][0]
Print (result) [/code]
The result is 5+5=10, in fact the copy () function simply points the index to Dict1 only, Dict1 changed, then Dict2 will also follow the change, is a shallow copy, there is no root copy. Unlike traditional cloning, it is important to know that a single individual is cloned.
15.
[Code]import Copy

List1 = [1, 2]
List2 = [3, 4]

Dict1 = {' 1 ': List1, ' 2 ': List2}
Dict2 = Copy.deepcopy (Dict1)

dict1[' 1 '][0] = 5

result = dict1[' 1 '][0] + dict2[' 1 '][0]
Print (result) [/code]
The Deepcopy () function of the copy module is called to achieve cloning in the traditional sense, and the result is result=5+1=6

Little Turtle 52nd: Think Like a geek.

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.