Python interview questions

Hello Friends,

Today in this article we are going to discuss about some basic questions about python so that it will help us in python so that it will help you in python.

  1. What is difference between
   print(mydict.get())
   print(mydict[])

2. Can we add list in the set?

3. What will the Output of –

 s = set()
 s.add(20)
 s.add(20.0)
 s.add("20")

4. What will be the solution of this error

pip is not recognized 

5. Write a code in terminal to get the current version of python you are using?

6. Write a program to print excel sheet in Python?

7. Create a blank GUI in Python?

8. Write a program to print multiple lines in Python?

9. Write a program to print the factorial number?

10. What is the difference between list and tuples in Python?

Solutions

Q1 solution

When we use dict([]) function and we enter a word which is not present in dictionary it gives the error

Fig-1 How  dict([]) gives an error when we enter a word which is not present in the dictionary
Fig-1 How dict([]) gives an error when we enter a word which is not present in the dictionary

When we use dict.get() and enter a word which is not present it do not gives the error

 When we use dict.get() and enter a word which is not present it do not gives the error
Fig-2 When we use dict.get() and enter a word which is not present it do not gives the error

Q2 solution

No we cannot add a list into the set. A set is an unordered and immutable collection of values or objects, and a list is an ordered and mutable collection of objects. 

Q3 solution

The output will be –

Fig-3 Output of the question 3
Fig-3 Output of the question 3

It will return only two values because the value of 20.0 and 20 is same

Q4 solution

  1. Press Windows key + R to open up a Run dialog box. Then, type “sysdm.cpl” and press Enter to open up the System Properties screen.
Fig-4 Environment variables
Fig-4 Environment variables

2. In the Environment Variables screen, go to System variables and click on Path to select it. Then with the Path selected, click the Edit… button.

Fig-5 System variables

In the Edit environment variable screen, click on New and add the path where the PiP installation is located. For Python 3.10, the default location is C:\Python34\Scripts. And then click on ok

Fig-6 Editing environment variable
Fig-6 Editing environment variable

Now, finally you have added the pip to check we can open our terminal and type pip. The result will be –

Fig-7 pip
Fig-7 pip

You can also install any package from pip. If you wanna learn more about pip click here

Q5 solution

python --version

Fig-8 Code to check the python version

Q6 solution

To get the solution check this link’s – part 1 – https://knowledge-junction.com/2021/08/22/python-how-to-read-the-complete-excel-file-using-xlrd/

part 2 – https://knowledge-junction.com/2021/08/22/python-how-to-read-the-complete-excel-file-using-xlrd/

Q7 solution

Code-

from tkinter import*
start=Tk()
start.mainloop()

Output

Fig-9 Blank GUI in Python

Q8 solution

print('''Today in this article we are going to
discuss about some basic questions about python
so that it will help us in python.''')

Here we have use triple quotes

Output

Fig-10 Output of the Q8
Fig-10 Output of the Q8

Q9 solution

num = int(input("Enter a number: "))
factorial = 1
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)

Q10 solution

Difference between List and Tuple

LISTTUPLES
Lists are mutable (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, ‘Knowledge’, 20]Syntax: tuple_1 = (10, ‘Knowledge’, 20)
Table-1 Difference between List and Tuple

Thanks for reading 📖

Have a nice day 🙂

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: