Give the output of this example: A[3] if A=[1,4,6,7,9,66,4,94].
Since indexing starts from zero, an element present at 3rd index is 7. So, the output is 7.
What is PEP 8?
PEP 8 is defined as a document that helps us to provide the guidelines on how to write the Python code. It was written by Guido van Rossum, Barry Warsaw and Nick Coghlan in 2001.
It stands for Python Enhancement Proposal, and its major task is to improve the readability and consistency of Python code.
What is a dictionary in Python?
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.
Keys index dictionaries.
Let’s take an example.
The following example contains some keys Country Hero & Cartoon. Their corresponding values are India, Modi, and Rahul respectively.
>>> dict = {‘Country’: ‘India’, ‘Hero’: ‘Modi’, ‘Cartoon’: ‘Rahul’}
>>>print dict[Country]
India
>>>print dict[Hero]
Modi
>>>print dict[Cartoon]
Rahul
Why do lambda forms in Python not have the statements?
Because it is used to make the new function object and return them in runtime.
How to remove whitespaces from a string in Python?
To remove the whitespaces and trailing spaces from the string, Python providies strip([str]) built-in function. This function returns a copy of the string after removing whitespaces if present. Otherwise returns original string.
string = ” javatpoint “
string2 = ” javatpoint “
string3 = ” javatpoint”
print(string)
print(string2)
print(string3)
print(“After stripping all have placed in a sequence:”)
print(string.strip())
print(string2.strip())
print(string3.strip())
javatpoint
javatpoint
javatpoint
How can we make forms in Python?
You have to import CGI module to access form fields using FieldStorage class.
Attributes of class FieldStorage for the form:
form.name: The name of the field, if specified.
form.filename: If an FTP transaction, the client-side filename.
form.value: The value of the field as a string.
form.file: file object from which data read.
form.type: The content type, if applicable.
form.type_options: The options of the ‘content-type’ line of the HTTP request, returned as a dictionary.
form.disposition: The field ‘content-disposition’; None, if unspecified.
form.disposition_options: The options for ‘content-disposition’.
form.headers: All of the HTTP headers returned as a dictionary.
import cgi
form = cgi.FieldStorage()
if not (form.has_key(“name”) and form.has_key(“age”)):
print ”Name & Age not Entered”
print “Fill the Name & Age accurately.”
return
print ”
name:”, form[“name”].value
print ”
Age:”, form[“age”].value
How is memory managed in Python?
Memory is managed in Python by the following way:
- The Python memory is managed by a Python private heap space. All the objects and data structures are located in a private heap. The programmer does not have permission to access this private heap.
- We can easily allocate heap space for Python objects by the Python memory manager. The core API gives access of some tools to the programmer for coding purpose.
- Python also has an inbuilt garbage collector, which recycle all the unused memory and frees the memory for the heap space.
What is the Python decorator?
Decorators are very powerful and a useful tool in Python that allows the programmers to modify the behaviour of any class or function. It allows us to wrap another function to extend the behaviour of the wrapped function, without permanently modifying it.
# Decorator example
def decoratorfun():
return another_fun
Functions vs. Decorators
A function is a block of code that performs a specific task whereas a decorator is a function that modifies other functions.
What will be the output of [‘!!Welcome!!’]*2?
The output will be [‘!!Welcome!! ‘, ‘!!Welcome!!’]
Explain Python Functions?
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. Python provides several built-in functions to complete tasks and also allows a user to create new functions as well.
There are two types of functions:
Built-In Functions: copy(), len(), count() are the some built-in functions.
User-defined Functions: Functions which are defined by a user known as user-defined functions.
Example: A general syntax of user defined function is given below.
def function_name(parameters list):
#— statements—
return a_value