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 46 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 46 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.

How to Install Visual Studio Code on Windows machine

Are you looking for a code editor that supports multiple languages and plugins? If yes, you have come to the right place to learn about one of the world’s most widely used code editors Visual Studio Code.

In this tutorial, you will learn what visual studio code is and how to install Visual studio code step by step from scratch. Still interested?

Let’s get into it.

Join 46 other followers

Table of Content

  1. What is Visual studio code?
  2. Benefits of Visual Studio Code
  3. Prerequisites
  4. Install and setup Visual studio code
  5. Setup Visual studio code with extension’s and plugins
  6. How to run python program in visual studio code
  7. Conclusion

What is Visual Studio Code?

Visual Studio Code is a light weighted code editor which can be installed on multiple operating systems such as windows operating system, Linux, and macOS. The beauty of this code editor is it can work with various languages together and supports multiple languages. Also, it has strong plugin installation capabilities.

Benefits of Visual Studio Code

Let’s look at some of the benefits of Visual Studio code in this section.

  • Visual Studio code automatically complete the code variables, methods and module because of its intelligent nature.
  • Visual Studio code comes with variety of in built themes which can be used while running the code.
  • Visual Studio code works with top extensions in industry which provides ease to use code inside the editor.
Visual Studio Plugins and extensions
Visual Studio Plugins and extensions

Prerequisites

  • Windows 7 plus machine is reqyuired.
  • .NET Framework 4.5.2 or higher is required. If you dont have .NET Framework 4.5.2 or higher , please install from here

Install Visual studio code

Now that you have a basic idea about Visual studio and why you need this. Let’s get into the installation part.

  • First open your favorite browser may be google chrome and click on the link here to install the Visual studio code zip file.
Downloading Visual studio code
Downloading Visual studio code
  • Once the zip file is downloaded move to any folder such as C:\Desktop\Softwares.
Viewing the Visual studio installer downloaded.
Viewing the Visual studio installer downloaded.
  • After you click on the windows zip installer, it will extract all the files in a folder as shown below. Finally click on Code to open Visual Studio Code.
Extracting the Visual studio installer
Extracting the Visual Studio installer
  • Now the Visual studio code main screen should come up and confirms successful installation of Visual studio code.
Opening the visual Studio code
Opening the Visual Studio code

Setup Visual studio code with extension and plugins.

Now that you have Visual studio code installed, which is great, you require plugins and extensions to work with various languages and tools. With more extensions, you make the editor better and more useful. Let’s install the Python extension in Visual studio code to kick off this section.

  • Now, execute the python command on command prompt to verify if python is successfully installed on your machine.
Executing the Python command on windows machine
Executing the Python command on the windows machine
  • Next, hop over to the Visual studio code , click on extension’s and search for Python. You should see a Python extension.
Checking Python Plugin
Checking Python Plugin
  • Further click on Install, to install the Python extension.
Installing Python Plugin
Installing Python Plugin
  • It takes few seconds to install the Python extension and once installed , it will reflect in installed section.
Installed Python Plugin
Installed Python Plugin
  • Also once python extension is installed you will notice at the bottom status bar will show you Python interpreter details.
Viewing the Python Interpreter.
Viewing the Python Interpreter.

How to run python program in visual studio code

Earlier, you installed the Python plugin in the Visual studio code, which allows you to write Python code easily and effectively. Let’s create a Python code and run a simple Python program in visual studio code.

  • Create a folder on desktop named python and create a file inside it named main.py and copy/paste the below code.
print("I just installed visual studio and its working fine")
print("Thanks automateinfra.com for taking through this tutorial")
Python folder creation containing main.py file
Python folder creation containing main.py file
  • Now switch to Visual studio code again and click on the File ➔ Add folder to workspace and select python folder that you created previously.
Adding the Python code in main.py file
Adding the Python code in the main.py file
  • Finally execute the Python code and as you can see the code has successfully executed.
The Python code in the main.py file

Conclusion

In this tutorial, you learned what visual studio code is and how to install visual studio code. You also learned how to install python extension and finally executed a successful python code.

This tutorial should give you a quick start to code editor visual studio code, so which applications do you plan to create in the visual studio next?

How to Setup Python on Windows machine

You might be already aware of difference between low level and high level language. Buts lets quickly see what are main difference between the both. High level language such as c , c ++ and python are portable , human friendly, that is they are easier to debug and understand while writing the programs. They need either compiler or interpreter to run the programs.

On the other hand, Low level language such as machine language or assembly level language they are difficult to understand and are written in assembly language which are converted using assembler.

In case of Linux distributions Python programming language by default , but in windows it does not come with it. In this tutorial, we will learn what is python which is widely used in artificial intelligence, by astronauts, by developers for creating the apps. Learning and setting python on windows is a need of the hour. So lets get started with it.

Table of content

  1. What is Python?
  2. Prerequisites
  3. How to install Python on windows machine?
  4. A simple python code to start your day
  5. Conclusion

What is Python?

Python is a high level , oops based , interactive and a general purpose scripting programing language. Python is very easier to understand and readable. It focuses on object over functions. Python is also a interpreted language because it converts codes in machine level code even before it runs. It works on variety of protocols such as https, ftp , smtp and many more. The latest version is 3.9.2 which was released on December 2020. Python works very well with most of the such as atom, notepad ++ , vim.

Prerequisites

  • Windows 7 + operating system with admin rights
  • Command Prompt which is already present in windows operating system

How to Install Python on windows machine?

  • Now run the executable installer which got downloaded.
  • Next step will prompt you to select whether to Disable path length limit , you ignore it or disable it.
  • Now python should be installed on your windows machine.

 Verify Python Was Installed On Windows

  • Now Navigate to command prompt and type python
  • Pip is basically python package manager which is used to fetch lots of other additional modules. Pip comes with most of pythons executable. To check if it is installed use
pip -V
  • To verify if python environmental variables was successfully added. Search for sysdm.cpl and click on edit environmental variables.

A simple python code to start your day

  • Create a simple file main.py on your desktop
mystring = " This is Author of Automateinfra and its time to start learning python"
mylist   = [letter for letter in mystring] # This is function which iterates over each alphabet in mystring
print(mylist)       # This will print the result of mylist
  • Navigate to your desktop location on command prompt and use below command to run your first program in python
python main.py
  • Great work ! python was installed successfully and we could run our first program of python quickly.

Conclusion

In this tutorial we covered basic difference between high level language and low level language. We also discussed what is python and how to install python on windows machine to get you started in organization scripting and automation work.

Hope this tutorial will helps you in understanding and setting up Python on Windows machine.

Please share with your friends.