Python Interview Questions
Python Interview Questions
== 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
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
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
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.
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