The Ultimate Python interview questions: learn python the hard way

If you are preparing for a DevOps interview or for a Python developer, consider this guide as your friend to practice Python interview questions: learn python the hard way and answers and help you pass the exam or interview.

Without further delay, let’s get into this Ultimate Python interview questions and answers guide, where you will have 20 questions to practice.

Let’s get into it.

Join 28 other followers

PAPER

Q1. What is difference between List and Tuple in Python?

Answer: Lists are mutable i.e they can be modified and Tuples are immutable. As you can see in the below code the list can be modified however Tuples are not modifable.

List = ["a","i",20]
Tuples = ("a","i",20)
List = ["a","i",20]
Tuples = ("a","i",20)
print(List)
print(Tuples)
List[1] = 22
print(List)
Tuples[1] = "n"
print(Tuples)

After running the above code, you will notice that the Lists can modify, but the Tuples are not.

Running Python code to modify list and tuple
Running Python code to modify list and tuple

Q2. What are key features of Python.

Answer: Python is interpreted language, that means it doesn’t required to be compiled. It can be used in various automation areas such as AI technologies, easier to understand and write language. The Latest version of Python is 3.10

Q3. Give Examples of some famous Python modules?

Answer: Some of the example of Python modules are os, json, sys, math, random, math

Q4. What are local and global variables in Python ?

Answer: Local Variables are declared inside the function and scope is inside function only however global variables are those variables whose scope is for entire program.

Q5. What are functions in Python ?

Answer: This is a block of code which is only executed when called. To declare the function you need use the below syntax def < function-name> and call it using <function-name> as shown below.

def function(n): 
     a, b = 0, 1
     while a > n:
         print(a)
         print(b)
     print(n)
function(200)     
python shanky.py
Running Python function
Running Python function

Q6. What is __init__ ?

Answer: This is a method which automatically allocate memory when object or instance is created. All the Classes have __init__method.

Q7. What is the Lambda function in Python?

Answer: Lambda functions are also known as the anonymous function, which takes any number of parameters but is written in a single statement.

square = lambda num: num ** 2

Q8. What is self in Python?

Answer: self is an instance or object of a class. This is used with __init__ function and is explicitly included as first parameter.

Q9. How can you Randomize the list in Python ?

Answer: To randamize the list in Python consider importaning random module as showb below.

from random import shuffle
list = ["a" , "b" , "c" ,"d", "e"] 
shuffle(list)
print(list)

Q10.What does *args and *Kargs mean ?

Answer: The *args are used when you are sure about the number of arguments that you will pass in a function. Similarly * Kargs are keyword arguments where you are not sure about number of argument but they are declared in the dictionary formats.

Q11. Does Python Supports OOPs concept?

Answer: Yes , Python does support OOPs concept by creating classes and objects . Backends determines where state is stored or loaded from. By default we have local backend but we can give remote backed such as S3

Q12. Name some Python Libraries ?

Answer: The Python libraries are collection of built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming. For example Pandas, Numpy etc.

Q13. What are various ways to import the modules in python?

Answer:

import math 

import math as mathes ( Alias name ) 
from flask import Flask

Q14. What is Python Flask?

Answer: Python flask is a web framework which makes life of developer easy by reusing the code, extensions for operation to build a reliable, scalable and maintainable web apps. With Python flask web frameworks, you can create and build from static to dynamic applications and work with API requests.

There are different Python Web frameworks apart from Python flask such as TORNADO, PYRAMID and DJANGO .

Related POST: Python Flask Tutorial: All about Python flask

Q15. How can we open a file in Python?

Answer:

with open("myfile.txt", "r") as newfile:

Q16. How can you see statistics of file located in directory?

Answer: To see the stats of file located in directory consider using os module.

import os

os.stat("file_name") # These stats include st_mode, the file type and permissions, and st_atime, the time the item was last accessed.

Q17. How do you define method and URL Bindings in Python Flask?

Answer:

@app.route("/login", methods = ["POST"])

Related POST: Python Flask Tutorial: All about Python flask

Q18. How does Python Flask gets executed in Python ?

Answer:

if __name__ ==  '__main__'
app.run(debug = True)

Q19. Which built in function evaluates expression and return Boolean result ?

Answer: can function

Q20. How can you make Python Script executable in Unix ?

Answer: Below are the steps that you would need to make Python Script executable in unix.

  • Define the Path of Python interpretor
#/usr/local/bin/python
  • Next convert the script into executable by using below command.
chmod +x abc.py
  • Finally run the script
python abc.py

Join 28 other followers

Conclusion

In this ultimate guide, you had a chance to revise everything you needed to pass the interview with Python interview questions and answers.

Now that you have sound knowledge of Python and various components, modules, and features, and are ready for your upcoming interview.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s