Saturday, June 29, 2019

python interview questions part 1


Question 1:
x = 6
print(6)
print("6")
what is the out put for the above code??

Question 2:

value1 = eval(input('Please enter a number: '))
value2 = eval(input('Please enter another number: '))
sum = value1 + value2
print(value1, '+', value2, '=', sum)

what is output for the above code??


Question 3:

print(10/3, 3/10, 10//3, 3//10)

out put of the above print??





input function in python


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM

*****************************************************"""
print('Please enter an integer value:')
x = input()
print('Please enter another integer value:')
y = input()
num1 = int(x)
num2 = int(y)
print(num1, '+', num2, '=', num1 + num2)

output:
Please enter an integer value:

25
Please enter another integer value:

52
25 + 52 = 77

typed function use in python

print('Please enter some text:')
x = input()
print('Text entered:', x)
print('Type:', type(x))

output:
Please enter some text:

anil
Text entered: anil
Type: <class 'str'>



type is used for find the catagory of the given input

Importing functions from a module in python

Importing functions from a module
Three ways to import functions:
1.
the simplest way: import everything from a module
advantage: simple usage e.g. of math functions
disadvantage: risk of naming conflicts when a variable has the same name as a module function
from numpy import *
print sin(pi/4)
# With this import method the following would give an error:
#sin = 5 # naming conflict!
#print sin(pi/4)
2.
import module under an alias name that is short enough to enhance code clarity
advantage: it is clear to see which function belongs to which module
import numpy as np
print np.sin(np.pi/4)
3.
import only the functions that are needed
advantage: simple usage e.g. of math functions
naming conflict possible, but less probable than with 1.
disadvantage: you must keep track of all the used functions and adapt the import statement if a
new function is used
from numpy import linspace, sin, exp, pi
print sin(pi/4)

using matplot lib


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM

*****************************************************"""
from numpy import linspace, sin, exp, pi
import matplotlib.pyplot as mp
# calculate 500 values for x and y without a for loop
x = linspace(0, 10*pi, 500)
y = sin(x) * exp(-x/10)
# make diagram
mp.plot(x,y)
mp.show()


output:


numpy usage in python


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM

*****************************************************"""
import numpy as np
# calculate 100 values for x and y without a for loop
x = np.linspace(0, 2* np.pi, 100)
y = np.sin(x)
print(x)
print(y)

output:

[0.         0.06346652 0.12693304 0.19039955 0.25386607 0.31733259
 0.38079911 0.44426563 0.50773215 0.57119866 0.63466518 0.6981317
 0.76159822 0.82506474 0.88853126 0.95199777 1.01546429 1.07893081
 1.14239733 1.20586385 1.26933037 1.33279688 1.3962634  1.45972992
 1.52319644 1.58666296 1.65012947 1.71359599 1.77706251 1.84052903
 1.90399555 1.96746207 2.03092858 2.0943951  2.15786162 2.22132814
 2.28479466 2.34826118 2.41172769 2.47519421 2.53866073 2.60212725
 2.66559377 2.72906028 2.7925268  2.85599332 2.91945984 2.98292636
 3.04639288 3.10985939 3.17332591 3.23679243 3.30025895 3.36372547
 3.42719199 3.4906585  3.55412502 3.61759154 3.68105806 3.74452458
 3.8079911  3.87145761 3.93492413 3.99839065 4.06185717 4.12532369
 4.1887902  4.25225672 4.31572324 4.37918976 4.44265628 4.5061228
 4.56958931 4.63305583 4.69652235 4.75998887 4.82345539 4.88692191
 4.95038842 5.01385494 5.07732146 5.14078798 5.2042545  5.26772102
 5.33118753 5.39465405 5.45812057 5.52158709 5.58505361 5.64852012
 5.71198664 5.77545316 5.83891968 5.9023862  5.96585272 6.02931923
 6.09278575 6.15625227 6.21971879 6.28318531]
[ 0.00000000e+00  6.34239197e-02  1.26592454e-01  1.89251244e-01
  2.51147987e-01  3.12033446e-01  3.71662456e-01  4.29794912e-01
  4.86196736e-01  5.40640817e-01  5.92907929e-01  6.42787610e-01
  6.90079011e-01  7.34591709e-01  7.76146464e-01  8.14575952e-01
  8.49725430e-01  8.81453363e-01  9.09631995e-01  9.34147860e-01
  9.54902241e-01  9.71811568e-01  9.84807753e-01  9.93838464e-01
  9.98867339e-01  9.99874128e-01  9.96854776e-01  9.89821442e-01
  9.78802446e-01  9.63842159e-01  9.45000819e-01  9.22354294e-01
  8.95993774e-01  8.66025404e-01  8.32569855e-01  7.95761841e-01
  7.55749574e-01  7.12694171e-01  6.66769001e-01  6.18158986e-01
  5.67059864e-01  5.13677392e-01  4.58226522e-01  4.00930535e-01
  3.42020143e-01  2.81732557e-01  2.20310533e-01  1.58001396e-01
  9.50560433e-02  3.17279335e-02 -3.17279335e-02 -9.50560433e-02
 -1.58001396e-01 -2.20310533e-01 -2.81732557e-01 -3.42020143e-01
 -4.00930535e-01 -4.58226522e-01 -5.13677392e-01 -5.67059864e-01
 -6.18158986e-01 -6.66769001e-01 -7.12694171e-01 -7.55749574e-01
 -7.95761841e-01 -8.32569855e-01 -8.66025404e-01 -8.95993774e-01
 -9.22354294e-01 -9.45000819e-01 -9.63842159e-01 -9.78802446e-01
 -9.89821442e-01 -9.96854776e-01 -9.99874128e-01 -9.98867339e-01
 -9.93838464e-01 -9.84807753e-01 -9.71811568e-01 -9.54902241e-01
 -9.34147860e-01 -9.09631995e-01 -8.81453363e-01 -8.49725430e-01
 -8.14575952e-01 -7.76146464e-01 -7.34591709e-01 -6.90079011e-01
 -6.42787610e-01 -5.92907929e-01 -5.40640817e-01 -4.86196736e-01
 -4.29794912e-01 -3.71662456e-01 -3.12033446e-01 -2.51147987e-01
 -1.89251244e-01 -1.26592454e-01 -6.34239197e-02 -2.44929360e-16]

functions2 in puthon


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM

*****************************************************"""
# function definition
def greeting():
    print("HELLO")
# main program using defined functions
greeting()

output:
HELLO

functions in python

""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM

*****************************************************"""
# function definitions
# calculate area of a rectangle
def area(b, h):
    A = b * h
    return A

#calulates perimeter of a rectangle
def perimeter(b, h):

    P = 2 * (b+h)
    return P

# main program using defined functions
width = 5
height = 3
print ("Area = ", area(width, height))
print ("Perimeter = ", perimeter(width, height))


output:
Area =  15
Perimeter =  16




example 2:


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM
*****************************************************"""
# function definitions
# calculate area of a rectangle
#calulates perimeter of a rectangle
def area_perimeter(b, h):
    A = b * h
    P = 2 * (b+h)
    return A,P

# main program using defined functions
width = 5
height = 3
ar,pr=area_perimeter(width,height)
print("area=",ar)
print("perimeter=",pr)

output:
area= 15
perimeter= 16

iterating with index using python

""" Dispay resistor colour code values"""
colours = [ "black", "brown", "red", "orange", "yellow",
"green", "blue", "violet", "grey","white" ]
cv = list (enumerate (colours))
for c in cv:
    print(c[0], "\t", c[1])


output:
0        black
1        brown
2        red
3        orange
4        yellow
5        green
6        blue
7        violet
8        grey
9        white

iterating through the list

mynames = [ "Sam", "Pit", "Misch" ]
for n in mynames:
    print ("HELLO ", n)


output:

HELLO  Sam
HELLO  Pit
HELLO  Misch


Example 2:

from math import *
for i in range (0, 5):
    print(i,"\t", sqrt(i))


output:

for floating point numbers use linspace and logspace from numpy! in python

""" for floating point numbers use linspace and logspace from numpy!"""
import numpy as np
r5 = np.linspace(0,2,9)
print(r5)

output:
[0.   0.25 0.5  0.75 1.   1.25 1.5  1.75 2.  ]

The syntax for linspace is
linspace ( <startvalue>, <stopvalue>, <number_of_values> )

Calculate quare root of numbers 0 to 100 in python

""" Calculate quare root of numbers 0 to 100"""
from math import *
i = 0
while i<= 100:
    print (i, "\t\t" , sqrt(i))
    i = i + 1
print("READY!")

output:
0                0.0
1                1.0
2                1.4142135623730951
3                1.7320508075688772
4                2.0
5                2.23606797749979
6                2.449489742783178
7                2.6457513110645907
8                2.8284271247461903
9                3.0

10               3.1622776601683795

.................................
.....................................
95               9.746794344808963
96               9.797958971132712
97               9.848857801796104
98               9.899494936611665
99               9.9498743710662

100              10.0

python class topic video