Question:- Describe Functions vs. Decorators
Answer:- A function is a block of code that performs a specific task whereas a decorator is a function that modifies other functions.
Question:- What are the rules for a local and global variable in Python?
Answer:- Global Variables: - Variables declared outside a function or in global space are called global variables. - If a variable is ever assigned a new value inside the function, the variable is implicitly local, and we need to declare it as global explicitly. To make a variable globally, we need to declare it by using global keyword. - Global variables are accessible anywhere in the program, and any function can access and modify its value.
Question:- What are iterators in Python?
Answer:- 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). In simple words: Iterators are objects which can be traversed though or iterated up
Question:- What is a generator in Python?
Answer:- In Python, the generator is a way that specifies how to implement iterators. It is a normal function except that it yields expression in the function. It does not implements __itr__ and next() method and reduce other overheads as well. If a function contains at least a yield statement, it becomes a generator. The yield keyword pauses the current execution by saving its states and then resume from the same when required.
Question:- What is slicing in Python?
Answer:- 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. Although we can get elements by specifying an index, we get only single element whereas using slicing we can get a group of elements.
Question:- What is a dictionary in Python?
Answer:- The Python dictionary is a built-in data type. It defines a one-to-one relationship between keys and values. Dictionaries contain a pair of keys and their corresponding values. It stores elements in key and value pairs. The keys are unique whereas values can be duplicate. The key accesses the dictionary elements.
Question:- Explain docstring in Python?
Answer:- The Python docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. It provides a convenient way to associate the documentation. String literals occurring immediately after a simple assignment at the top are called "attribute docstrings". String literals occurring immediately after another docstring are called "additional docstrings". Python uses triple quotes to create docstrings even though the string fits on one line. Docstring phrase ends with a period (.) and can be multiple lines. It may consist of spaces and other special chars.
Question:- What is a negative index in Python and why are they used?
Answer:- The sequences in Python are indexed and it consists of the positive as well as negative numbers. The numbers that are positive uses 0 that is uses as first index and 1 as the second index and the process go on like that. The index for the negative number starts from -1 that represents the last index in the sequence and -2 as the penultimate index and the sequence carries forward like the positive number. The negative index is used to remove any new-line spaces from the string and allow the string to except the last character that is given as S[:-1]. The negative index is also used to show the index to represent the string in correct order.
Question:- What is pickling and unpickling in Python?
Answer:- The Python pickle is defined as a module which accepts any Python object and converts it into a string representation. It dumps the Python object into a file using the dump function; this process is called Pickling. The process of retrieving the original Python objects from the stored string representation is called as Unpickling.
Question:- Which programming language is a good choice between Java and Python?
Answer:- Java and Python both are object-oriented programming languages. Lets compare both on some criteria given below: -Ease of use/ Java is Good / Python is Very Good -Coding Speed/ Java is Average/ Python is Excellent -Data types/ Java is Static type/ Python is Dynamic type -Data Science and Machine learning application/ Java is Average/ Python is Very Good
Question:- What is the usage of help() and dir() function in Python?
Answer:- Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions. Help() function: The help() function is used to display the documentation string and also facilitates us to see the help related to modules, keywords, and attributes. Dir() function: The dir() function is used to display the defined symbols.
Question:- What are the differences between Python 2.x and Python 3.x?
Answer:- Python 2.x is an older version of Python. Python 3.x is newer and latest version. Python 2.x is legacy now. Python 3.x is the present and future of this language. The most visible difference between Python2 and Python3 is in print statement (function). In Python 2, it looks like print "Hello", and in Python 3, it is print ("Hello"). String in Python2 is ASCII implicitly, and in Python3 it is Unicode. The xrange() method has removed from Python 3 version. A new keyword as is introduced in Error handling.
Question:- How Python does Compile-time and Run-time code checking?
Answer:- In Python, some amount of coding is done at compile time, but most of the checking such as type, name, etc. are postponed until code execution. Consequently, if the Python code references a user-defined function that does not exist, the code will compile successfully. The Python code will fail only with an exception when the code execution path does not exist.
Question:- What is the usage of enumerate () function in Python?
Answer:- The enumerate() function is used to iterate through the sequence and retrieve the index position and its corresponding value at the same time.
