

















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















































































Top 25 Python Questions:
If a variable is defined outside function then it is implicitly global. If variable is assigned new value inside the function means it is local. If we want to make it global we need to explicitly define it as global. Variable referenced inside the function are implicit global. Following code snippet will explain further the difference #!/usr/bin/python
def fun1(a): print 'a:', a a= 33; print 'local a: ', a a = 100 fun1(a) print 'a outside fun1:', a def fun2(): global b print 'b: ', b b = 33 print 'global b:', b b = fun2() print 'b outside fun2', b
Output $ python variable_localglobal.py a: 100 local a: 33 a outside fun1: 100 b : global b: 33 b outside fun2: 33
config.a = 1 config.b = config.c= print “ a, b & resp. are : “ , config.a, config.b, config.c
output of module1.py will be 1 2 3
Output - ('i :', 64) ('j :', 701.85008797642115) ('k :', 0.18173593240301023)
('Sunday', 'Mondays', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday') Sets stores unordered values & have no index. And unlike Tuples and Lists, Sets can have no duplicate data, It is similar to Mathematical sets. add() function can be used to add element to a set. update() function can be used to add a group of elements to a set. Copy() function can be used to create clone of set. Set Code Snippet: disneyLand = set (['Minnie Mouse', 'Donald Duck', 'Daisy Duck', 'Goofy']) disneyLand.add('Pluto') print disneyLand
Output set(['Goofy', 'Daisy Duck', 'Donald Duck', 'Minnie Mouse', ’Pluto’]) Dictionary are similar to what their name is. In a dictionary, In python, the word is called a 'key', and the definition a 'value'. Dictionaries consist of pairs of keys and their corresponding values. Dictionary Code Snippet: >>> dict = {'India': 'Bharat', 'Angel': ‘Mother Teresa’, 'Cartoon': 'Mickey'} >>>print dict[India] Bharat >>>print dict[Angel] Mother Teresa
Answer:d Explanation:Dictionary stores values in terms of keys and values.
a) print(‘hello’, ‘how’, ‘are’, ‘you’) b) print(‘hello’, ‘how’, ‘are’, ‘you’ + ‘-’ * 4) c) print(‘hello-’ + ‘how-are-you’) d) print(‘hello’ + ‘-’ + ‘how’ + ‘-’ + ‘are’ + ‘-’ + ‘you’) Answer:c,d Explanation:Execute in the shell.
Explanation:String literals seperated by white space are allowed. They are concatenated.
a) None None b) None 22 c) 22 None d) Error is generated Answer:d Explanation:self.o1 was never created.
d) None Answer:b Explanation:Starts with checks if the given string starts with the parameter that is passed.
d) World Answer:d Explanation:Execute help(string.strip) to find details.
Answer:a,b Explanation:Execute in shell to verify. 4. If a class defines the str(self) method, for an object obj for the class, you can use which command to invoke the str method.(multiple answers allowed) a) obj.str() b) str(obj) c) print obj d) str(obj) Answer:a,b,c Explanation:Execute in shell to verify.
Answer:c Explanation:Execute in the shell objects cannot have same id, however in the case of strings its different.