Python Quetion and Answers
Python Quetion and Answers
• Python zip() function returns a zip object, which maps a similar index of multiple
containers.
• It takes an iterable, convert into iterator and aggregates the elements based on
iterables passed. It returns an iterator of tuples.
• Pass by references
• Pass by value
5. What is PEP 8?
• PEP 8 stands for Python Enhancement Proposal, it can be defined as a document that
helps us to provide the guidelines on how to write the Python code.
• It is basically a set of rules that specify how to format Python code for maximum
readability.
• 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.
• A function is a section of the program or a block of code that is written once and can
be executed whenever required in the program.
• A function is a block of self-contained statements which has a valid name, parameters
list, and body.
• Functions make programming more functional and modular to perform modular
tasks.
• The break statement is used to terminate the execution of the current loop.
• Break always breaks the current execution and transfer control to outside the current
block.
• If the block is in a loop, it exits from the loop, and if the break is in a nested loop, it
exits from the innermost loop.
• The Python provides libraries/modules that enable you to manipulate text files and
binary files on the file system. It helps to create files, update their contents, copy, and
delete files. The libraries are os, os.path, and shutil.
• Here, os and os.path - modules include a function for accessing the filesystem
• To remove the whitespaces and trailing spaces from the string, Python provides
strip([str]) built-in function.
• This function returns a copy of the string after removing whitespaces if present.
Otherwise returns original string.
Python uses a rich set of operators to perform a variety of operations. Some individual
operators like membership and identity operators are not so familiar but allow to perform
operations.
20. What are the different file processing modes supported by Python?
Python provides four modes to open files. The read-only (r), write-only (w), read-write (rw)
and append mode (a). 'r' is used to open a file in read-only mode, 'w' is used to open a file in
write-only mode, 'rw' is used to open in reading and write mode, 'a' is used to open a file in
append mode. If the mode is not specified, by default file opens in read-only mode.
• Read-only mode (r): Open a file for reading. It is the default mode.
• Write-only mode (w): Open a file for writing. If the file contains data, data would be
lost. Other a new file is created.
• Read-Write mode (rw): Open a file for reading, write mode. It means updating mode.
• Append mode (a): Open for writing, append to the end of the file, if the file exists.
• Decorators are very powerful and a useful tool in Python that allows the programmers
to add functionality to an existing code.
• This is also called metaprogramming because a part of the program tries to modify
another part of the program at compile time.
• It allows the user to wrap another function to extend the behavior of the wrapped
function, without permanently modifying it.
23. What are the rules for a local and global variable in Python?
Global Variables
• Variables declared outside a function or in global space are called global variables.
• Global variables are accessible anywhere in the program, and any function can access
and modify its value.
Local Variables
• Any variable declared inside a function is known as a local variable. This variable is
present in the local space and not in the global space.
• Local variables are accessible within local body only.
• In Python 3, the old Unicode type has replaced by "str" type, and the string is treated
as Unicode by default. We can make a string in Unicode by using
art.title.encode("utf-8") function.
• In Python, iterators are used to iterate a group of elements, containers like a list.
Iterators are the collection of items, and it can be a list, tuple, or a dictionary. Python
iterator implements __itr__ and next() method to iterate the stored elements.
• In Python, we generally use loops to iterate over the collections (list, tuple).
• Slicing is a mechanism used to select a range of items from sequence type like list,
tuple, and string.
• It is beneficial and easy to get elements from a range by using slice way. It requires a :
(colon) which separates the start and end index of the field.
• All the data collection types List or tuple allows us to use slicing to fetch elements.