Instagram
youtube
Facebook
Twitter

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?
Ans
- 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?
Ans
- 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?
Ans
- 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?
Ans
- 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?
Ans
- 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?
Ans
- In python floats are basic numeric values that contain decimal values.

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

Que 12 - What is List datatype in python?
Ans
- 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?
Ans
- 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?
Ans
- 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?
Ans
- 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?
Ans
- 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?
Ans
- these are the variables declared inside the function. It can’t be accessed outside the function.

Que 18 - What are global variables in python?
Ans
- 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?
Ans
- 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?
Ans
- the latest version of python is Python 3.11.1.

Que 21 - What are tuples in python?
Ans
- 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?
Ans

List

Tuple

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?
Ans
- we can use strip() function to remove extra whitespaces from a string. 
Example:

a = "    codersdaily!"
print(a)
print(a.strip())

Output: 

    codersdaily!
codersdaily!

Que 24 - What are sets in python?
Ans
- 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?
Ans
- A join() method is used to add multiple items into one string. 
Example:

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

Output: 

codersdailypythontutorial

Que 26 - What are dictionaries in python?
Ans
- 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?
Ans
- Example:

my_dict = {"car": "BMW","model": "i8","price": "2.6 Cr."}
print(my_dict.keys())
print(my_dict.values())

Output:

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

Que 28 - What is slicing in python? Explain with an example.
Ans
- 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.
Example:

a = ["hello", "my", "name", "is", "rohit"]
print(a[2:5])
b = "hello my name is rohit"
print(b[0:10])

Output:

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

Que 29 - What are keywords in python? Name some keywords.
Ans
- 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?
Ans
- 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?
Ans
- Yes, python is a case-sensitive language.

Que 32 - what is the difference between the python List and Arrays?
Ans
- 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?
Ans
- 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?
Ans
- 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?
Ans
- 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.
Ans
- 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?
Ans
- 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?
Ans
- In python, capitalize() method is used to capitalize() first letter of any string.

Que 39 - What is a pass statement in python?
Ans
- 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?
Ans
- Yes, python is an object-oriented programming language.

Que 41 - What are classes in python?
Ans
- 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?
Ans
- 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?
Ans
- 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?
Ans
- 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?
Ans
- To convert a string to lowercase we can use lower() function and to convert a string to uppercase we can use upper() function.
Example:

a = "codersdaily"
b = "CoDeRsDaIlY"
print(a.upper())
print(a.lower())

Output:

CODERSDAILY
codersdaily


Que 46 - What is the finally clause in python?
Ans
- 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.
Ans
- 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? 
Ans
- Inheritance which is the mixture of one or more types of inheritance is known as Hybrid Inheritance.

Que 49 - What are iterators in python? 
Ans
- 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? 
Ans
- _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? 
Ans
- 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? 
Ans
- 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? 
Ans
- 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? 
Ans
- 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. 
Ans
- Here, we have created a 3d array using NumPy.
Example:

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


Output:

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

Que 56 - What is Operator Overloading in python? 
Ans
- 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? 
Ans
- 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? 
Ans
- 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? 
Ans
-

Code:

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


Output:
 

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

 

Que 60 - What is a random module in python? 
Ans
- 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.