Python Interview Questions

This article lists top top Python interview questions and answers.

Question 1: What is Python?  

  • Python is a general purpose, high level programming language.
  • Python is a dynamically typed programming language and it is also an Object Oriented programming language.
  • Python can be used for console, GUI, and CGI (Web development) programming.
  • Python is also used for programming Arduino (IOT).
  • Python is one of the most popular programming language for machine learning.

Question 2: What are the features of Python programming?

  • Dynamic data type.
  • Python supports structural and OOPS method.
  • Python has an automatic garbage collection.
  • Python can be used for programming Arduino (IOT).

Question 3: Is Python a case sensitive programming language?

 
Yes! Python is case sensitive language.
 
Example
  1. Str1="c#"   
  2. str1="corner"  
  3. print(Str1)  
  4. print(str1)   
Output

 
 

Question 4: What are the data types available in Python?

 
Python generally supports five data types, namely:
  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Question 5: What is the output of print(str) if str="C#Corner"?

 
String is exactly print using the print() function and output is C#Corner:
  1. str="C#Corner"  
  2. print(str)  
 
 

Question 6: What is the output of print str[0] if str = 'C#Corner'?


Only the first letter of string is printed \
  1. str="C#Corner"  
  2. print(str[0])  
 
  

Question 7: What is the output of print(str[2:5]) if str = 'C#Corner'? 

 
In this problem, we used the slicing operator in print() function.
 
 
Example:
  1. str="C#Corner"  
  2. print(str[2:5])  
Output:

 
 

Question 8: What is the output of print(str[2:]) if str="C#Corner" ?

 
in this problem, string offset starts from index 2 and executes until the end of the string.
 
Example: 
  1. str="C#Corner"  
  2. print(str[2:])  
Output:

 
 

Question 9: What is the output of print(str*2) if str="C#Corner" ?

 
In this problem, we used the multiplication operator with the string, which means the string is multiplied by 2 or print the string two times.
 
Example:
  1. str="C#Corner"  
  2. print(str*2)  
Output:

 

Question 10: What is the output of print(str+" community") if str="C#Corner" ? 


In this problem, we used concatenation operator(+), which appends a community at the end of the string.
 
Example:
  1. str="C#Corner"  
  2. print(str+" community")  
Output: 

 
 

Question 11: What is the output of print(list) if list=["python","c#",".net"] ?

 
It prints whole list. 
 
Example:
  1. list=["python","c#",".net"]  
  2. print(list)  
Output:

 
 

Question 12: What is the output of print(list[0]) if list=["python","c#",".net"] ?


In this problem, it prints only the first element of the list.
 
Example:
  1. list=["python","c#",".net"]  
  2. print(list[0])  
Output:

 
 

Question 13: What is the output of print(list[1:3]) if list=["python","c#",".net"] ? 

 
This problem uses a slicing operator, where a starting index is 1 and ending index is 2.
 
Example: 
  1. list=["python","c#",".net"]  
  2. print(list[1:3])  
Output:

 

Question 14: What is the output of print(list[0:]) if list=["python","c#",".net"] ?

 
In this problem, offset starts from 0 and ends with the length of the list.
 
Example:
  1. list=["python","c#",".net"]  
  2. print(list[0:])  
Output:

 
 

Question 15: What is the output of print(list*2) if list=["python","c#",".net"] ?

 
In this problem, we used * operator and list is repeated two times.
 
Example:
  1. list=["python","c#",".net"]  
  2. print(list*2)  
Output:

 
 

Question 16: What is the output of print(list+tlist) if list=["python","c#",".net"]  & tlist=["c++","php"] ?

 
In this problem, concatenation operator is used with the list and tlist operator combines both the lists.
  1. list=["python","c#",".net"]  
  2. tlist=["C++","php"]  
  3. print(list+tlist)  
Output:

 
 

Question 17: What is tuple in Python? 

  • Tuple is sequential type data type and it is used to store a value in a sequence.
  • Tuple is just like a list in Python but a tuple is little bit different from a list. 
  • Tuple values can not be updated after initialization
Example: 
  1. tuple=("ajay","python",22)  
  2. print(tuple)  
Output:

 
 

Question 18: What is the difference between tuples and lists in Python? 

 
They are listed below,
Operation tuple list
updating    tuple can't be updated after declaration list can be updated again and again
declaration tuple is declared using () brackets list is declared using [] brackets
size size of tuple can't be changed size of list can be changed

we can say tuple is a read only list. 
 

Question 19: What is the output of print(tuple) if tuple = ("c# corner",".net","python","asp","mvc")?

 
It prints all the elements of a tuple
 
