Python Program Output | Set 5

Counters | Python Methods and Functions

Program 1:

def gfgFunction ():

"Geeksforgeeks is cool website for boosting up technical skills"

return 1

 

print gfgFunction .__ doc __ [ 17 : 21 ]

Exit:

 cool 

Explanation:
There is a docstring defined for this method by putting the line on the first line after start defining the function. The docstring can be referenced using the function's __doc__ attribute. 
And hence it prints the indexed line.

Program 2:

class A ( object ):

val = 1

 

class B (A):

pass

 

class C (A):

pass

 

print A.val, B.val, C. val

B.val = 2

print A.val, B.val, C.val

A.val = 3

print A.val, B.val, C.val

Exit:

 1 1 1 1 2 1 3 2 3 

Explanation:
In Python, class variables are internally treated like dictionaries. If the variable name is not found in the dictionary of the current class, the class hierarchy (ie its parent classes) is searched until the specified variable name is found; if the variable is not found, an error is thrown. 
So, in the above program, the first call to print () prints the initialized value, i.e. 1.
In the second call, since B. val is set to 2, the output is 1 2 1.
The last conclusion 3 2 3 may be surprising. Instead of 3 3 3 here B.val reflects 2 instead of 3 because it is overridden earlier.

Program 3:

check1 = [ ' Learn' , 'Quiz' , ' Practice ' , ' Contribute' ]

check2 = check1

check3 = check1 [:]

 

check2 [ 0 ] = ' Cod e'

check3 [ 1 ] = 'Mcq'

  

count = 0

for c in (check1, check2, check3):

if c [ 0 ] = = 'Code' :

count + = 1

if c [ 1 ] = = 'Mcq' :

count + = 10

 

print count

Output :

 12 

Explanation:
When assigning check1 to check2, we create a second link to the same list. Changes to check2 affect check1. When we slice all the elements in check1 to check3, we create a complete copy of check1 that can be modified independently (ie, any change in check3 will not affect check1). 
Thus, when checking check1 & # 39; Code & # 39; is matched and the count increases to 1, but the Mcq doest is matched as it is only available in check3. 
Now checking check2, here is also & # 39; Code & # 39; matches, resulting in a counter value of 2.
Finally, when checking check3, which is separate from check1 and check2, only Mcq is compared here, and count becomes 12.

Program 4:

def gfg (x, l = []):

  for i in range (x):

  l.append (i * i)

print (l) 

  

gfg ( 2 )

gfg ( 3 , [ 3 , 2 , 1 ] )

gfg ( 3 )

Exit:

 [0, 1] [3, 2, 1, 0, 1, 4] [0, 1, 0, 1, 4] 

Explanation:
The first function call should be obvious enough, the loop adds 0 and then 1 to an empty list l. l — a variable name that points to a list stored in memory. The second call begins by creating a new list in a new block of memory. Then I refer to this new list. He then adds 0, 1 and 4 to this new list. So this is great. The third function call — strange. The original list saved in the original block of memory is used. That's why it starts with 0 and 1.

This article courtesy of Harsh Agarwal . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.





Tutorials