data-flair/Python/ Interview questions level 1 Flashcards
Q.1. What are the key features of Python?
If it makes for an introductory language to programming, Python must mean something. These are its qualities:
Interpreted Dynamically-typed Object-oriented Concise and simple Free Has a large community
Q.2. Differentiate between lists and tuples
A list is mutable, but a tuple is immutable
> > > mylist=[1,3,3]
mylist[1]=2
mytuple=(1,3,3)
mytuple[1]=2
Traceback (most recent call last):
File “”, line 1, in
mytuple[1]=2
TypeError: ‘tuple’ object does not support item assignment
Q.4. What are negative indices?
mylist=[0,1,2,3,4,5,6,7,8]
mylist[-3]
6
Q.5. Is Python case-sensitive?
> > > myname=’Ayushi’
Myname
Traceback (most recent call last):
File “”, line 1, in
Myname
NameError: name ‘Myname’ is not defined
Q.6. How long can an identifier be in Python?
According to the official Python documentation, an identifier can be of any length. However, PEP 8 suggests that you should limit all lines to a maximum of 79 characters. Also, PEP 20 says ‘readability counts’
It can only begin with an underscore or a character from A-Z or a-z.
Keywords cannot be used as identifiers
Q.7. How would you convert a string into lowercase?
> > > ‘AyuShi’.lower()
‘ayushi’
Q.8. What is the pass and break statements in Python?
There may be times in our code when we haven’t decided what to do yet, but we must type something for it to be syntactically correct. In such a case, we use the pass statement.
> > > def func(*args):
pass
the break statement breaks out of a loop.
> > > for i in range(7):
if i==3: break
the continue statement skips to the next iteration.
for i in range(7):
if i==3:
continue
Outcome of:
for i in range(7): if i==3: continue print(i)
0 1 2 4 5 6
Q.9. Explain help() and dir() functions in Python
The help() function displays the documentation string and help for its argument.
import copy
help(copy.copy)
The dir() function displays all the members of an object(any kind) [‘\_\_annotations\_\_’, ‘\_\_call\_\_’, ‘\_\_class\_\_’, ‘\_\_closure\_\_’, ‘\_\_code\_\_’, ‘\_\_defaults\_\_’, ‘\_\_delattr\_\_’, ‘\_\_dict\_\_’,...
Q.10. How do you get a list of all the keys in a dictionary?
mydict={‘a’:1,’b’:2,’c’:3,’e’:5}
mydict.keys()
Outcome:
dict_keys([‘a’, ‘b’, ‘c’, ‘e’])
Q.11. What is slicing?
Slicing is a technique that allows us to retrieve only a part of a list, tuple, or string. For this, we use the slicing operator [].
(1,2,3,4,5)[2:4]
Outcome:
(3, 4)
Q.12. How would you declare a comment in Python?
All it has is octothorpe (#). Anything following a hash is considered a comment, and the interpreter ignores it.
#line 1 of comment
Q.13. How will you check if all characters in a string are alphanumeric?
‘fsdfsdf42’.isalnum()
Outcome:
True
Q.14. How will you capitalize the first letter of a string?
‘ayushi’.capitalize()
‘Ayushi’
Q.15. We know Python is all the rage these days. But to be truly accepting of a great technology, you must know its pitfalls as well. Would you like to talk about this?
Python’s interpreted nature imposes a speed penalty on it.
While Python is great for a lot of things, it is weak in mobile computing, and in browsers.
Being dynamically-typed, Python uses duck-typing (If it looks like a duck, it must be a duck). This can raise runtime errors.
Python has underdeveloped database access layers. This renders it a less-than-perfect choice for huge database applications.
And then, well, of course. Being easy makes it addictive. Once a Python-coder, always a Python coder.
Q.16. With Python, how do you find out which directory you are currently in?
import os
os.getcwd()
Outcome:
‘/content’
Q.17. How do you insert an object at a given index in Python?
a=[1,2,4]
a.insert(2,3)
a
Outcome:
[1, 2, 3, 4]
Q.18. And how do you reverse a list?
a=[1,2,4]
a.reverse()
a
Outcome:
[4, 2, 1]
Q.19. What is the Python interpreter prompt?
> > >
Q.20. How does a function return values?
A function uses the ‘return’ keyword to return a value.
def add(a,b): return a+b
add(3,4)
Outcome:
7
Q.21. How would you define a block in Python?
we must end such statements with colons and then indent the blocks under those with the same amount.
if 3>1:
print(“Hello”)
Q.22. Why do we need break and continue in Python?
Both break and continue are statements that control flow in Python loops. break stops the current loop from executing further and transfers the control to the next block. continue jumps to the next iteration of the loop without exhausting it
Q.24. In one line, show us how you’ll get the max alphabetical character from a string
max(‘flyiNg’)
Outcome:
y
Q.25. What is Python good for?
Python is a jack of many trades, check out Applications of Python to find out more.
Meanwhile, we’ll say we can use it for:
Web and Internet Development Desktop GUI Scientific and Numeric Applications Software Development Applications Applications in Education Applications in Business Database Access Network Programming Games, 3D Graphics Other Python Applications
Outcome of complex(3.5,4) :
(3.5+4j)
Outcome of:
eval(‘print(max(22,22.0)-min(2,3))’)
eval()- Parses a string as an expression.
20
Q.27. What will the following code output?
word= ‘abcdefghij’
word[:3]+word[3:]
‘abcdefghij’
Q.28. How will you convert a list into a string?
nums=[‘one’,’two’,’three’,’four’,’five’,’six’,’seven’]
s=’ ‘.join(nums)
s
Outcome:
‘one two three four five six seven’