This project contains Interview questions with solution and answers
This project is maintained by AshishNamdev
This code will reverse the user_input
AIDNI
This code will give
1
2
3
count()
function.week_days = ['sun','mon','tue','wed','thu','fri','sun','mon','mon']
print(week_days.count('mon'))
3
week_days = ['sun','mon','tue','wed','thu','fri','sun','mon','mon']
print([[x,week_days.count(x)] for x in set(week_days)])
[['wed', 1], ['sun', 2], ['thu', 1], ['tue', 1], ['mon', 3], ['fri', 1]]
tuple()
function we can convert a list into a tuple. But we can’t change the list after turning it into tuple, because it becomers immutable.week_days = ['sun','mon','tue','wed','thu','fri','sat']
list_as_tuple = tuple(week_days)
print(list_as_tuple)
('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
set()
function.week_days = ['sun','mon','tue','wed','thu','fri','sat','sun','tue']
list_as_set = set(week_days)
print(list_as_set)
set(['wed', 'sun', 'thu', 'tue', 'mon', 'fri', 'sat'])
.join()
method which joins all the elements into one and returns as a string.week_days = ['sun','mon','tue','wed','thu','fri','sat']
list_as_str = ' '.join(week_days)
print(list_as_str)
yield
keyword in Python can turn any function into a generator. Yields work like a standard return keyword.
But it’ll always return a generator object. Also, a function can have multiple calls to the yield
keyword.def test_gen(index):
week_days = ['sun','mon','tue','wed','thu','fri','sat']
yield week_days[index]
yield week_days[index+1]
day = testgen(0)
print(next(day), next(day))
sun mon
Arrays
and Lists
. Positive index reads the elements from the starting of an array
or list
but in the negative index, Python reads elements from the end of an array
or list
.This code will give
n
enumerate()
function adds a counter to an iterable object. enumerate()
function can accept sequential indexes starting from zero.subjects = ('Python', 'Interview', 'Questions')
for i, subject in enumerate(subjects):
print(i, subject)
0 Python
1 Interview
2 Questions
set
is a kind of collection. It has been part of Python since version 2.4.
A set contains an unordered collection of unique and immutable objects.# *** Create a set with strings and perform the search in a set
objects = {'python', 'coding', 'tips', 'for', 'beginners'}
# Print set.
print(objects)
print(len(objects))
# Use of 'in' keyword.
if 'tips' in objects:
print('These are the best Python coding tips.')
# Use of 'not in' keyword.
if 'Java tips' not in objects:
print('These are the best Python coding tips not Java tips.')
{'python', 'coding', 'tips', 'for', 'beginners'}
5
These are the best Python coding tips.
These are the best Python coding tips not Java tips.
# *** Lets initialize an empty set
items = set()
## Add three strings.
items.add('Python')
items.add('coding')
items.add('tips')
print(items)
{'Python', 'coding', 'tips'}
How do you Concatenate Strings in Python?
+
to concatenate strings.# See how to use '+' to concatenate strings.
print('Python' + ' Interview' + ' Questions')
Python Interview Questions
random()
– This command returns a floating point number, between 0 and 1.uniform(X, Y)
– It returns a floating point number between the values given as X
and Y
.randint(X, Y)
– This command returns a random integer between the values given as X
and Y
.print sum(range(1,101))
sum
funtion print sum of the elements of range funtion, i.e 1 to 100
.global
variable in other functions by declaring it as global
in each function that assigns to itglobvar = 0
def set_globvar_to_one():
global globvar # Needed to modify global copy of globvar
globvar = 1
def print_globvar():
print(globvar) # No need for global declaration to read value of globvar
set_globvar_to_one()
print_globvar() # Prints 1
1
This code will give
12
names1
and names2
both refers to the same memory space, so names2[0] = 'Alice'
will modify the
first memebr of the list, and it will become ['Alice', 'Bear', 'Charlton', 'Daman']
names3
is a new list in memoery due to slicing names3 = names1[:]
, so names3[1] = 'Bob'
will make it ['Amir', 'Bob', 'Charlton', 'Daman']
ls
in for
loop will refer names1
, names2
and names3
respectivelynames1
and names2
both have 'Alice'
at first index, so sum
will be incremented by 1
in
if ls[0] == 'Alice'
conditionnames3
contains 'Bob'
as second value (index 1)
, so sum
will be added by 10
in
if ls[1] == 'Bob'
conditionFor more details try following code
print("id(names1) = %s" % id(names1))
print("id(names2) = %s" % id(names2))
print("id(names3) = %s" % id(names3))
id(names1) = 12928504
id(names2) = 12928504
id(names3) = 12929664
you will see
id(names1)
andid(names2)
will be same, values ofid(names1)
,id(names2)
andid(names3)
will vary on every execution
This code will give
[1, 3, 2, 1, 3, 2]
list("hello")
will give
['h', 'e', 'l', 'l', 'o']
Sets are mutable. But since they are unordered, indexing have no meaning.
We cannot access or change an element of set using indexing or slicing. Set does not support it.
We can add single element using the add()
method and multiple elements using the update()
method. The update()
method can take tuples, lists, strings or other sets as its argument. In all cases, duplicates are avoided.
# initialize my_set
my_set = {1,3}
print(my_set)
# if you uncomment line 9,
# you will get an error
# TypeError: 'set' object does not support indexing
#my_set[0]
# add an element
my_set.add(2)
print(my_set)
# add multiple elements
my_set.update([2,3,4])
print(my_set)
# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4,5], {1,6,8})
print(my_set)
{1, 3}
{1, 2, 3}
{1, 2, 3, 4}
{1, 2, 3, 4, 5, 6, 8}
A particular item can be removed from set using methods, discard()
and remove()
.
The only difference between the two is that, while using discard()
if the item does not exist in the set, it remains unchanged. But remove()
will raise an error in such condition.
# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set)
# discard an element
my_set.discard(4)
print(my_set)
# remove an element
my_set.remove(6)
print(my_set)
# discard an element
# not present in my_set
my_set.discard(2)
print(my_set)
# remove an element
# not present in my_set
# If you uncomment line 27,
# you will get an error.
my_set.remove(2)
print(my_set)
{1, 3, 4, 5, 6}
{1, 3, 5, 6}
{1, 3, 5}
{1, 3, 5}
Traceback (most recent call last):
File "<stdin>", line 27, in <module>
my_set.remove(2)
KeyError: 2
Similarly, we can remove and return an item using the pop()
method.
Set being unordered, there is no way of determining which item will be popped. It is completely arbitrary.
We can also remove all items from a set using clear()
# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set)
# discard an element
# Output: {1, 3, 5, 6}
my_set.discard(4)
print(my_set)
# remove an element
# Output: {1, 3, 5}
my_set.remove(6)
print(my_set)
# discard an element
# not present in my_set
# Output: {1, 3, 5}
my_set.discard(2)
print(my_set)
# remove an element
# not present in my_set
# If you uncomment line 27,
# you will get an error.
# Output: KeyError: 2
my_set.remove(2)
print(my_set)
{'r', 'H', 'l', 'e', 'W', 'd', 'o'}
r
{'l', 'e', 'W', 'd', 'o'}
set()
Sets can be used to carry out mathematical set operations like union, intersection, difference and symmetric difference. We can do this with operators or methods.
Let us consider the following two sets for the following operations.
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
Union of A
and B
is a set of all elements from both sets.
Union is performed using |
operator. Same can be accomplished using the method union()
.
# initialize A and B
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use | operator
print(A | B)
{1, 2, 3, 4, 5, 6, 7, 8}
Intersection of A
and B
is a set of elements that are common in both sets.
Intersection is performed using &
operator. Same can be accomplished using the method intersection()
# initialize A and B
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use & operator
print(A & B)
{4, 5}
Difference of A
and B
(A - B)
is a set of elements that are only in A
but not in B
. Similarly, B - A
is a set of element in B
but not in A
.
Difference is performed using -
operator. Same can be accomplished using the method difference()
# initialize A and B
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use - operator on A
print(A - B)
{1, 2, 3}
Symmetric Difference of A
and B
is a set of elements in both A
and B
except those that are common in both.
Symmetric difference is performed using ^
operator. Same can be accomplished using the method symmetric_difference()
# initialize A and B
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use ^ operator
print(A ^ B)
{1, 2, 3, 6, 7, 8}