Python Program Output | Set 1

Python Methods and Functions

Program 1:

r = lambda q: q * 2

s = lambda q: q * 3

x = 2

x = r (x)

x = s (x)

x = r (x)

print x

Exit :

 24 

Explanation: In the above program, r and s are lambda functions or anonymous functions, and q is an argument to both functions. In the first step, we initialized x as 2. In the second step, we passed x as an argument to the lambda function r, this will return x * 2, which is stored in x. That is, x = 4 now. Similarly, in the third step, we passed x to the lambda function s, so x = 4 * 3.i.e. x = 12 now. Again, in the last step, x is multiplied by 2, passing it to r. Therefore, x = 24.

Program 2:

a = 4.5

b = 2

print a / / b

Exit:

 2.0 

Explanation: This type of division is called

a = True

b = False

c = False

  

if a or b and c:

print "GEEKSFORGEEKS"

else :

print "pythonengineering"

Exit:

GEEKSFORGEEKS

Explanation: In Python, the AND operator takes precedence over the OR operator. So this is evaluated first. those. (b and c) evaluates to false. The OR operator is now evaluated. Here (True or False) evaluates to True. Thus, the if condition becomes True and GEEKSFORGEEKS is outputted as output.

Program 4:

a = True

b = False

c = False

  

if not a or b:

print 1

elif not a or not b and c:

print 2

elif not a or b or not b and a:

print 3

else :

print 4

Output:

 3 

Explanation: In Python, the priority order is — first NOT, and then AND and in the last OR. Thus, the if condition and the second elif condition evaluates to False, while the third elif condition evaluates to True, which results in 3 as output.

Program 5:

count = 1  

 

def doThis ():

 

global count

 

for i in ( 1 , 2 < / code> , 3 ): 

count + = 1

 
doThis ()

 

print count

Output:

 4 

Explanation: the count variable outside the function is a global variable, and the count variable that the function refers to is the same global variable defined outside the function. This way, changes made to the variable in the function are reflected in the original variable. So, the output of the program — 4.

Python Quizzes

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.





Get Solution for free from DataCamp guru