Example 
  1. tuple=("c# corner",".net","python","asp","mvc")  
  2. print(tuple)   
Output
 
 
 

Question 20: What is the output of print(tuple[0]) if tuple = ("c# corner",".net","python","asp","mvc")?

 
Only first element of a tuple is printed, because the index is passed in the print function.
 
Example:
  1. tuple=("c# corner",".net","python","asp","mvc")  
  2. print(tuple[0])  
Output:
 
 
 

Question 21: What is the output of print(tuple[1:3]) if tuple = ("c# corner",".net","python","asp","mvc")?

 
In this problem, only 2nd and 3rd element is printed. 
 
Example 
  1. tuple=("c# corner",".net","python","asp","mvc")  
  2. print(tuple[1:3])  
Output
 
 
 

Question 22: What is the output of print(tuple[2:]) if tuple = ("c# corner",".net","python","asp","mvc")? 

 
It helps to print the second  element to the last element.
 
Example
  1. tuple=("c# corner",".net","python","asp","mvc")  
  2. print(tuple[2:])  
Output
 
 
 

Question 23: What is the output of print(tuple*2) if tuple = ("c# corner",".net","python","asp","mvc")?

 
In this problem, a tuple prints two times because the tuple is multiplied by 2.
 
Example:
  1. tuple=("c# corner",".net","python","asp","mvc")  
  2. print(tuple*2)  
Output
 
 

Question 24: What is the output of print(tuple+tuple1) if tuple = ("c# corner",".net","python","asp","mvc") and tuple1=("c","c++")?

 
It helps in concatenation of both the tuples.
 
Example
  1. tuple=("c# corner",".net","python","asp","mvc")  
  2. tuple1=("c","c++")  
  3. print(tuple+tuple1)  
Output

 
 

Question 25: What are Python's dictionaries and how will you create a dictionary in Python?


Dictionaries is one of the datatypes in Python. Dictionaries are having the the key and value pair. These are defined, using {} brackets.
 
Example: 
  1. dict={"name":"ajay","sex":"male"}  
  2. print("Name is:",dict["name"])  
  3. print("sex is:",dict["sex"])  
Output:

 
 

Question 26: How will you get all the keys from the dictionary? 

 
In Python programming, we will get all the keys from the dictionary, using keys() method.
 
Example:
  1. dict={"name":"ajay","sex":"male"}  
  2. print("all keys are:",dict.keys())  
Output

 
 

Question 27: How will you get all the values from the dictionary?

 
In Python programming, we will get all the values from the dictionary, using values() method.
 
Example:
  1. dict={"name":"ajay","sex":"male"}  
  2. print("all values are:",dict.values())  
Output
 
 
 

Question 28: How will you convert a string to an int in Python?

 
In Python programming, we will convert a string into integer, using int() method.

Example
  1. str="9999999999"  
  2. number=int(str)  
  3. print("number is:",number)  
output

 

Question 29: How will you convert a string to a float in Python?

 
In Python programming, we will convert a string into a fractional, using float() method.
 
Example
  1. str="10"  
  2. number=float(str)  
  3. print("float number is:",number)  
Output

 
 

Question 30: How will you sort a list?

 
In Python programming, we will sort a list, using sort() method.
 
Example 
  1. list=[50,1,16,2,20]  
  2. print("before sorting",list)  
  3. list.sort()  
  4. print("after softing",list)  
Output

 
 

Question 31: How will you reverse a list?

 
In Python programming, we will reverse the list, using reverse() method.
 
Example:
  1. list=[50,1,16,2,20]  
  2. print("before reverse",list)  
  3. list.reverse()  
  4. print("after reverse",list)  
Output

 
 

Question 32: How will you remove an object from a list? 

 
In Python programming, we will remove an element from the list, using remove() method.
 
Example
  1. list=[50,1,16,2,20]  
  2. print("list before remove",list)  
  3. list.remove(20)  
  4. print("list after remove 20:",list)  
Output

 
 

Question 33: How will you remove the last object(element) from a list?

 
In Python programming, we will remove the last element from the list, using pop() method.
 
Example
  1. list=[50,1,16,2,20]  
  2. print("list before remove",list)  
  3. list.pop()  
  4. print("list after remove last element:",list)  
Output

 
 

Question 34: How will you insert an object at a given index in a list?

 
In Python programming, we will insert an element in a list at a given index, using an insert() method.
Example
  1. list=[50,1,16,2,20]  
  2. print("list before insert",list)  
  3. list.insert(0,100)  
  4. print("list after insert element at 0 index:",list)  
