General Python Questions and Answers
Discover General Python Questions and Answers to enhance your programming skills. Prepare effectively for interviews and deepen your understanding of Python.
Q 1. Why are strings immutable in Python?
Strings in Python are “immutable” which means they can not be changed after they are created.
Q 2. Python program to capitalize the first and last character of each word in a string?
Q 3. Python Program to swap the First and the Last Characters of a string.
Method 1:
Method 2:
Q 4. What is split () in Python?
In Python, the split() function is used to split a string into a list of substrings based on a specified delimiter. Here’s how the split() function works.
Syntax:
str.split(separator, maxsplit)
- separator (optional): This is the delimiter on which the string is split. If not specified, any whitespace (spaces, tabs, newlines) is used as the delimiter.
- maxsplit (optional): It specifies the maximum number of splits. If provided, the string is split at most maxsplit times. The default value of maxsplit is -1, meaning no limit on the number of splits.
Example1:
Example2:
Q 5. What is slicing in Python?
As the name suggests, ‘slicing’ is taking parts of.
Syntax for slicing is [start: stop: step]
- start is the starting index from where to slice a list or tuple
- stop is the ending index or where to sop.
- step is the number of steps to jump.
The default value for start is 0, stop is a number of items, and step is 1.
Slicing can be done on strings, arrays, lists, and tuples.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(numbers[1 :: 2]) #output : [2, 4, 6, 8, 10]
Q 6. What is docstring in Python?
Documentation string or docstring is a multiline string used to document a specific code segment. The docstring should describe what the function or method does.
Q 7. Explain the join() function in Python.
You can use the join() function to join a list of strings based on a delimiter to give a single string.
Q 8. What is PYTHONPATH in Python?
PYTHONPATH is an environment variable that you can set to add additional directories where Python will look for modules and packages. This is especially useful in maintaining Python libraries you do not wish to install in the global default location.
Q 9. What is the difference between range and range in Python?
range () and range() are quite similar in terms of functionality. They both generate a sequence of integers, with the only difference being that range() returns a Python list, whereas, xrange() returns an xrange object.
So how does that make a difference? It sure does, because unlike range(), xrange() doesn’t generate a static list, it creates the value on the go.
Note: xrange has been deprecated as of Python 3.x. Now range does exactly the same as what xrange used to do in Python 2. x, since it was way better to use xrange() than the original range() function in Python 2. x
Q 10. How do you copy an object in Python?
In Python, the assignment statement (= operator) does not copy objects. Instead, it creates a binding between the existing object and the target variable name. To create copies of an object in Python, we need to use the copy module. Moreover, there are two ways of creating copies for the given object using the copy module –
A shallow Copy is a bit-wise copy of an object. The copied object created has an exact copy of the values in the original object. If either of the values is a reference to other objects, just the reference addresses for the same are copied.
Deep Copy copies all values recursively from the source to the target object, i.e. it even duplicates the objects referenced by the source object.
For Free, Demo classes Call: 02071171500
Registration Link: Online Python Training in Pune!
Q 11. What is an iterator in Python?
An iterator is an object (like a pointer) that points to an element inside the container. We can use iterators to move through the contents of the container. They can be visualised as something similar to a pointer pointing to some location and we can access content at that particular location using them.
Q 12. What Is Yield In Python?
The Yield keyword in Python is similar to a return statement used for returning values or objects in Python. However, there is a slight difference. The yield statement returns a generator object to the one who calls the function which contains yield, instead of simply returning a value.
Q 13. What is a generator in Python?
In Python, generator functions are those functions that, instead of returning a single value, return an iterable generator object. You can access or read the values returned from the generator function stored inside a generator object one by one using a simple loop or using next() or list() methods.
You can create a generator function using the generator() and yield keywords. Consider the example below.
Do visit our channel: Click Here
Author:-
Deepali Shinkar
Call the Trainer and Book your free demo Class For Python
Call now!!! | SevenMentor Pvt Ltd.
© Copyright 2021 | SevenMentor Pvt Ltd.