Python New Memory Page

Python New Memory Notes

























1. What is Python?


Python is a high-level, interpreted, general-purpose programming language.



2. Is Python case sensitive?


Yes, Python is absolutely case-sensitive.



3. How is Python interpreted?


Python code is executed line by line by the Python interpreter, making it an interpreted language.







4. What is __init__?


__init__ is a contructor method in Python and is automatically called to allocate memory when a new object/instance is created.



5. How do you check the data type of a variable in Python?


You can check the data type of a variable using the type() function in Python.



6. What is an array in Python?


An array is a data structure that can hold multiple values of the same data type.







7. What is a list in Python?


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.



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


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



9. 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.







10. What is pass 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.



11. What is docstring in Python?


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



12. What is break statement in Python?


The break statement terminates the loop immediately and the control flows to the statement after the body of the loop.







13. What is continue statement in Python?


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.



14. What is pass statement in Python?


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.



15. What are global 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.







16. What are protected attributes in Python?


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.



17. What are private attributes in Python?


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.



18. What are modules 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.







19. What are packages in Python?


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.



20. What is PEP 8?


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.



21. What are Python decorators?


Decorators are a way to modify or extend the behavior of functions or methods without changing their actual code.







22. How are Python decorators defined?


Python decorators are defined with the @decorator_name syntax.



23. 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.



24. 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.







25. 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.



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


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



27. 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.







28. 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.



29. 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.



30. How to replace substring in Python?


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







31. What are functions in Python?


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



32. What is the return statement used for?


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



33. 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.







34. What are dictionaries in Python?


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



35. How do you access values in a Python dictionary?


You can access values in a dictionary using the keys in Python.



36. How do you merge two dictionaries in Python?


You can merge two dictionaries using the update() method or the ** unpacking operator in Python.







37. How do you create a dictionary with default values?


You can create a Python dictionary with default values using the defaultdict from the collections module.



38. How do you get a value from a dictionary with a default value if the key does not exist?


You can use the get() method to retrieve a value from a dictionary and provide a default value if the key does not exist.



39. How do you create a nested dictionary?


A nested dictionary is a dictionary within a dictionary. You can create it by assigning a dictionary as a value to a key.







40. How do you flatten a nested dictionary in Python?


Flattening a nested dictionary involves converting it into a single-level dictionary. This can be done using a custom function.



41. How do you get the maximum and minimum values in a Python dictionary?


You can get the maximum and minimum values in a Python dictionary using the max() and min() functions along with the values() method.



42. How do you convert a dictionary into a list of tuples?


You can convert a dictionary into a list of tuples using the items() method in Python.







43. How do you convert a dictionary to a JSON string?


You can convert a dictionary to a JSON string using the json.dumps() method from the json module in Python.



44. What are sets in Python?


Sets in Python are unordered collections of unique elements.



45. 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.







46. What is type conversion in Python?


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



47. What are the types of loops in Python?


for loop and while loop.



48. What is the purpose of the range() function in a for loop?


The range() function is used to generate a sequence of numbers that a for loop can iterate over.







49. Why is indentation significant in Python?


Indentation in Python is crucial because it’s how the language defines code blocks (groups of statements).



50. What is mutability in Python?


Mutability refers to the ability of an object to change its state or contents after it is created.



51. What are frozensets in Python?


Frozensets are immutable sets. They are like sets but cannot be changed once created. They are defined using the frozenset() function.







52. What is a tuple in Python?


A tuple in Python is an immutable, ordered collection of elements.



53. Explain the use of namedtuple in Python?


namedtuple is a factory function for creating tuple subclasses with named fields, making the code more readable and self-documenting.



54. How do you repeat elements in a tuple?


You can repeat elements in a tuple using the * operator in Python.







55. How do you find the length of a string in Python?


You can find the length of a string using the len() function in Python.



56. How do you convert a Python string to uppercase or lowercase?


You can convert a string to uppercase using the upper() method and to lowercase using the lower() method in Python.



57. How do you split a string into a list of substrings?


You can split a string into a list of substrings using the split() method in Python.







58. How do you add elements to a list in Python?


You can add elements to a list using the append() method to add a single element, or the extend() method to add multiple elements.



59. How do you remove elements from a list?


You can remove elements from a list using the Python remove() method, the pop() method, or the del statement.