Output

 
 

Question 35: How will you get the index of an object in a list? 

 
In Python programming, we will get a given element in a list, using an index() method 
 
Example
  1. list=[50,1,16,2,20]  
  2. print("index of 16 is:",list.index(16))   
Output
 
 
 

Question 36: How will you get the min valued item of a list?

 
In Python programming, we will get the min valued item of a list, using min() method. 
 
Example
  1. list=[50,1,16,2,20]  
  2. print("minimum value element is:",min(list))  
Output

 
 

Question 37: How will you get the max valued item of a list? 

 
In Python programming, we will get the max valued item of a list, using max() method. 
 
Example
  1. list=[50,1,16,2,20]  
  2. print("maximum value element is:",max(list))  
Output

 
 

Question 38: How will you get the length of a list?

 
In Python programming, we will get the length of a list, using len() method.
 
Example
  1. list=[50,1,16,2,20]  
  2. print("length of a list is:",len(list))   
Output
 
 

Question 39: What is the output of list[1:] if list=[50,1,16,2,20]?

 
In this problem, list element is printed from the first index to the last index.
 
example
  1. list=[50,1,16,2,20]  
  2. print(list[1:])  
Output

 
 

Question 40: What is the output of print(list[-2]) if list=[50,1,16,2,20]? 

 
In this problem, it helps to print the second to the last element, because a negative index of the last element is -1
 
Example
  1. list=[50,1,16,2,20]  
  2. print(list[-2])  
Output

 

Question 41: What is the output of print(list[2]) if list=[50,1,16,2,20]? 

 
In this problem, it helps to print the third element because offset starts from zero(0).
 
Example
  1. list=[50,1,16,2,20]  
  2. print(list[2])  
Output

 
 

Question 42: What is the output of for item in list: print(item) if list=[50,1,16,2,20]?

 
For loop iterates all the elements from the list.
 
Example
  1. list=[50,1,16,2,20]  
  2. for item in list:  
  3.     print(item)  
Output

 
 

Question 43: What is the output of print(2 in list) if list=[50,1,16,2,20]?

 
In this problem "in operator" checks if the given element is present in the list and if it is present, it returns true otherwise false.
 
Example
  1. list=[50,1,16,2,20]  
  2. print(2 in list)  
Output

 
 

Question 44: What is the output of print(list*2) if list=[50,1,16,2,20]?

 
In this problem, list is printed twice.
 
Example
  1. list=[50,1,16,2,20]  
  2. print(list*2)  
Output
 
 
 

Question 45: What is the output of print(list+list1) if list=[50,1,16,2,20] and list1=[100,40]?

 
This helps to print concatenation of the list and list2.
 
Example
  1. list=[50,1,16,2,20]  
  2. list1=[100,40]  
  3. print(list+list1)  
Output

 
 

Question 46: What is the output of print(len([1, 2, 3]))?

 
This helps to print 3, because 3 element is passed into len() method.
 
Example
  1. print(len([1,2,3]))  
Output

 
 

Question 47: What is the difference between del() and remove() methods of the list? 

 
remove() method is used to delete a single element from the list.
 
del() method is used to delete the whole list. 
 

Question 48: How will you check in a string that all the characters are decimal? 

 
In Python programming, we will use isdecimal() method to check in a string if all the characters are decimal or not.
 
Example
  1. str="hello welcome in c# corner"  
  2. print(str.isdecimal())  
Output

 
 

Question 49: How will you convert a string to all uppercase?

 
In Python programming, we will use upper() method to convert lowercase string into uppercase.
 
Example
  1. str="csharp corner"  
  2. print("string is before used upper method:",str)  
  3. str1=str.upper()  
  4. print("string is after used upper method:",str1)  
Output

 
 

Question 50: How will you get titlecased version of a string?

 
In Python programming, we will use title() method to convert a string into a titlecase.
 
Example
  1. str="csharp corner"  
  2. print("string is",str)  
  3. str1=str.title()  
  4. print("titlecased string is:",str1)  
Output

 
 

Question 51: How will you change case for all the letters in a string?

 
In Python programming, we will use swapcase() method to change the case of the string.
 
Example
  1. str="CSHARP CORNER"  
  2. print("string is",str)  
  3. str1=str.swapcase()  
  4. print("swapcase is:",str1)  
Output

 
 

Question 52: How will you remove all the leading and trailing whitespace in a string?

 
In Python programming, we use strip() method to remove all the leading and trailing whitespace in a string.
 
Example
  1. str="            CSHARP CORNER    "  
  2. print("string is",str)  
  3. str1=str.strip(' ')  
  4. print("after remove whitespace is:",str1)  
