Python Interview Question

Que 1 - What is Python?

Ans - Python is a high-level interpreted, interactive, and object-oriented scripting language. Python is designed to be highly readable (the syntax of python is very easy). 

Que 2 - What are the use cases of Python? 

Ans -

  • Web development.

  • Software development.

  • Scripting.

  • Machine Learning & Artificial Intelligence.

  • Gaming.


Que  - What are the advantages of using Python?

Ans -

  • It is highly readable

  • Python is platform-independent. This means you can run python scripts on all platforms like Linux, windows, and mac os.

  • A Huge community of developers.

  • Python is a dynamically typed language. You don't have to define datatype before writing a variable.

  • Python has huge support for databases. Python can connect with databases like MySQL, Postgres, SQL Server, MongoDB, etc.

  • It is highly Scalable. You can use python to create an application of 10 lines of code to millions of lines of code with the same ease.


Que 4 - What are the disadvantages of using Python?

Ans - Python is an interpreted programming language because of which applications created with python are usually slower than other compiler-based programming languages like java, c++, etc.

Que 5 - python is which type of language? Interpreted or compiled?
- Python is an interpreted programming language i.e, it is not in the machine-level code before runtime. Interpreted languages are slower than compiled languages.

Que 6 - Why are variables in python?
- A python variable is a way to store values in a defined memory location. Variables in Python scripts are used to provide values to the computer. Variables in Python are dynamically typed which means you don't have to define the data type of a variable in python.

Que 7 - python is which type of language? Statically typed or dynamically typed?
- Python is a dynamically typed language i.e, we don’t have to declare the type of variables while using. We can just type x=23 or x=”hello” and python automatically detects its type. 

Que 8 - What are the built-in data types in python?
- Datatypes in python are used to classify and categorize data items in the form of the values they hold.

  • Numeric - Integer, complex numbers, and float type.

  • Sequence - List, tuple, strings.

  • Set

  • Boolean

  • Dictionary


Que 9 - What is PEP 8?
- Python Enhancement Proposal (PEP) is a document containing information about how to format python code with maximum readability.

Que 10 - What are float data-type in python?
- In python floats are basic numeric values that contain decimal values.

Que 11 - What is the difference between .py and .pyc files in python?
- In python .py files contain the source code and .pyc files contain the bytecode.

Que 12 - What is List datatype in python?
- In python List is a sequential datatype. It is used to store n number of data items in sequential form. Each data item in the list has an index value. The list is defined using square brackets [].

Que 13 - What are the properties of the List?
- Some main properties of the list are:

  • A list is a sequential data type, meaning each data item has a defined position or index value.

  • The list is an ordered datatype.

  • The list is mutable. You can update, insert, and delete items inside a list.

  • Each data item in the list holds two memory locations. Which makes the list slower than tuples and other data types.

  • A list allows repetitions.


Que 14 - What are namespaces in python?
- In python, a namespace is a system to control the names in a program.  It is a collection of all previously defined names along with much more information. It makes ensure that all the names in our program are different to avoid conflicts.

Que 15 - What are some main list operations in python?
- these are some commonly used list operations in python.

  • append() - it is used to add an element to the last index of the list.

  • pop() - It is used to remove the last data item from the list.

  • index() - it returns the index value of a particular element.

  • insert() - It is used to insert a value at a defined index inside the list.

  • remove() - It is used to remove a particular item from a list.

  • sort() - It is used to sort a particular list.


Que 16 - What are functions in python?
- A function is a code block executed only when called. Generally, the def keyword is used to define a function in python. 

Que 17 - What are local variables in python?
- these are the variables declared inside the function. It can’t be accessed outside the function.

Que 18 - What are global variables in python?
- these are the variables declared outside the function. It can be accessed by any function in our program.

Que 19 - What are strings in python?
- In python string is a sequential data type. It is a sequence of characters. String behaves in the same manner as the list does in python. Strings are mutable datatypes i.e, you can update, insert, and delete a string after defining it. String in python is defined inside Quotes("Codersdaily").

Que 20 - What is the latest version of python?
- the latest version of python is Python 3.11.1.

Que 21 - What are tuples in python?
- A tuple is a sequential datatype that is ordered and immutable. This means you can’t update the tuple once it is created. A tuple is faster than a list in terms of running time as it only takes one memory location to same each data item. Tuples are defined under small brackets ().

