Question : Is indentation required in python?
Answer : Yes, indentation is required in Python. Use tabs instead of single spaces to resolve the silly indentation errors. Indentations make the code easy to read for the developers in all the programming languages but in Python, it is very important to indent the code in a specific order. Mainly the developers use “ tabs ” for indentation in Python programs.
Question : What is the use of Assertions in Python?
Answer : Assert statement is used to evaluate the expression attached. If the expression is false, then python raises an AssertionError Exception.
Question : What Is the Purpose of the Pass Statement?
Answer : The pass statement is used when there’s a syntactic but not an operational requirement. For example – The program below prints a string ignoring the spaces.
var=”Si mplilea rn”
for i in var:
if i==” “:
pass
else:
print(i,end=””)
Here, the pass statement refers to ‘no action required.’
Question : How Is Multithreading Achieved in Python?
Answer : Multithreading usually implies that multiple threads are executed concurrently. The Python Global Interpreter Lock doesn’t allow more than one thread to hold the Python interpreter at that particular point of time. So multithreading in python is achieved through context switching. It is quite different from multiprocessing which actually opens up multiple processes across multiple threads.
Question : What is the Difference Between a Shallow Copy and Deep Copy?
Answer : Deepcopy creates a different object and populates it with the child objects of the original object. Therefore, changes in the original object are not reflected in the copy.
copy.deepcopy() creates a Deep Copy.
Shallow copy creates a different object and populates it with the references of the child objects within the original object. Therefore, changes in the original object are reflected in the copy.
Question : How Will You Merge Elements in a Sequence?
Answer : There are three types of sequences in Python:
- Lists
- Tuples
- Strings
- Example of Lists –
l1=[1,2,3]
l2=[4,5,6]
l1+l2
Output: [1,2,3,4,5,6]
Question : Example of Tuples –
Answer : >>t1=(1,2,3)
t2=(4,5,6)
t1+t2
Output: (1,2,3,4,5,6)
Question : Example of String –
Answer : >>s1=“Simpli”
s2=“learn”
s1+s2
Output: ‘Simplilearn’
Question : How Would You Remove All Leading Whitespace in a String?
Answer : Python provides the inbuilt function lstrip() to remove all leading spaces from a string.
“ Python”.lstrip
Output: Python
Question : What does len() do?
Answer : “ len() ” is used to measure the number of elements/items in the List, String, etc.
It will return the number of characters if the object is a string.
Example:
a = "Python Language"
b = len(a)
print(b)
Output: 15
Question : What is Concatenation technique in Python?
Answer : You can use “+”. Check the below example:
firstName = “Srini”
lastName = “mf”
name = firstName + lastName
Result: Srinimf
Question : What are the top Number Data types in Python?
Answer : Those are “int”, “float”.
Question : Why is Python different from other languages such as Java, C, C++?
Answer : Basically, Python is an interpreted language. So, the Python code will execute dynamically. Compilation of Python program not required.
Question :How can you create a GUI-based application in Python for client-side functionality?
Answer : Python along with the standard library “Tkinter” is used to create GUI-based applications. Tkinter library supports various widgets that can create and handle events that are widget specific.
Question : Does the same Python code work on multiple platforms without any changes?
Answer : Yes. As long as you have the Python environment on your target platform (Linux, Windows, Mac), you can run the same code.
Question : Can you write code to determine the name of an object in Python?
Answer : No objects in Python have any associated names. So there is no way of getting the one for an object. The assignment is only the means of binding a name to the value. The name then can only refer to access the value. The most we can do is to find the reference name of the object.
Example.
class Test:
def init(self, name):
self.cards = []
self.name = name
def str(self):
return ‘{} holds …’.format(self.name)
obj1 = Test(‘obj1’)
print obj1
obj2 = Test(‘obj2’)
print obj2
Question :What is the best way to split a string in Python?
Answer : We can use Python function to break a string into substrings based on the defined separator. It returns the list of all words present in the input string.
test = “I am learning Python.”
print test.split(” “)
Program Output.
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux
[‘I’, ‘am’, ‘learning’, ‘Python.’]
Question :What is the function to randomize the items of a list in-place?
Answer : Python has a built-in module called as . It exports a public method )> which can randomize any input sequence.
import random
list = [2, 18, 8, 4]
print “Prior Shuffling – 0”, list
random.shuffle(list)
print “After Shuffling – 1”, list
random.shuffle(list)
print “After Shuffling – 2”, list
Question : What is the best way to parse strings and find patterns in Python?
Answer : Python has built-in support to parse strings using the Regular expression module.
Perform the following steps to perform the parsing:
- Import the module and use the functions to find a substring.
- Replace a part of a string, etc.
Question : How do you make use of Arrays in Python?
Answer : Python does not have in-built data structures like arrays, and it does not support arrays. However, you can use List which can store an unlimited number of elements.
Question : What tools can be used to unit test your Python code?
Answer : The best and easiest tool used to “unit test” is the python standard library, which is also used to test the units/classes. Its features are similar to the other unit testing tools such as JUnit, TestNG.
Question : What is PIP software in the Python world?
Answer : PIP stands for Python Installer Package, which provides a seamless interface to install the various Python modules. It is a command-line tool that searches for packages over the Internet and installs them without any user interaction.
Question : Does Python allow you to program in a structured style?
Answer : Yes., Python allows to code in a structured as well as Object-oriented style. It offers excellent flexibility to design and implement the application code depending on the requirements of the application.
Question : Mention at least 3-4 benefits of using Python over the other scripting languages such as Javascript.
Answer : Enlisted below are some of the benefits of using Python:
- Application development is faster and easy.
- Extensive support of modules for any kind of application development, including data analytics/machine learning/math-intensive applications.
- Community is always active to resolve user’s queries.
Question : Can Python be used for web client and web server side programming? Which one is best suited to Python?
Answer : Python is best suited for web server-side application development due to its vast set of features for creating business logic, database interactions, web server hosting, etc.
Question : What is the result of the below Python code?
Answer : keyword = ‘aeioubcdfg’
print keyword [:3] + keyword [3:]
The above code will produce the following result.
<‘aeioubcdfg’>
In Python, while performing string slicing, whenever the indices of both the slices collide, a <+> operator get applied to concatenates them.
Question : What is the right way to transform a Python string into a list?
Answer : In Python, strings are just like lists. And it is easy to convert a string into the list. Simply by passing the string as an argument to the list would result in a string-to-list conversion.
list(“I am learning Python.”)
Program Output.
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux
=> [‘I’, ‘ ‘, ‘a’, ‘m’, ‘ ‘, ‘l’, ‘e’, ‘a’, ‘r’, ‘n’, ‘i’, ‘n’, ‘g’, ‘ ‘, ‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’, ‘.’]
Question : How do you launch sub-processes within the main process of a Python application?
Answer : Python has a built-in module called sub-process. You can import this module and either use run() or Popen() function calls to launch a subprocess and get control of its return code.
Question : How is Exception Handling done in Python?
Answer : There are 3 main keywords:
- Try: Try is the block of a code that is monitored for errors.
- Except: This block gets executed when an error occurs.
- Catch: The beauty of the final block is to execute the code after trying for error. This block gets executed irrespective of whether an error occurred or not. Finally block is used to do the required cleanup activities of objects/variables.
Posted Date:- 2021-08-16 04:05:47
Question : How does Lambda function differ from a normal function in Python?
Answer : Lambda is similar to the inline function in C programming. It returns a function object. It contains only one expression and can accept any number of arguments.
Question : How will you share global variables across modules?
Answer : If you add a variable to the <__builtin__> module, it will be accessible as if a global from any other module that includes <__builtin__> — which is all of them, by default.
a.py contains
print foo
b.py contains
import builtin
builtin.foo = 1
import a
The result is that “1” is printed.
Note: Python 3 introduced the builtins keyword as a replacement for the <__builtin__>
Question : How to accept user INPUT in Python?
Answer : You need to use input parameter.
newInput = input(“abcdefghijk”)
print(newInput)
Result:abcdefghijk
Question : What is Concatenation technique in Python?
Answer : You can use “+”. Check the below example:
firstName = “Srini”
lastName = “mf”
name = firstName + lastName
Result: Srinimf
Question :What is indention in Python? and why this is important?
Answer : Python supports indentation. Like COBOL, if you remember, there are indentation rules that ‘IF” statement should start from 12th position.
Print (“Hello”)
Print (“World”)
Question : What are the top Looping Techniques in Python?
Answer : Those are “FOR” and “WHILE”
Question : What is the difference between abs () and fabs ()?
Answer : “abs ()” is a built-in function that works with integer, float, and complex numbers.
“fabs ()” is defined in the math module which doesn’t work with complex numbers.
Question : What do you mean by ‘suites’ in Python?
Answer : The group of individual statements, thereby making a logical block of code are called suites.
Example:
If expression
Suite
Else
Suite
Write a command to convert a string into an int in python.
int(x [,base])
Write a code to display the current time.
currenttime= time.localtime(time.time())
print (“Current time is”, currenttime)
Question : What does stringVar.strip() does?
Answer : This is one of the string methods which removes leading/trailing white space in the code.
Question : What is the difference between Assign and Equal?
Answer :
If you give single = , that is called assign.
If you give ==, that is called equal.
Question : Why Colon (“:”) is required in IF statement?
Answer :
To give statements after the IF, the colon is mandatory.
IF sale > 100 :
sale = sale + 20
else :
sale = sale – 20
Related Topic :
What is LibreOffice? LibreOffice Impress Features ?