Output

 
 

Question 53: How will you replace all the occurrences of the old substring in string with a new string? 

 
In Python programming, we use replace() method to replace all the occurrences of old substring in a string with a new string.
 
Example
  1. str="CSHARP CORNER    "  
  2. print("string is",str)  
  3. str1=str.replace("SHARP","#")  
  4. print(str1)  
Output

 
 

Question 54: How will you get the length of the string?

 
In Python programming language, we will use len() method to get the length of the string.
 
Example
  1. str="c# corner"  
  2. print("length of string is :",len(str))  
Output

 
 

Question 55: How will you check in a string that all characters are whitespaces? 

In Python programming language, we will used isspace() method to check in a string, if all characters are whitespaces or not.
 
Example
  1. str="      "  
  2. print(str.isspace())  
Output

 
 

Question 56: How will you check in a string that all the characters are numerics?


In Python programming language, we will use isnumeric() method to check in a string, if all the characters are numerics or not.
 
Example
  1. str="123"  
  2. print(str.isnumeric())  
 
 

Question 57: How will you check in a string that all the characters are in lowercase?


In Python programming language, we will use islower() method to check in a string if all characters are in lowercase or not.
 
Example
  1. str="hello python"  
  2. print(str.islower())  
Output

 
 

Question 58: How will you check in a string that all the characters are digits?


In Python programming language, we will use islower() method to check in a string if all characters are digits or not.
 
Example
  1. str="9999999999"  
  2. print(str.isdigit())  
 
 

Question 59: How will you check in a string that all characters are alphanumeric?


In Python programming language, we will use isalphanumeric() method to check in a string if all the characters are alphanumeric or not.
 
Example
  1. str="n123"  
  2. print(str.isalnum())  
Output
 
 

Question 60: How will you capitalize first letter of the string?


In Python programming language, we will use capitalize() method to capitalize the first letter of a string.
 
Example
  1. str="python"  
  2. print("string before capatilized",str)  
  3. print("After capitalize",str.capitalize())  
Output

 
 

Question 61: How will you convert a string to a tuple in Python?

 
In Python programming language, we will use tuple() method to convert a string to a tuple.
 
Example
  1. str="abc"  
  2. tup=tuple(str)  
  3. print("string is:",str)  
  4. print("tuple is:",tup)  
Output

 
 

Question 62: How will you convert a string to a list in Python?

 
In Python programming language, we use list() method to convert a string to a list.
 
Example
  1. str="abc"  
  2. li=list(str)  
  3. print("string is:",str)  
  4. print("list is:",li)  
Output

 
 

Question 63: How will you convert a string to a set in Python?

 
In Python programming language, we use set() method to convert a string to a set.
 
Example
  1. str="abc"  
  2. set=set(str)  
  3. print("string is:",str)  
  4. print("set is:",set)  
Output

 
 

Question 64: How will you create a dictionary, using tuples in Python?

 
In Python programming language, we use dict() method to create a dictionary, using tuples.
 
Example
  1. tuple=(("name","ajay"),("sex","male"))  
  2. dict=dict(tuple)  
  3. print("tuple:",tuple)  
  4. print("dictionary element is:")  
  5. print("name:",dict["name"])  
  6. print("sex",dict["sex"])  
Output

 
 

Question 65:What is the use of ** operator?


In Python programming language, ** operator is used to perform an exponential(power).
 
Example
  1. a=2  
  2. b=4  
  3. print("2 to power 4: ",a**b)  
Output:

 
 

Question 66:What is the use of // operator?

 
This operator returns the quotient, after the decimal point is removed.
 
Example
  1. a=5  
  2. b=2  
  3. print("a/b is:",a/b)  
  4. print("a//b is:",a//b)  
 
 

Question 67:What is the purpose of is operator?

 
Is operator is used to check if the given string is in another given string or not.
 
Example
  1. str="hello"  
  2. str1="hello python"  
  3. print(str in str1)  
Output:

 
 

Question 68:What is the purpose of not in operator?

 
This statement is used to check if the given variable is not in sequence.
 
Example
  1. str="c#"  
  2. str1="hello python"  
  3. print(str not in str1)  
Output

 
 

Question 69: What is the purpose of break statement in Python?

 
Break statement is used to terminate the current loop and resumes execution at the next statement.
 

Question 70: What is the purpose of continue statement in Python?

 
Continue statement is used to reject all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.
  

Question 71:What is the purpose of pass statement in Python?

 
This statement is used, when the statement is needed syntactically but otherwise there is no need of any statement.