Que 22 - What is the difference between a list and a tuple in python?



Lists are mutable

Tuples are immutable

Lists are slower

Tuples are faster as compared to Lists

Ex - lst = [1,2,3,4]

Ex - tup_ = (1,2,3,4)


Que 23 - How can we remove extra whitespaces from a python string?
- we can use strip() function to remove extra whitespaces from a string. 

a = "    codersdaily!"



Que 24 - What are sets in python?
- A set is a datatype in python which is unordered and unindexed. This means to each element inside a set has no position. The set does not allow any kind of repetition.

Que 25 - What is a join() method in python?
- A join() method is used to add multiple items into one string. 

a = ["codersdaily","python", "tutorial"]
b = ""
ans = b.join(a)



Que 26 - What are dictionaries in python?
- Dictionary is a datatype in python which works on key-value pairs. Data inside the dictionary is defined in form of keys and values. Each key in the dictionary has a corresponding value assigned to it. Dictionary is defined using curly braces{}.

Que 27 - What code to access all keys and values of a dictionary in python?
- Example:

my_dict = {"car": "BMW","model": "i8","price": "2.6 Cr."}


dict_keys(['car', 'model', 'price'])
dict_values(['BMW', 'i8', '2.6 Cr.'])

Que 28 - What is slicing in python? Explain with an example.
- Slicing is a feature in python that allows us to access specific parts of sequences like strings, lists, and tuples. We can slice sequences by using :(colon) along with the index values of our sequences.

a = ["hello", "my", "name", "is", "rohit"]
b = "hello my name is rohit"


['name', 'is', 'rohit']
hello my n

Que 29 - What are keywords in python? Name some keywords.
- Keywords are some special reserved words in python with a specific meaning. They are used to perform specific tasks. We can not use the keyword as the name of any variable or function. Some keywords in python are - And, Or, Not, If, Elif, Else, For, While, Break, Def, Lambda, Pass, Return, True, False, Try, Except, Finally, etc.

Que 30 - What are conditional statements in python?
- Conditional statements in python are used in a way like any other programming language. In general, there are three conditional statements in python if, elif, and else. if statement is the most widely used conditional statement. there could be one or more than one elif depending on the requirements. elif is used in the same way as else if is used in most programming languages. The usage of else is mostly optional.

Que 31 - Is python a case-sensitive language?
- Yes, python is a case-sensitive language.

Que 32 - what is the difference between the python List and Arrays?
- In python, both lists and arrays are used to store sequential data. But, lists are capable to store multiple data types but arrays can hold only a single data type.

Que 33 - What are “and & or” in conditional statements in python?
- In Python and(&&) conditional statement is used when there is more than one condition to be checked. And it is required that both conditions are true to run any statement. Also, or(||) in the conditional statement is used when there are more than one conditions to be checked. And it is required for one of them to be True.

Que 34 - What are Modules in python?
- In Python module is a python file that contains python code. We can use these python files in our programs by importing them. Modules can contain python code as functions, variables, and classes. We can even create our own modules in python. Some commonly used modules are - os, date time, math, random, etc.

Que 35 - What is python lambda function?
- Lambda functions in python are linear anonymous functions, as it is cleared by the word “anonymous” this function is nameless. Python lambda function can have a number of arguments but have only a single expression.

Que 36 - Explain loops in python.
- there are two types of loops in python, for loop, and while loop.

For Loop - for statement in python is used to iterate or repeat a set of statements or lists or strings or any sequence. In python, the for loop works better than the c language as it does not iterate over any numeric conditions. You can apply for loop in python over the list, string, tuple, dictionary, etc.

While Loop - While loop in python works on the concept of iterating a statement till the conditional statement is True. While loop is preferred over for loop if there is a condition to be checked before iteration occurs. It also uses less memory to iterate hence the processing speed or time complexity of the while loop is lesser than that of for loop.

Que 37 - What is range() function in python?
- If we want to iterate the loop over a sequence of numbers between a range then the range function is used inside for loop.

Que 38 - What capitalize() method do in python?
- In python, capitalize() method is used to capitalize() first letter of any string.

Que 39 - What is a pass statement in python?
- In python pass statement does not do anything. This statement is used to return a null operation in python. When the pass statement is executed nothing happens to the output but it is used to avoid empty code errors.

Que 40 - Is python an object-oriented programming language?
- Yes, python is an object-oriented programming language.