60. How do you sort a list in Python?


You can sort a list in Python using the sort() method for in-place sorting or the sorted() function for returning a new sorted list in Python.







61. How do you copy a list in Python?


You can copy a list using the copy() method, the slicing technique, or the list() constructor in Python.



62. How do you extend an array with another array in Python?


You can extend an array with another array using the extend() method.



63. How do you insert an element at a specific position in an array in Python?


You can insert an element at a specific position in an array using the insert() method.







64. How do you get the buffer information of an array?


You can get the buffer information of an array using the buffer_info() method.



65. How do you count the occurrences of an element in an array in Python?


You can count the occurrences of an element in an array using the count() method.



66. How do you read the content of a file in Python?


You can read the content of a file using the read() method.







67. How do you open a file in Python?


You can open a file in Python using the open() function, which returns a file object.



68. How do you write to a file in Python?


You can write to a file using the write() method. If the file does not exist, it will be created.



69. How do you read a file line by line in Python?


You can read a file line by line using the readline() method or by iterating over the file object.







70. How do you check if a file exists in Python?


You can check if a file exists using the os.path.exists() method from the os module.



71. How do you delete a file in Python?


You can delete a file using the os.remove() method from the os module.



72. How do you rename a file in Python?


You can rename a file using the os.rename() method from the os module.







73. How do you get the size of a file in Python?


You can get the size of a file using the os.path.getsize() method from the os module.



74. Does Python have a built-in switch-case statement?


No, Python does not have a built-in switch-case statement like some other programming languages.



75. How do you handle multiple cases with the same outcome using the match statement?


You can handle multiple cases with the same outcome by separating the cases with a pipe (|).







76. How can you use lambdas in a dictionary-based switch-case implementation?


You can use lambda functions in a dictionary to perform calculations or operations directly.



77. How do you get the current date and time in Python?


You can get the current date and time using the datetime.now() method from the datetime module.



78. How do you format a date in Python?


You can format a date using the strftime() method from the datetime module.







79. How do you parse a string into a datetime object in Python?


You can parse a string into a datetime object using the strptime() method from the datetime module.



80. How do you get the current date in Python?


You can get the current date using the date.today() method from the datetime module.



81. How do you add or subtract days from a date in Python?


You can add or subtract days from a date using the timedelta object from the datetime module.







82. How do you get the day of the week from a date in Python?


You can get the day of the week using the weekday() method, which returns an integer (0 for Monday, 6 for Sunday).



83. How do you get the current time in Python?


You can get the current time using the time() method from the datetime module.



84. How do you convert a timestamp to a datetime object in Python?


You can convert a timestamp to a datetime object using the fromtimestamp() method from the datetime module.







85. How do you perform set difference in Python?


You can perform set difference using the difference() method or the - operator.



86. How do you perform set symmetric difference in Python?


You can perform set symmetric difference using the symmetric_difference() method or the ^ operator.



87. How do you check if a set is a subset of another set in Python?


You can check if a set is a subset of another set using the issubset() method or the <







88. How do you create a custom exception in Python?


You can create a custom exception by defining a new class that inherits from the Exception class.



89. How do you handle exceptions with a context manager in Python?


You can handle exceptions in a context manager by using the with statement and a custom context manager class with __enter__ and __exit__ methods.



90. How do you log exceptions in Python?


You can log exceptions using the logging module by calling logging.exception() within an except block.







91. How do you use the assert statement for exception handling in Python?


The assert statement is used to set a condition that must be true; if the condition is false, an AssertionError is raised.



92. What is the @staticmethod decorator?


The @staticmethod decorator is used to define a method that does not operate on an instance of the class (i.e., it has no self parameter).



93. How do you manage packages in Python?


Python packages are managed using tools like pip, the package installer for Python.







94. What is a context manager in Python?


Context managers allow you to allocate and release resources precisely when you want to. The most common way to use a context manager is with the with statement.



95. Explain the concept of monkey patching in Python.


Monkey patching refers to the dynamic modifications of a class or module at runtime. This can be useful for altering the behavior of libraries or frameworks without modifying their source code.



96. How do you define a class in Python?


You define a class in Python using the class keyword followed by the class name and a colon.







97. How do you create an instance of a class in Python?


You create an instance of a class by calling the class name followed by parentheses, optionally passing arguments if the class constructor requires them.



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