Python Interview Questions for 2022

 



Q1) What is the difference between list and tuples in Python?

List Tuples
Lists are mutable i.e 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 = [18, ‘FIFA’, 20] Syntax: tup_1 = (18, ‘FIFA’ , 20)


Q2)What is an Interpreted language?

An Interpreted language executes its statements line by line. Languages such as Python, JavaScript, R, PHP, and Ruby are prime examples of Interpreted languages. Programs written in an interpreted language runs directly from the source code, with no intermediary compilation step.

Q3)What are Python namespaces?

A namespace in python refers to the name which is assigned to each object in python. The objects are variables and functions. As each object is created, its name along with space(the address of the outer function in which the object is), gets created. The namespaces are maintained in python like a dictionary where the key is the namespace and value is the address of the object. There 4 types of namespace in python-

  • Built-in namespace– These namespaces contain all the built in objects in python and are available whenever python is running.
  • Global namespace– These are namespaces for all the objects created at the level of the main program.
  • Enclosing namespaces– These namespaces are at the higher level or outer function.
  • Local namespaces– These namespaces are at the local or inner function.


Q4)What is Scope in Python?

Every object in Python functions within a scope. A scope is a block of code where an object in Python remains relevant. Namespaces uniquely identify all the objects inside a program. However, these namespaces also have a scope defined for them where you could use their objects without any prefix. A few examples of scope created during code execution in Python are as follows:

  • A local scope refers to the local objects available in the current function.
  • A global scope refers to the objects available throughout the code execution since their inception.
  • A module-level scope refers to the global objects of the current module accessible in the program.
  • An outermost scope refers to all the built-in names callable in the program. The objects in this scope are searched last to find the name referenced.


Q5)What are Keywords in Python?

Keywords in Python are reserved words which are used as identifiers, function name or variable name. They help define the structure and syntax of the language. There are a total of 35 keywords in Python. A list of all the keywords is provided below:

False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield

Q6)What are Literals in Python and explain about different Literals?

Literals in Python refer to the data that is given in a variable or constant. Python has various kinds of literals including:

  • String Literals: It is a sequence of characters enclosed in codes. There can be single, double and triple strings based on the number of quotes used. Character literals are single characters surrounded by single or double-quotes.
  • Numeric Literals: These are unchangeable kind and belong to three different types – integer, float and complex.
  • Boolean Literals: They can have either of the two values- True or False which represents ‘1’ and ‘0’ respectively.
  • Special Literals: Special literals are sued to classify fields that are not created. It is represented by the value ‘none’.

Q7)What are functions in Python?

Functions in Python refer to blocks that have organised, and reusable codes to perform single, and related events. Functions are important to create better modularity for applications which reuse high degree of coding. Python has a number of built-in functions like print(). However, it also allows you to create user-defined functions.

There are three types of functions:

  • Built-In Functions: copy(), len(), count() are the some built-in functions.
  • User-defined Functions: Functions which are defined by a user known as user-defined functions.
  • Anonymous functions: These functions are also known as lambda functions because they are not declared with the standard def keyword.

Q8)How can you initialize a 5*5 numpy array with only zeroes?

We will be using the .zeros() method

import numpy as np
n1=np.zeros((5,5))
n1

Use np.zeros() and pass in the dimensions inside it. Since, we want a 5*5 matrix, we will pass (5,5) inside the .zeros() method.

Output:

Q9)What is the difference between remove() function and del statement?

The user can use the remove() function to delete a specific object in the list.


Example:
list_1 = [ 3, 5, 7, 3, 9, 3 ]
print(list_1)
list_1.remove(3)
print("After removal: ", list_1)
Output:
If you want to delete an object at a specific location (index) in the list, you can either use del or pop.
Example:
list_1 = [ 3, 5, 7, 3, 9, 3 ]
print(list_1)
del list_1[2]
print("After deleting: ", list_1)
Output:

Q10)What is swapcase() function in the Python?

It is a string's function which converts all uppercase characters into lowercase and vice versa. It is used to alter the existing case of the string. This method creates a copy of the string which contains all the characters in the swap case. If the string is in lowercase, it generates a small case string and vice versa. It automatically ignores all the non-alphabetic characters. See an example below.


Example:
string = "LOWERCASE."
print(string.swapcase())
string = "uppercase."
print(string.swapcase())
Output:



More Questions Will be Added Soon.
AJ Blogs

Hello everyone, My name Arth and I like to write about what I learn. Follow My Website - https://sites.google.com/view/aj-blogs/home

Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page