Python Programming


             ***PYTHON INTERVIEW QUESTION***


1) 


LISTTUPLES
Lists are mutable i.e they can be edited.Tuples are immutable (tuples are lists which can’t be edited).
Lists are slower than tuples.Tuples are faster than list.
Syntax: list_1 = [10, ‘Chelsea’, 20]Syntax: tup_1 = (10, ‘Chelsea’ , 20)

2) Arrays and lists, in Python, have the same way of storing data. But, arrays can hold only a single data type elements whereas lists can hold any data type elements.

3) Range & XRange both provide a way to generate a list of integers for you to use. The only difference is that range returns a Python list object and x range returns an xrange object.

4) Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.

5) Functions that return an iterable set of items are called generators.

6) We use *args when we aren’t sure how many arguments are going to be passed to a function, or if we want to pass a stored list or tuple of arguments to a function. 

**kwargs is used when we don’t know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments.

7) Write a program in Python to produce Star triangle

def pyfunc(r):
    for x in range(r):
        print(' '*(r-x-1)+'*'*(2*x+1))

pyfunc(9)

        *

       ***

      *****

     *******

    *********

   ***********

  *************

 ***************

*****************


8) Write a program in python to execute a bubble sort algorithm.


def bs(a):

...     b=len(a)-1

...     for x in range(b):

...             for y in range(b-x):

...                     if a[y]>a[y+1]:

...                             a[y],a[y+1]=a[y+1],a[y]

...     return a



bs([32,5,3,6,7,54,87])


[3, 5, 6, 7, 32, 54, 87]



9) Python Program for Fibonacci Number:


def Fibonacci(n):

    if n<0:

        print("Incorrect Input")

    elif n==0:

        return 0

    elif n==1:

        return 1

    else:

        return Fibonacci(n-1)+Fibonacci(n-2)




10) Write a program in Python to check if a number is prime:


def prime(n):

...     if n>1:

...             for x in range(2,n):

...                     if(n%x)==0:

...                             print("nor a prime")

...                             break

...             else:

...                     print("Prime")

...     else:

...             print("not Prime")


prime(3)

Prime

prime(4)

nor a prime


11) Write a program in Python to check if a string is a Palindrome.


def isPalindrome(str): 

...     for i in range(0, int(len(str)/2)):

...             if str[i] != str[len(str)-i-1]:

...                     return False

...     return True


s = "malayalam"

ans = isPalindrome(s)

if (ans):

...     print ("yes")

... else:

...     print ("No")

 

yes


12) Get the unique values from list:

def uniq(list1):

...     uniq_list=[]

...     for i in list1:

...             if i not in uniq_list:

...                     uniq_list.append(i)

...     return uniq_list


uniq([1,1,2,3,3,2,1,4])

[1, 2, 3, 4]


13) Program to interchange the first and last element in a list:


def swapList(newList):

...     size = len(newList)

...     temp=newList[0]

...     newList[0]=newList[size-1]

...     newList[size-1]=temp

...     return newList



print(swapList([12, 35, 9, 56, 24]))

[24, 35, 9, 56, 12]



14) Python to find sum of elements in list:


total = 0

list1 = [11, 5, 17, 18, 23]

for x in range(0,len(list1)):

...     total=total+list1[x]


print("Sum of all elements in given list: ", total) 

('Sum of all elements in given list: ', 74)



15) 











No comments:

Post a Comment

Pages