0% found this document useful (0 votes)
11 views3 pages

Python Interview Questions

Uploaded by

Shopping Venk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Python Interview Questions

Uploaded by

Shopping Venk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. Explain the difference between == and is operators in Python.

== is equality operator and is used to compare if the value of two variables is same or not.
Is is a membership operator and is used to check if two variables points to memory address

2. Explain the difference between mutable and immutable objects in Python.


Mutable objects are (Editable) those whose values can be changed without creating a new
copy of the object ex : Lists, Dictonary, Sets

Immutable objects are (Non-Editable) those whose value cannot be changed once they are
created, even if the value is changed, it will create a new object to save the data. Ex: Tuple,
frozenSet, Bool, Int, String

3. How can you check the type of a variable in Python?


Use type(variableName)
Ex: a =[1,2,3]
type(a)

4. Explain the concept of variable scope in Python.


There are 2 variable scope in Pyton
1. Global scope
a. Variables that are defined outside the functions/ indentations are called global
variables. Any value changed to global variable also will reflect in local variable.
2. Local scope
a. Variables that are defined inside the indentations/loops/functions are called
local variables. Any value changed in local scope will not reflect at global variable
b.
5. Find the Maximum of Three Numbers?

list = [5,20,15]
Solution 1:
j =0
for i in list:
if(i > j):
j=i
print (j)
Output : 20

Solution 2:
max(list)
Output : 20

6. How can you add a new key-value pair to an existing dictionary in Python?

dict1={"name":'venkat',"age":26}
dict1["Gender"] ="Male"
dict1
Output : {'name': 'venkat', 'age': 26, 'Gender': 'Male'}
7. How do you reverse a string in Python?
str= "My Name is Raviteja"
Solution 1:
revStr = ''
for i in str:
revStr = i + revStr
revStr
Solution 2:
revStr = str[::-1]
revStr

8. How can you remove duplicates from a list in Python?


list = [ 12, 16, 33, 24, 53, 48, 33 ]
Solution 1:
list2 = []
for i in list:
if i not in list2:
list2.append(i)
list2
Output: [12, 16, 33, 24, 53, 48]
Solution 2:
list3 = dict.fromkeys(mylist)
list(list3)
Output: [12, 16, 33, 24, 53, 48]

9. Explain the difference between a "for" loop and a "while" loop in Python. When would you
use each?

For loop will be used on any iteratible object whose length is based on the values in the
object and the length of the object will be the maximum iterations for the loop.

While loop will be used when the iterations are not known in advance or till a certain
condition is satisfied. While loop if used incorrectly will run into infinite loop.

10. How do you comment out a line of code in Python?


Use a # to comment the line.

11. Difference between *args and **kwargs?


*args will be used when there are multiple parameters to be passed as parameters to a
function.

**kwargs will be used to pass dictionary object as parameters to a function

12. How can you add a new key-value pair to an existing dictionary?
dict1={"name":harish',"age":26}
dict1["Gender"] ="Male"
print (dict1)
13. Write a program to find whether the given string is palindrome or not?
str= input("Ënter a String:")
if(str == str[::-1]):
print("This is a Palindrome String")
else:
print("This is a Not a Palindrome")

Output:
1. Ënter a String:Random
This is a Not a Palindrome
2. Ënter a String:RADAR
This is a Palindrome String

You might also like