Que 41 - What are classes in python?
- Python classes provide the functionality to bundle data and functionality together. In python creating a new class creates a new object. Following the concept that everything in python is an object. Python classes could also have methods to update their states and functionalities.

Que 42 - What are exceptions in python?
- An Exception is a condition that occurs during the execution of the program and disrupts the normal flow of the program. When python comes across a condition it can’t handle an exception is raised. So, when a python program throws an exception we have to handle it else the program stops working.

Que 43 - What is the difference between Syntax Errors & Exceptions in python?
- Syntax Errors - Syntax errors are the errors that occur because of invalid syntax such as incorrect spelling, wrong labels, etc.

Exceptions - Exceptions occur when the program is correct by syntax but results in an error during the compilation. Sometimes it stops the execution of the program and sometimes it changes the normal flow of the program.

Que 44 - What is the try and except clause in python?
- In python, any exception can be handled using the try and except clause. We have to write all the code and logic inside the try clause and we have to write code to handle exceptions inside the exception clause.

Que 45 - How to uppercase and lowercase a string in python?
- To convert a string to lowercase we can use lower() function and to convert a string to uppercase we can use upper() function.

a = "codersdaily"
b = "CoDeRsDaIlY"



Que 46 - What is the finally clause in python?
- In python, we can use the finally keyword after the try and except clause. It is generally used to print the final statement or to release external resources. the finally code block is always executed even if the try block works successfully or if an exception occurs.

Que 47 - What is Inheritance in python? Write its types.
- Inheritance in python is a way of assigning properties of one class to another. Inheritance works on the concept of Parent and child class. Where in child class inherits the properties of the Parent class. Inheritance allows us to share the methods across classes. Which increases the reusability of the code.

Types of Inheritance in Python:

  • Single Level Inheritance

  • Multi-Level Inheritance

  • Multiple Inheritance

  • Herarichal Inheritance

  • Hybrid Inheritance


Que 48 - What is hybrid Inheritance in python? 
- Inheritance which is the mixture of one or more types of inheritance is known as Hybrid Inheritance.

Que 49 - What are iterators in python? 
- An iterator is a python object which has n number of elements on which we can traverse one by one.

Que 50 - What is _init_ in python? 
- _init_ is a method in python. It is similar to constructors in other languages like c++. This method is automatically called whenever an object is  created from a class.

Que 51 - What is Encapsulation in python? 
- Encapsulation in python is the concept of making variables or methods private to restrict the access at the time of inheritance. Encapsulation lets you define variables as private variables. Who's base value is not updated from the child class.

Que 52 - What are Generators in python? 
- Python Generators are the easiest way of creating iterators. simply speaking, a generator is a function that returns an object(iterator) that we can iterate over(one value at a time).

Que 53 - How to comment multiple lines in python? 
- we can comment on multiple lines in python by using the hash symbol (#) in starting of the line.

Que 54 - What are Decorators in python? 
- Decorators in python are used to modify and extend the properties of an existing function. In decorators, functions are used as an argument in other functions and then called inside a wrapper function. Decorators allow us to wrap another function in order to extend the behaviour of the wrapped function, without permanently modifying it.

Que 55 - Write code to create a 3d array in python. 
- Here, we have created a 3d array using NumPy.

import numpy as cd
_3darray = cd.array([[1,2,3,4], [5,6,7,8],[3,4,5,6] ])
print (_3darray)


[[1 2 3 4]
 [5 6 7 8]
 [3 4 5 6]]

Que 56 - What is Operator Overloading in python? 
- Operator Overloading means giving extended meaning beyond their predefined operational meaning. For example operator + is used to add two integers as well as join two strings and merge two lists. It is achievable because ‘+’ operator is overloaded by the int class and str class.

Que 57 - What is Self in python? 
- Self is the instance of a class in python. By using it we can access the attributes and methods of a class in python.

Que 58 - What is the os module in python? 
- os module helps us to make a connection between the user’s python console and operating system so, we can interact with the operating system.

Que 59 - What code to create a half pyramid pattern in python? 


a = int(input("enter total number of rows: "))
for i in range(a+1):
    print(i*"* ")


enter total number of rows: 8
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * *


Que 60 - What is a random module in python? 
- In python, we can use the random module to generate random numbers. The generated numbers are pseudo-random  i.e, they are not fully random.