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.
Question:- Give the output of this example: A[3] if A=[1,4,6,7,9,66,4,94].
Answer:- Since indexing starts from zero, an element present at 3rd index is 7. So, the output is 7.
Question:- What is type conversion in Python?
Answer:- Type conversion refers to the conversion of one data type iinto another. - int() - converts any data type into integer type - float() - converts any data type into float type - ord() - converts characters into integer - hex() - converts integers to hexadecimal - oct() - converts integer to octal - tuple() - This function is used to convert to a tuple. - set() - This function returns the type after converting to set. - list() - This function is used to convert any data type to a list type. - dict() - This function is used to convert a tuple of order (key,value) into a dictionary. - str() - Used to convert integer into a string. - complex(real,imag) - This functionconverts real numbers to complex(real,imag) number.
Question:- How to send an email in Python Language?
Answer:- To send an email, Python provides smtplib and email modules. Import these modules into the created mail script and send mail by authenticating a user. It has a method SMTP(smtp-server, port). It requires two parameters to establish SMTP connection.
Question:- What is the difference between Python Arrays and lists?
Answer:- Arrays and lists, in Python, have the same way of storing data. But, arrays can hold only a single data type elements whereas lists can hold any data type elements.
Question:- What is lambda function in Python?
Answer:- The anonymous function in python is a function that is defined without a name. The normal functions are defined using a keyword "def", whereas, the anonymous functions are defined using the lambda function. The anonymous functions are also called as lambda functions.
Question:- Why do lambda forms in Python not have the statements?
Answer:- Lambda forms in Python does not have the statement because it is used to make the new function object and return them in runtime.
Question:- What are functions in Python?
Answer:- A function is a block of code which is executed only when it is called. To define a Python function, the def keyword is used.
Question:- What is __init__?
Answer:- The __init__ is a method or constructor in Python. This method is automatically called to allocate memory when a new object/ instance of a class is created. All classes have the __init__ method.
Question:- What is PYTHONPATH?
Answer:- PYTHONPATH is an environment variable which is used when a module is imported. Whenever a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.
Question:- What are python modules? Name some commonly used built-in modules in Python?
Answer:- Python modules are files containing Python code. This code can either be functions classes or variables. A Python module is a .py file containing executable code. Some of the commonly used built-in modules are: - os - sys - math - random - data time - JSON
