You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+129-6Lines changed: 129 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1760,7 +1760,22 @@ for i in range(7):
1760
1760
1761
1761
## Q. What is a closure in Python?
1762
1762
1763
-
A closure is said to occur when a nested function references a value in its enclosing scope. The whole point here is that it remembers the value.
1763
+
In Python, a closure is a function object that has access to variables in its enclosing lexical scope, even when the function is called outside that scope. In other words, a closure allows a function to remember and access the values of the variables in the environment where it was created, even if those variables are no longer in scope when the function is called.
1764
+
1765
+
Closures are created when a nested function references a value from its enclosing function. The enclosing function returns the nested function, which maintains a reference to the enclosed value. The enclosed value is stored in the closure, which is attached to the nested function object.
1766
+
1767
+
Here's an example of a closure in Python:
1768
+
1769
+
```py
1770
+
defouter_function(x):
1771
+
definner_function(y):
1772
+
return x + y
1773
+
return inner_function
1774
+
1775
+
closure = outer_function(10)
1776
+
result = closure(5)
1777
+
print(result) # Output: 15
1778
+
```
1764
1779
1765
1780
```py
1766
1781
defA(x):
@@ -2434,7 +2449,17 @@ You can create your own function or use one of Python\'s many built-in functions
2434
2449
2435
2450
## Q. What is recursion?
2436
2451
2437
-
When a function makes a call to itself, it is termed recursion. But then, in order for it to avoid forming an infinite loop, we must have a base condition. Let\'s take an example.
2452
+
Recursion is a programming technique in which a function calls itself to solve a problem. The idea is to break down a complex problem into smaller, simpler problems and solve them in a recursive manner until the base case is reached. The base case is a condition that stops the recursion and returns a value.
2453
+
2454
+
A common example of a recursive function is the factorial function, which calculates the product of all positive integers up to a given number. Here's an example of a recursive factorial function in Python:
2455
+
2456
+
```py
2457
+
deffactorial(n):
2458
+
if n ==0:
2459
+
return1
2460
+
else:
2461
+
return n * factorial(n-1)
2462
+
```
2438
2463
2439
2464
```py
2440
2465
deffacto(n):
@@ -4309,15 +4334,113 @@ Using bubble sort.
4309
4334
4310
4335
## Q. How will you check memory leak on Linux?
4311
4336
4312
-
- valgrind along with gcc.
4337
+
There are several tools and techniques that can be used to check for memory leaks on Linux:
4338
+
4339
+
- Valgrind: Valgrind is a powerful memory debugging tool that can be used to detect memory leaks, among other issues. It works by running the program in a simulated environment that tracks all memory allocations and deallocations, and generates a detailed report of any errors or leaks that it finds.
4340
+
4341
+
- LeakSanitizer (LSan): LSan is a lightweight tool that is built into the Clang and GCC compilers. It can be used to detect memory leaks at runtime by instrumenting the code with memory tracking code. When a leak is detected, LSan generates a report that includes the stack trace of the leaking allocation.
4342
+
4343
+
- AddressSanitizer (ASan): ASan is another tool built into the Clang and GCC compilers. It can be used to detect a wide range of memory errors, including memory leaks, buffer overflows, and use-after-free errors. Like LSan, ASan instruments the code with memory tracking code and generates a report when an error is detected.
4344
+
4345
+
- /proc/meminfo: The /proc/meminfo file contains information about the system's memory usage, including the total amount of memory, the amount of free memory, and the amount of memory used by each process. By monitoring the values in this file over time, it may be possible to detect a memory leak by looking for a steady increase in the amount of memory used by a particular process.
4346
+
4347
+
- ps and top: The ps and top commands can be used to monitor the system's memory usage and identify processes that are consuming large amounts of memory. By monitoring the memory usage of a particular process over time, it may be possible to detect a memory leak by looking for a steady increase in the amount of memory used by that process.
4348
+
4349
+
In general, detecting and diagnosing memory leaks can be a complex and time-consuming process. It often requires a combination of tools and techniques, as well as a deep understanding of the code and the system's memory usage patterns.
4313
4350
4314
4351
<divalign="right">
4315
4352
<b><a href="#">↥ back to top</a></b>
4316
4353
</div>
4317
4354
4318
-
#### Q. How can you return multiple values from a function?
4319
-
#### Q. What is the fastest way to swap the values bound to two variables?
4320
-
#### Q. What is the importance of reference counting?
4355
+
## Q. How can you return multiple values from a function?
4356
+
4357
+
In Python, you can return multiple values from a function by packing them into a tuple or a list. Here's an example using a tuple:
4358
+
4359
+
```py
4360
+
defget_user_info(user_id):
4361
+
# ... do some work to retrieve user info ...
4362
+
name ="John Doe"
4363
+
email ="johndoe@example.com"
4364
+
age =35
4365
+
return name, email, age
4366
+
4367
+
# call the function and unpack the returned values
In this example, the `get_user_info` function returns three values (`name`, `email`, and `age`) as a tuple. When the function is called, the returned tuple is unpacked into individual variables (`user_name`, `user_email`, and `user_age`) using tuple unpacking syntax. The variables can then be used as needed.
4377
+
4378
+
You can also return multiple values as a list or any other iterable. For example:
4379
+
4380
+
```py
4381
+
defget_user_info(user_id):
4382
+
# ... do some work to retrieve user info ...
4383
+
name ="John Doe"
4384
+
email ="johndoe@example.com"
4385
+
age =35
4386
+
return [name, email, age]
4387
+
4388
+
# call the function and unpack the returned values
4389
+
user_info = get_user_info(123)
4390
+
user_name, user_email, user_age = user_info
4391
+
4392
+
# print the values
4393
+
print(user_name) # Output: John Doe
4394
+
print(user_email) # Output: johndoe@example.com
4395
+
print(user_age) # Output: 35
4396
+
```
4397
+
4398
+
In this example, the get_user_info function returns a list instead of a tuple, and the returned values are unpacked into variables using list unpacking syntax.
4399
+
4400
+
4401
+
## Q. What is the fastest way to swap the values bound to two variables?
4402
+
4403
+
In Python, the fastest way to swap the values bound to two variables is to use tuple unpacking, like this:
4404
+
4405
+
```py
4406
+
a, b = b, a
4407
+
```
4408
+
4409
+
In this code, the values of a and b are swapped by creating a tuple of the values in reverse order ((`b`, `a`)) and unpacking them into the variables a and b. The assignment is performed simultaneously, which makes this method faster than using a temporary variable, which requires an additional assignment.
4410
+
4411
+
For example:
4412
+
4413
+
```py
4414
+
a =5
4415
+
b =10
4416
+
4417
+
# swap the values using tuple unpacking
4418
+
a, b = b, a
4419
+
4420
+
print(a) # Output: 10
4421
+
print(b) # Output: 5
4422
+
```
4423
+
4424
+
In this example, the values of `a` and `b` are swapped using tuple unpacking, and the final output shows that the values have indeed been swapped.
4425
+
4426
+
4427
+
## Q. What is the importance of reference counting?
4428
+
4429
+
In Python, reference counting is a technique used for automatic memory management. It involves keeping track of the number of references to an object and automatically deallocating the object when there are no more references to it.
4430
+
4431
+
The importance of reference counting in Python can be summarized as follows:
4432
+
4433
+
- Automatic memory management: Reference counting allows Python to automatically manage memory without requiring explicit memory allocation or deallocation by the programmer. This simplifies programming and reduces the risk of memory leaks and other memory-related bugs.
4434
+
4435
+
- Efficient memory management: By deallocating objects as soon as they are no longer needed, reference counting helps to minimize the amount of memory used by a program. This is especially important for long-running programs or programs that work with large data sets.
4436
+
4437
+
- Improved performance: Because Python uses reference counting to manage memory, it can often achieve better performance than other languages that use garbage collection or other memory management techniques. This is because reference counting can be faster and more predictable than other methods, especially for small or short-lived objects.
4438
+
4439
+
- Object lifetime management: Reference counting ensures that objects are deallocated as soon as they are no longer needed, which helps to avoid problems such as dangling pointers or use-after-free errors. This helps to ensure the correctness and reliability of Python programs.
4440
+
4441
+
Overall, reference counting is a key feature of Python's automatic memory management system, and it plays an important role in ensuring that Python programs are both efficient and reliable.
4442
+
4443
+
4321
4444
#### Q. Do functions return something even if there is not a `return statement?
0 commit comments