iqa

This project contains Interview questions with solution and answers

This project is maintained by AshishNamdev

Python Answers

<Back

A1-Python

This code will reverse the user_input

Output:

AIDNI

A2-Python

This code will give

Output:

1
2
3

A3-Python

Example # 1:

week_days = ['sun','mon','tue','wed','thu','fri','sun','mon','mon']
print(week_days.count('mon'))

Output:

3

Example # 2:

week_days = ['sun','mon','tue','wed','thu','fri','sun','mon','mon']
print([[x,week_days.count(x)] for x in set(week_days)])

Output:

[['wed', 1], ['sun', 2], ['thu', 1], ['tue', 1], ['mon', 3], ['fri', 1]]

A4-Python

Example:

week_days = ['sun','mon','tue','wed','thu','fri','sat']
list_as_tuple = tuple(week_days)
print(list_as_tuple)

Output:

('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')

A5-Python

Example:

week_days = ['sun','mon','tue','wed','thu','fri','sat','sun','tue']
list_as_set = set(week_days)
print(list_as_set)

Output:

set(['wed', 'sun', 'thu', 'tue', 'mon', 'fri', 'sat'])

A6-Python

Example:

week_days = ['sun','mon','tue','wed','thu','fri','sat']
list_as_str = ' '.join(week_days)
print(list_as_str)

A7-Python

Example:

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))

Output:

sun mon

A8-Python

A9-Python

This code will give

Output:

n

A10-Python

Example:

subjects = ('Python', 'Interview', 'Questions')
for i, subject in enumerate(subjects):
    print(i, subject)

Output:

0 Python
1 Interview
2 Questions

A11-Python

Example # 1 :

# *** 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.')

Output:

{'python', 'coding', 'tips', 'for', 'beginners'}
5
These are the best Python coding tips.
These are the best Python coding tips not Java tips.

Example # 2:

# *** Lets initialize an empty set
items = set()

## Add three strings.
items.add('Python')
items.add('coding')
items.add('tips')

print(items)

Output:

{'Python', 'coding', 'tips'}

A12-Python

How do you Concatenate Strings in Python?

Example:

# See how to use '+' to concatenate strings.

print('Python' + ' Interview' + ' Questions')

Output:

Python Interview Questions

A13-Python

  1. random() – This command returns a floating point number, between 0 and 1.
  2. uniform(X, Y) – It returns a floating point number between the values given as X and Y.
  3. randint(X, Y) – This command returns a random integer between the values given as X and Y.

A14-Python

A15-Python

Example:

globvar = 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

Output:

1

A16-Python

This code will give

Output:

12

Explanation:

For more details try following code

print("id(names1) = %s" % id(names1))
print("id(names2) = %s" % id(names2))
print("id(names3) = %s" % id(names3))

Output:

id(names1) = 12928504
id(names2) = 12928504
id(names3) = 12929664

you will see id(names1) and id(names2) will be same, values of id(names1), id(names2) and id(names3) will vary on every execution

A17-Python

This code will give

Output:

[1, 3, 2, 1, 3, 2]

A18-Python

list("hello") will give

Output:

['h', 'e', 'l', 'l', 'o']

A19-Python

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.

Example:

        # 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)

Output:

        {1, 3}
        {1, 2, 3}
        {1, 2, 3, 4}
        {1, 2, 3, 4, 5, 6, 8}

A20-Python

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.

Example:

        # 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)

Output:

        {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()

Example:

        # 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)

Output:

        {'r', 'H', 'l', 'e', 'W', 'd', 'o'}
        r
        {'l', 'e', 'W', 'd', 'o'}
        set()

A21-Python

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}

Set Union

Set Union

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)

Output:

        {1, 2, 3, 4, 5, 6, 7, 8}

Set Intersection

Set Intersection

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)

Output:

        {4, 5}

Set Difference

Set Difference

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)

Output:

       {1, 2, 3}

Set Symmetric Difference

Set Symmetric Difference

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)

Output:

       {1, 2, 3, 6, 7, 8}

<Back