Python Memory Page

Python Memory Notes

























1. What is __init__?


__init__ is a contructor method in Python and is automatically called to allocate memory when a new object/instance is created. All classes have a __init__ method associated with them. It helps in distinguishing methods and attributes of a class from local variables.



2. What is the difference between Python Arrays and lists?


Arrays in python can only contain elements of same data types i.e., data type of array should be homogeneous. It is a thin wrapper around C language arrays and consumes far less memory than lists. Lists in python can contain elements of different data types i.e., data type of lists can be heterogeneous. It has the disadvantage of consuming large memory.



3. Explain how can you make a Python Script executable on Unix?


Script file must begin with #!/usr/bin/env python







4. What is the use of self in Python?


Self is used to represent the instance of the class. With this keyword, you can access the attributes and methods of the class in python. It binds the attributes with the given arguments. self is used in different places and often thought to be a keyword. But unlike in C++, self is not a keyword in Python.




The pass keyword represents a null operation in Python. It is generally used for the purpose of filling up empty blocks of code which may execute during runtime but has yet to be written. Without the pass statement in the following code, we may run into some errors during code execution.



5. What is docstring in Python?


Documentation string or docstring is a multiline string used to document a specific code segment.



6. What is break, continue and pass in Python?


Break: The break statement terminates the loop immediately and the control flows to the statement after the body of the loop. Continue: The continue statement terminates the current iteration of the statement, skips the rest of the code in the current iteration and the control flows to the next iteration of the loop. Pass: The pass keyword in Python is generally used to fill up empty blocks and is similar to an empty statement represented by a semi-colon in languages such as Java, C++, Javascript, etc.







7. What are global, protected and private attributes in Python?


Global variables are public variables that are defined in the global scope. To use the variable in the global scope inside a function, we use the global keyword. Protected attributes are attributes defined with an underscore prefixed to their identifier eg. _sara. They can still be accessed and modified from outside the class they are defined in but a responsible developer should refrain from doing so. Private attributes are attributes with double underscore prefixed to their identifier eg. __ansh. They cannot be accessed or modified from the outside directly and will result in an AttributeError if such an attempt is made.



8. What are modules and packages in Python?


Modules, in general, are simply Python files with a .py extension and can have a set of functions, classes, or variables defined and implemented. They can be imported and initialized once using the import statement. If partial functionality is needed, import the requisite classes or functions using from foo import bar. Packages allow for hierarchial structuring of the module namespace using dot notation. As, modules help avoid clashes between global variable names, in a similar manner, packages help avoid clashes between module names.



9. What is PEP 8 and why is it important?


PEP stands for Python Enhancement Proposal. A PEP is an official design document providing information to the Python community, or describing a new feature for Python or its processes. PEP 8 is especially important since it documents the style guidelines for Python Code.







10. What are Python decorators?


Decorators are a way to modify or extend the behavior of functions or methods without changing their actual code. They are usually defined with the @decorator_name syntax.



11. How do you convert a list to a set in Python?


You can convert a Python list to a set using the set() function. This removes duplicate elements from the list.



12. What is the difference between str and repr in Python?


The str function is meant to return a human-readable representation of an object, while repr is meant to return an “official” string representation that can ideally be used to recreate the object.







13. How do you check the type of an object in Python?


You can check the type of an object in Python using the type() function.



14. How do you convert a string to an integer?


You can convert a string to an integer using the int() function.



15. What is slicing in Python?


Slicing in Python is a powerful technique for extracting portions of sequences, like strings, lists, tuples, and other iterable objects.







16. How to check if a string contains only digits?


The isdigit() method returns True if all characters in the string are digits, and False otherwise.



17. How to removes any leading and trailing whitespace from a string ?


The strip() method removes any leading and trailing whitespace (spaces, tabs, or newline characters) from a string.



18. How to replace substring in Python?


You can replace a substring within a string in Python using the replace() method.







19. What are functions in Python?


A function in Python is a block of reusable code designed to perform a specific task.



20. What is the return statement used for?


The return statement is used to exit a function and optionally return a value to the caller.



21. How does the zip() function work?


The zip() function takes multiple iterables (like lists, tuples, or strings) as input and returns an iterator of tuples. Each tuple contains corresponding elements from the input iterables.







22. What are dictionaries in Python?


Dictionaries in Python are unordered collections of key-value pairs.



23. What are sets in Python?


Sets in Python are unordered collections of unique elements.



24. What is the purpose of the finally clause?


The finally clause is an optional part of the try-except block. The code within the finally block always executes, regardless of whether an exception was raised or not.







25. Is python case sensitive?


Yes. Python is a case sensitive language.



26. What is type conversion in Python?


Type conversion refers to the conversion of one data type into another.



List Memory Pages
Share Via Whastapp/Facebook
Share to Your Friends

Share this protal to share friends and complete unlimited tests here. You can also make friends on our protal also start mutual competition tests with your firends easily.

Share to Facebook Share to WhatsApp Promote & Earn