

















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Here are python interview questions asked for data science jobs.
Typology: Exams
1 / 89
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
str="hello" str[:2] str a) he b) lo c) olleh d) hello Answer:d Explanation: Strings are immutable and hence the answer is d.
x = 13? 2 objective is to make sure x has a integer value, select all that apply (python 3.xx) a) x = 13 // 2 b) x = int(13 / 2)
Answer:d Explanation:Dictionary stores values in terms of keys and values.
grade1 = 80 grade2 = 90 average = (grade1 + grade2) / 2 a) 85 b) 85. c) 95 d) 95. Answer:b Explanation:Cause a decimal value to appear as output.
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.
"a"+"bc" a) a b) bc c) bca d) abc Answer:d Explanation:+ operator is concatenation operator.
"abcd"[2:] a) a b) ab c) cd d) dc Answer:c Explanation:Slice operation is performed on string.
Explanation:String literals seperated by white space are allowed. They are concatenated.
print '\x97\x98' a) Error b) 97 98 c) _~ d) \x97\x Answer:c Explanation:\x is an escape sequence that means the following 2 digits are a hexadicmal number encoding a character.
str1="helloworld" str1[::-1] a) dlrowolleh
b) hello c) world d) helloworld Answer:a Explanation:Execute in shell to verify.
obj = child(22) print "%d %d" % (obj.o1, obj.o2)
a) None None b) None 22 c) 22 None d) Error is generated Answer:d Explanation:self.o1 was never created.
temp = tester(12) print temp.id a) 224 b) Error c) 12 d) None Answer:c Explanation:id in this case will be the attribute of the class.
example = "snow world" print "%s" % example[4:7] a) wo b) world c) sn d) rl Answer:a Explanation:Execute in the shell and verify.
example = "snow world" example[3] = 's' print example
str1="helloworld" >>>str1[::-1] a) dlrowolleh b) hello c) world d) helloworld Answer:a Explanation:Execute in shell to verify. 10. print 0xA + 0xB + 0xC : a) 0xA0xB0xC b) Error c) 0× d) 33 Answer:d Explanation:0xA and 0xB and 0xC are hexadecimal integer literals representing the decimal values 10,11 and 12 respectively. There sum is 33. This set of Python Questions & Answers focuses on “Strings”. 1. What is the output of the following code? class father: def init(self, param): self.o1 = param class child(father): def init(self, param): self.o2 = param >>>obj = child(22) >>>print "%d %d" % (obj.o1, obj.o2) a) None None b) None 22 c) 22 None d) Error is generated Answer:d Explanation:self.o1 was never created. 2. What is the output of the following code? class tester: def init(self, id): self.id = str(id) id="224" >>>temp = tester(12) >>>print temp.id a) 224 b) Error c) 12 d) None Answer:c Explanation:id in this case will be the attribute of the class. 3. What is the output of the following code? >>>example = "snow world" >>>print "%s" % example[4:7] a) wo b) world c) sn d) rl Answer:a Explanation:Execute in the shell and verify. 4. What is the output of the following code? >>>example = "snow world" >>>example[3] = 's' >>>print example a) snow b) snow world str1[::-1] a) dlrowolleh b) hello c) world d) helloworld Answer:a Explanation:Execute in shell to verify. 10. print 0xA + 0xB + 0xC : a) 0xA0xB0xC b) Error c) 0× d) 33 Answer:d Explanation:0xA and 0xB and 0xC are hexadecimal integer literals representing the decimal values 10,11 and 12 respectively. There sum is 33. This set of Python Questions & Answers focuses on “Strings”. 1. What is the output of the following code? class father: def init(self, param): self.o1 = param class child(father): def init(self, param): self.o2 = param >>>obj = child(22) >>>print "%d %d" % (obj.o1, obj.o2) a) None None b) None 22 c) 22 None d) Error is generated Answer:d Explanation:self.o1 was never created. 2. What is the output of the following code? class tester: def init(self, id): self.id = str(id) id="224" >>>temp = tester(12) >>>print temp.id a) 224 b) Error c) 12 d) None Answer:c Explanation:id in this case will be the attribute of the class. 3. What is the output of the following code? >>>example = "snow world" >>>print "%s" % example[4:7] a) wo b) world c) sn d) rl Answer:a Explanation:Execute in the shell and verify. 4. What is the output of the following code? >>>example = "snow world" >>>example[3] = 's' >>>print example a) snow b) snow world c) Error d) snos world Answer:c Explanation:Strings cannot be modified.
d) None Answer:b Explanation:Starts with checks if the given string starts with the parameter that is passed.
chr(ord('A')) a) A b) B c) a d) Error Answer:a Explanation:Execute in shell to verify.
print(chr(ord('b')+1)) a) a b) b c) c d) A View Answer Answer:c Explanation:Execute in the shell to verify.
d) World Answer:d Explanation:Execute help(string.strip) to find details.
print("D", end = ' ') print("C", end = ' ') print("B", end = ' ') print("A", end = ' ') a) DCBA b) A, B, C, D c) D C B A d) A, B, C, D will be displayed on four lines Answer:c Explanation:Execute in the shell.
print(format("Welcome", "10s"), end = '#') print(format(111, "4d"), end = '#') print(format(924.656, "3.2f")) a) Welcome# 111#924. b) Welcome#111#924. c) Welcome#111#. d) Welcome # 111#924. Answer:d
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.