https://www.rakeshmgs.in/search/label/Template
https://www.rakeshmgs.in


RakeshMgs

O Level M3-R5 Python Viva Questions and Answer in Hindi

Updated:

1. Docstring in Python?

पाइथन में Docstring एक स्पेशल टाइप का कमेंट होता है जो function, class, model या फिर script के शुरू में लिखा जाता है। Docstring का उपयोग कोड को एक्सप्लेन करने और डॉक्यूमेंटेशन generate करने के लिए होता है। इससे कोड को समझना और मेन्टेन करना आसान हो जाता है।

Docstring ट्रिपल quotes (""") के बिच लिखा जाता है और इसमें फंक्शन या क्लास के बारे में important information, parameters, return values और function का क्या काम है यह सब लिखा जाता है। निचे एक बेसिक docstring का example है:

def greet(name):
    """
    Function to greet a person by name.
    Parameters:
    name (str): The name of the person to greet.
    Returns:
    str: A greeting message.
    """
    return f"Hello, {name}!"

इस example में "Function to greet a person by name" एक docstring है जो function के काम को describe करता है।

2. LEGB Rule in Python?

Python में LEGB रूल का मतलब है की variables और names को ढूंढने के लिए एक स्पेसिफिक आर्डर होता है, LEGB का फुल फॉर्म है:

  • Local: सबसे पहले पाइथन सबसे अन्दर वाले स्कोप (फंक्शन के अंदर) में नाम को ढूंढता है
  • Enclosing: अगर लोकल स्कोप में नाम नहीं मिलता तो पाइथन आउटर स्कोप (एनक्लोसिंग फंक्शन) में नाम को ढूंढता है
  • Globle: अगर एनक्लोसिंग स्कोप में भी नाम नहीं मिलता तो पाइथन module लेवल (ग्लोबल स्कोप) में नाम को ढूंढता है
  • Built-in: अगर नाम ग्लोबल स्कोप में भी नहीं मिलता तो लास्ट में पाइथन बिल्ट-इन namespace (जैसे 'len', 'open', 'min', 'max' etc.) में नाम को ढूंढता है

निचे एक LEGB का example है:

x = "global"

def outer():
    x = "enclosing"
    
    def inner():
        x = "local"
        print(x)
    
    inner()

outer()

इस कोड में, inner() function के अन्दर x को प्रिंट करते टाइम, ये सबसे पहले लोकल स्कोप में x को ढूंढ़ता है और उसको प्रिंट करता है, इसलिए output होगा "local"

अगर inner() function के अन्दर x को define नहीं किया जाता, तो ये enclosing scope (मतलब outer() function) में x को ढूंढ़ता। और अगर वहां भी नहीं मिलता, तो ये global scope में x को ढूंढ़ता

3. Comprehension in Python?

Python me comprehension ek concise tarika hai expressions likhne ka jo lists, dictionaries, aur sets ko create karne ke liye istemal hota hai. Yeh expressions readable aur succinct hote hain. Python me teen prakar ke comprehensions hote hain:

List Comprehension: List comprehension ek compact way hai ek list ko create karne ka based on ek existing iterable (jaise list, tuple, ya string) ke elements par apply kiye gaye conditions aur operations ke saath.
Example:

numbers = [1, 2, 3, 4, 5]
squares = [x**2 for x in numbers if x % 2 == 0]
print(squares)  # Output: [4, 16]

Dictionary Comprehension: Dictionary comprehension ek dictionary ko create karne ka ek tarika hai based on ek existing iterable ke elements aur un par apply kiye gaye conditions aur operations ke saath.
Example:

numbers = [1, 2, 3, 4, 5]
squared_dict = {x: x**2 for x in numbers if x % 2 == 0}
print(squared_dict)  # Output: {2: 4, 4: 16}

Set Comprehension: Set comprehension ek set ko create karne ka tarika hai based on ek existing iterable ke unique elements par apply kiye gaye conditions aur operations ke saath.
Example:

numbers = [1, 2, 3, 4, 5, 2, 3]
squared_set = {x**2 for x in numbers if x % 2 != 0}
print(squared_set)  # Output: {1, 9, 25}

Comprehensions Python me code ko readable aur concise banate hain, aur efficient tarike se data ko process karne me madad karte hain.

4. Difference Between Mutability and immutability?

Python me data types mutability (परिवर्तनशील) ya immutability (अपरिवर्तनशील) me aate hain. Yeh concept yeh batata hai ki ek data type ke values ko hum kaise change kar sakte hain.

  1. Mutable (परिवर्तनशील):
    • Mutable data types woh hote hain jinke values badli ja sakti hain once they are created. Matlab ki aap unme changes kar sakte hain without creating a new object.
    • Examples of mutable data types in Python include lists, dictionaries, sets.
    • Jab aap ek mutable object ko modify karte hain, to wo original object change hota hai.
      For example:
  2. my_list = [1, 2, 3]
    my_list.append(4)  # Original list modified
    
  3. Immutable (अपरिवर्तनशील)
    • Immutable data types woh hote hain jinke values create hone ke baad badla nahi ja sakta hai. Matlab ki aap unme changes nahi kar sakte hain.
    • Examples of immutable data types in Python include integers, floats, strings, tuples, frozensets.
    • Jab aap ek immutable object ko modify karte hain, to ek naya object create hota hai. Original object change nahi hota.
      For example:
    my_string = "Hello"
    new_string = my_string.upper()  # Creates a new string object
    

Yeh concept Python programming me important hai kyun ki isse memory usage aur program performance ka bhi asar hota hai. Mutable objects ki flexibility hoti hai lekin unka use cautious tarike se karna chahiye, jabki immutable objects predictable aur safe hote hain lekin unme changes karne ke liye naye objects create karne padte hain.

5. Memory management in python?

Python me memory management automatic aur dynamic hota hai, jo Python interpreter ke dwara handle kiya jata hai. Yahaan Python ka memory management kaam kaise karta hai uske kuchh mukhya points hain:

Automatic Memory Allocation: Python me memory allocation ka kaam interpreter dwara automatic tarike se hota hai. Jab aap ek object create karte hain, jaise ek variable assign karte hain ya ek function call karte hain, interpreter khud hi memory allocate karta hai us object ke liye.

Reference Counting: Python me har object ke sath ek reference count associated hota hai jo batata hai ki kitne references ya pointers us object ko point kar rahe hain. Jab ek object ka reference count zero ho jata hai (matlab ki koi bhi reference us object ko nahi point kar raha hai), to wo object automatically garbage collection ke process me free ho jata hai, matlab delete ho jata hai.

Garbage Collection: Python me automatic garbage collection process hoti hai jo unused objects ko detect kar ke unhe free karti hai memory se. Isse memory management efficient hoti hai aur memory leaks se bacha jata hai. Python ka primary garbage collection algorithm reference counting ke sath-sath generational garbage collection bhi istemal karta hai (jisme objects ko age ke basis par manage kiya jata hai).

Memory Saving Techniques:

  • Reuse of Immutable Objects: Immutable objects, jaise ki integers aur strings, ko reuse karne ka prayas kiya jata hai. Jaise, small integers (-5 to 256) reuse kiye jate hain.
  • Object Pooling: Small objects aur frequently used objects ko pool me rakha jata hai taaki unhe phir se create karne ki zarurat na ho.

Python ka memory management system programmers ko normally explicit taur par memory ko allocate aur free karne ki zarurat nahi padti, jisse development aur debugging me madad milti hai. Python interpreter khud hi efficient tarike se memory ko handle karta hai jisse ki code execution aur performance optimize rahe.

6. Difference between NumPy and List?

Numpy aur Python ki built-in lists (ya arrays) me kuchh mukhya antar hain:

Memory Management:

  • Lists: Python ki built-in lists flexible hote hain lekin inefficient memory management karte hain. Har element ko individual object ke roop me store karte hain, jo additional memory overhead aur slower performance ka karan ban sakta hai jab bade data sets ka use hota hai.
  • Numpy Arrays: Numpy arrays optimized data storage aur operations ke liye hote hain. Numpy arrays contiguous memory block me store hote hain, jo fast element access aur efficient mathematical operations allow karte hain.

Performance:

  • Lists: Lists me element access linear time complexity (O(n)) hoti hai, matlab ki element ke index tak pahunchne ke liye list me har element ko sequentially traverse karna padta hai.
  • Numpy Arrays: Numpy arrays me element access constant time complexity (O(1)) hoti hai, matlab ki index se directly element tak pahunch sakte hain. Iski wajah se operations jaise slicing, indexing, aur mathematical operations numpy arrays me fast execute ho sakte hain.

Functionality:

  • Lists: Lists general-purpose data structures hote hain jo heterogeneous data ko store karne me madad karte hain. Inme alag-alag data types ke elements store kar sakte hain.
  • Numpy Arrays: Numpy arrays homogeneous data structures hote hain, matlab ki ek hi type ke elements ko store karte hain. Isse vectorized operations possible hote hain jaise element-wise operations, linear algebra, statistical operations, etc.

Ease of Use:

  • Lists: Lists simple syntax aur flexible nature ke karan beginners ke liye easy hote hain, lekin large datasets ke liye performance limitations ho sakte hain.
  • Numpy Arrays: Numpy arrays specialized operations aur optimized performance ke sath aate hain, lekin inka use specific numerical computing tasks me jyada suitable hota hai.

In summary, numpy arrays Python ki built-in lists se performance aur functionality me superior hote hain, especially numerical computing tasks jaise ki data analysis, machine learning, aur scientific computing ke liye. Agar aapko numerical data ko efficient tarike se handle karna hai, to numpy arrays ka use karna beneficial ho sakta hai.

Numpy ek powerful numerical computing library hai Python me, jiska use data analysis, scientific computing, aur machine learning me kiya jata hai. Numpy ke fayde aur nuksan is prakar hain:

Advantages of Numpy:

  • Fast Array Operations: Numpy arrays optimized hote hain efficient element-wise operations aur mathematical computations ke liye. Iski wajah se large datasets ke saath kaam karte samay performance bahut acchi hoti hai.
  • Memory Efficiency: Numpy arrays contiguous memory block me store hote hain, jo memory management ko efficient banata hai aur memory usage ko kam karta hai compared to Python lists.
  • Broadcasting: Numpy supports broadcasting, jisse ek array ka shape dusre array ke shape ke saath match kiya ja sakta hai operations ke liye, jisse code likhne aur execute karna easy aur efficient ho jata hai.
  • Vectorized Operations: Numpy arrays me vectorized operations use karne se loops ki zarurat nahi hoti hai, jo code ko concise aur fast banata hai.
  • Comprehensive Functionality: Numpy ke saath aane wale functions aur methods mathematical computations, linear algebra, random number generation, Fourier transforms, aur bahut se scientific operations ko support karte hain.
  • Interoperability: Numpy arrays easily interchange ho sakte hain with other libraries jo numerical data processing me use hoti hain jaise ki Pandas, SciPy, Matplotlib, etc.

Disadvantages of Numpy:

  • Complexity for Beginners: Numpy ke advanced features aur syntax beginners ke liye initially complex ho sakte hain compared to Python built-in data structures jaise ki lists.
  • Limited Data Types: Numpy arrays me data types predefined hote hain aur immutable hote hain, jisse flexibility me thoda limitation ho sakta hai compared to Python lists.
  • Memory Management Responsibility: Numpy ka use efficient memory management ke liye hota hai, lekin yeh manual memory management ka kaam karta hai jisse code complexity aur debugging me thoda challenge ho sakta hai.

Overall, Numpy ek powerful tool hai jo numerical data ko handle karne aur optimized performance achieve karne me madad karta hai. Agar aapko numerical computing tasks jaise ki data analysis, scientific computing, aur machine learning me kaam karna hai, to Numpy ka istemal beneficial ho sakta hai.


आपको आर्टिकल कैसा लगा? अपनी राय अवश्य दें
Please don't Add spam links,
if you want backlinks from my blog contact me on rakeshmgs.in@gmail.com