મુખ્ય સામગ્રી પર જાઓ
OOPS & Python Programming (4351108) - Summer 2024 Solution - Gujarati
  1. સંસાધનો/
  2. અભ્યાસ સામગ્રી/
  3. ઇલેક્ટ્રોનિક્સ અને કમ્યુનિકેશન એન્જિનિયરિંગ/
  4. ઇસીઇ સેમેસ્ટર 5/
  5. OOPS અને પાયથન પ્રોગ્રામિંગ (4351108)/

OOPS & Python Programming (4351108) - Summer 2024 Solution - Gujarati

મિલવ ડબગર
લેખક
મિલવ ડબગર
ઇલેક્ટ્રિકલ અને ઇલેક્ટ્રોનિક મેન્યુફેક્ચરિંગ ઉદ્યોગમાં અનુભવી લેક્ચરર. એમ્બેડેડ સિસ્ટમ્સ, ઈમેજ પ્રોસેસિંગ, ડેટા સાયન્સ, મેટલેબ, પાયથન, STM32માં કુશળ. એલ.ડી. કોલેજ ઓફ એન્જિનિયરિંગ - અમદાવાદથી કમ્યુનિકેશન સિસ્ટમ્સ એન્જિનિયરિંગમાં માસ્ટર્સ ડિગ્રી ધરાવતા મજબૂત શિક્ષણ વ્યાવસાયિક.
અનુક્રમણિકા

પ્રશ્ન 1(અ) [3 ગુણ]
#

Python માં for loop નું કાર્ય સમજાવો.

જવાબ:

For loop એ list, tuple અથવા string જેવા sequence ના દરેક item માટે code block ને repeat કરે છે.

સિન્ટેક્સ ટેબલ:

ઘટકSyntaxઉદાહરણ
મૂળભૂતfor variable in sequence:for i in [1,2,3]:
Rangefor i in range(n):for i in range(5):
Stringfor char in string:for c in "hello":

આકૃતિ:

StartEMIBxotaeveccemkusYttetCeolsoheelnfEcoetxkox?eptciufbitoteide-tymNleoom-os-p-l-beC>ofhdteEycnikdnisfeqiuteenmcseleft
  • પુનરાવર્તન: Loop variable ને sequence માંથી દરેક value એક પછી એક મળે છે
  • આપમેળે: Python આપમેળે next item પર જવાનું handle કરે છે
  • લવચીક: Lists, strings, tuples, ranges સાથે કામ કરે છે

યાદી કૌશલ્ય: “દરેક Item માટે, Block Execute કરો”


પ્રશ્ન 1(બ) [4 ગુણ]
#

Python માં if-elif-else નું કાર્ય સમજાવો.

જવાબ:

બહુ-માર્ગીય નિર્ણય માળખું જે sequence માં અનેક conditions ને ચકાસે છે.

માળખાકીય ટેબલ:

StatementહેતુSyntax
ifપ્રથમ શરતif condition1:
elifવૈકલ્પિક શરતોelif condition2:
elseમૂળભૂત કેસelse:

પ્રવાહ આકૃતિ:

EiCxfheeTcbEScrulntkutodaeecri/kEebtfxlleioEcFCcfcnoahcTukdnleortdscnueiekdeti/ietoliCoEenioFhrxlfnaeesElcecenskludestbneeleoxctkelif
  • ક્રમબદ્ધ: ઉપરથી નીચે conditions ને ચકાસે છે
  • વિશિષ્ટ: માત્ર એક જ block execute થાય છે
  • વૈકલ્પિક: elif અને else વૈકલ્પિક છે

યાદી કૌશલ્ય: “જો આ, અથવા જો તે, અથવા Default”


પ્રશ્ન 1(ક) [7 ગુણ]
#

Python પ્રોગ્રામનું માળખું સમજાવો.

જવાબ:

Python પ્રોગ્રામમાં તાર્કિક ક્રમમાં વિશિષ્ટ ઘટકો સાથે વ્યવસ્થિત માળખું હોય છે.

પ્રોગ્રામ માળખું ટેબલ:

ઘટકહેતુઉદાહરણ
Commentsદસ્તાવેજીકરણ# This is comment
Importબાહ્ય modulesimport math
Constantsનિશ્ચિત વેલ્યુઝPI = 3.14
Functionsપુનઃઉપયોગી કોડdef function_name():
ClassesObjects નો blueprintclass ClassName:
Main codeપ્રોગ્રામ executionif __name__ == "__main__":

પ્રોગ્રામ આર્કિટેક્ચર:

#iFDCDMImCueleaCDmpoVnfafiEooponacisinxmcorsrtnsnemurttiiiiPcemtaaottrunemnbniiottnSotloogistedsennroacusssanttl&miieoosnn
  • મોડ્યુલર: દરેક વિભાગનો વિશિષ્ટ હેતુ હોય છે
  • વાંચવા યોગ્ય: સ્પષ્ટ સંગઠન સમજવામાં મદદ કરે છે
  • જાળવણી યોગ્ય: ફેરફાર અને debug કરવું સરળ
  • માનક: Python conventions ને અનુસરે છે

સરળ ઉદાહરણ:

# Program to calculate area
import math

PI = 3.14159

def calculate_area(radius):
    return PI * radius * radius

# Main execution
radius = float(input("Enter radius: "))
area = calculate_area(radius)
print(f"Area = {area}")

યાદી કૌશલ્ય: “Comment, Import, Constant, Function, Class, Main”


પ્રશ્ન 1(ક OR) [7 ગુણ]
#

Python પ્રોગ્રામિંગ લેંગવેજની વિશેષતાઓ સમજાવો.

જવાબ:

Python ની અનન્ય લાક્ષણિકતાઓ છે જે તેને beginners અને professionals માટે લોકપ્રિય બનાવે છે.

Python વિશેષતાઓ ટેબલ:

વિશેષતાવર્ણનલાભ
સરળસરળ syntaxઝડપી શીખવા
Interpretedકોઈ compilation નહીંઝડપી development
Object-OrientedClasses અને objectsકોડની પુનઃઉપયોગીતા
Open Sourceઉપયોગ માટે મફતકોઈ licensing ખર્ચ નહીં
Cross-Platformદરેક જગ્યાએ run થાય છેઉચ્ચ portability

વિશેષતા કેટેગરીઝ:

LF---aenaSRDgtieyuumanarpdagelamesebilcPeytTF---heeocaIPEnhtnoxnutrtFireteeceranaaspbstlrliueebrtleCFeesoedmamt---uunrOLAiepactsertyngievSeoLuiSrbucrpeaproyrt
  • શિખાઉ-મિત્ર: અંગ્રેજી ભાષા જેવું સરળ syntax
  • બહુમુખી: web, AI, data science, automation માટે ઉપયોગ
  • સમૃદ્ધ લાયબ્રેરીઝ: પ્રી-બિલ્ટ modules નો વિશાળ સંગ્રહ
  • ડાયનેમિક ટાઇપિંગ: variable types declare કરવાની જરૂર નથી
  • ઇન્ટરેક્ટિવ: interpreter માં લાઇન બાય લાઇન કોડ ટેસ્ટ કરી શકાય
  • હાઇ-લેવલ: memory management આપમેળે handle કરે છે

કોડ ઉદાહરણ:

# Simple Python syntax
name = "Python"
print(f"Hello, {name}!")

યાદી કૌશલ્ય: “સરળ, Interpreted, Object-Oriented, Open, Cross-platform”


પ્રશ્ન 2(અ) [3 ગુણ]
#

સ્ટ્રિંગ પર થતાં કોઈ 3 ઓપરેશન સમજાવો.

જવાબ:

String operations વિવિધ રીતે text data ને manipulate અને process કરે છે.

સ્ટ્રિંગ ઓપરેશન્સ ટેબલ:

ઓપરેશનMethodઉદાહરણપરિણામ
જોડવું+"Hello" + "World""HelloWorld"
લંબાઈlen()len("Python")6
મોટા અક્ષર.upper()"hello".upper()"HELLO"

ઓપરેશન ઉદાહરણો:

text = "Python"
# 1. જોડવું
result1 = text + " Programming"
# 2. લંબાઈ શોધવી
result2 = len(text)
# 3. મોટા અક્ષરમાં કન્વર્ટ કરવું
result3 = text.upper()
  • જોડવું: બે અથવા વધુ strings ને સાથે જોડે છે
  • લંબાઈ: string માં કુલ characters ગણે છે
  • કેસ કન્વર્ઝન: અક્ષરોના કેસ બદલે છે (upper/lower)

યાદી કૌશલ્ય: “જોડો, ગણો, કન્વર્ટ કરો”


પ્રશ્ન 2(બ) [4 ગુણ]
#

તાપમાનને ફેરનહાઇટથી સેલ્સિયસ એકમમાં (C=(F-32)/1.8 સમીકરણથી) પરિવર્તિત કરવા માટેનો Python પ્રોગ્રામ વિકસાવો.

જવાબ:

પ્રોગ્રામ user input સાથે ગાણિતિક formula વાપરીને temperature convert કરે છે.

એલ્ગોરિધમ ટેબલ:

પગલુંક્રિયાકોડ
1Input લોfahrenheit = float(input())
2Formula લાગુ કરોcelsius = (fahrenheit - 32) / 1.8
3પરિણામ દર્શાવોprint(f"Celsius: {celsius}")

સંપૂર્ણ પ્રોગ્રામ:

# Temperature conversion program
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) / 1.8
print(f"Temperature in Celsius: {celsius:.2f}")

ટેસ્ટ કેસેસ:

  • Input: 32°F → Output: 0.00°C

  • Input: 100°F → Output: 37.78°C

  • યુઝર ઇનપુટ: યુઝર પાસેથી Fahrenheit temperature લે છે

  • ફોર્મ્યુલા એપ્લિકેશન: આપેલ conversion equation વાપરે છે

  • ફોર્મેટેડ આઉટપુટ: decimal places સાથે પરિણામ બતાવે છે

યાદી કૌશલ્ય: “Input, Calculate, Output”


પ્રશ્ન 2(ક) [7 ગુણ]
#

Python માં list ડેટા ટાઇપ વિસ્તૃત રીતે સમજાવો.

જવાબ:

List એ ordered, mutable collection છે જે single variable માં multiple items store કરે છે.

લિસ્ટ લાક્ષણિકતાઓ ટેબલ:

પ્રોપર્ટીવર્ણનઉદાહરણ
ક્રમબદ્ધItems નો position હોય છે[1, 2, 3]
પરિવર્તનશીલબદલાઈ શકાય છેlist[0] = 10
ઇન્ડેક્સ્ડPosition દ્વારા accesslist[0]
મિશ્ર પ્રકારોવિવિધ data types[1, "hello", 3.14]

લિસ્ટ ઓપરેશન્સ આકૃતિ:

LIOinpsdeterl:xaAi:tcs"[ict110oe[00ns0",ss]:210,320,43l[0i5]Ms0ot,d[i02f]0y=,5030,40]

સામાન્ય લિસ્ટ મેથડ્સ:

Methodહેતુઉદાહરણ
append()અંતે item ઉમેરોlist.append(5)
insert()position પર ઉમેરોlist.insert(1, 15)
remove()item ડિલીટ કરોlist.remove(20)
pop()છેલ્લું item દૂર કરોlist.pop()
len()લંબાઈ મેળવોlen(list)

ઉદાહરણ કોડ:

# Creating and using lists
numbers = [1, 2, 3, 4, 5]
numbers.append(6)        # અંતે 6 ઉમેરો
numbers.insert(0, 0)     # શરૂઆતમાં 0 ઉમેરો
print(numbers[2])        # 3જું element access કરો
numbers.remove(3)        # value 3 દૂર કરો
  • ડાયનેમિક સાઇઝ: execution દરમ્યાન વધી અથવા ઘટી શકે છે
  • ઝીરો ઇન્ડેક્સિંગ: પ્રથમ element index 0 પર
  • સ્લાઇસિંગ: [start:end] વાપરીને ભાગો extract કરી શકાય
  • નેસ્ટેડ લિસ્ટ્સ: અન્ય lists સમાવી શકે છે

યાદી કૌશલ્ય: “ક્રમબદ્ધ, પરિવર્તનશીલ, ઇન્ડેક્સ્ડ, મિશ્ર”


પ્રશ્ન 2(અ OR) [3 ગુણ]
#

Python માં સ્ટ્રિંગ ફોર્મેટિંગ સમજાવો.

જવાબ:

String formatting એ templates માં values insert કરીને formatted strings બનાવે છે.

ફોર્મેટિંગ મેથડ્સ ટેબલ:

MethodSyntaxઉદાહરણ
f-stringsf"text {variable}"f"Hello {name}"
format()"text {}".format(value)"Age: {}".format(25)
% operator"text %s" % value"Name: %s" % "John"

ઉપયોગ ઉદાહરણ:

name = "Alice"
age = 25
# f-string formatting
message = f"Hello {name}, you are {age} years old"
  • પ્લેસહોલ્ડર: {} માર્ક કરે છે કે values ક્યાં જાય છે
  • ડાયનેમિક: Runtime પર values insert થાય છે
  • વાંચવા યોગ્ય: Concatenation કરતાં કોડ સાફ બનાવે છે

યાદી કૌશલ્ય: “Format, Insert, Display”


પ્રશ્ન 2(બ OR) [4 ગુણ]
#

સ્કેન કરેલ નંબર એકી સંખ્યા છે કે બેકી સંખ્યા છે તે ઓળખી અને યોગ્ય મેસેજ પ્રિન્ટ કરતો Python પ્રોગ્રામ વિકસાવો.

જવાબ:

પ્રોગ્રામ number 2 થી divisible છે કે નહીં તે ચકાસીને even અથવા odd નક્કી કરે છે.

લૉજિક ટેબલ:

શરતપરિણામમેસેજ
number % 2 == 0Even“Number is even”
number % 2 != 0Odd“Number is odd”

સંપૂર્ણ પ્રોગ્રામ:

# Even/Odd checker program
number = int(input("Enter a number: "))
if number % 2 == 0:
    print(f"{number} is even")
else:
    print(f"{number} is odd")

ટેસ્ટ કેસેસ:

  • Input: 4 → Output: “4 is even”

  • Input: 7 → Output: “7 is odd”

  • મોડ્યુલો ઓપરેટર: % division પછીનો remainder આપે છે

  • કંડિશનલ લૉજિક: if-else પરિણામ નક્કી કરે છે

  • યુઝર ફીડબેક: પરિણામ વિશે સ્પષ્ટ મેસેજ

યાદી કૌશલ્ય: “Input, Check Remainder, Display Result”


પ્રશ્ન 2(ક OR) [7 ગુણ]
#

Python માં Set ડેટા ટાઇપ વિસ્તૃત રીતે સમજાવો.

જવાબ:

Set એ unordered collection છે જેમાં unique items હોય છે અને duplicate values નહીં.

સેટ લાક્ષણિકતાઓ ટેબલ:

પ્રોપર્ટીવર્ણનઉદાહરણ
અક્રમકોઈ નિશ્ચિત position નથી{1, 3, 2}
અનન્યકોઈ duplicates નથી{1, 2, 3}
પરિવર્તનશીલફેરફાર કરી શકાયset.add(4)
પુનરાવર્તન યોગ્યLoop કરી શકાયfor item in set:

સેટ ઓપરેશન્સ આકૃતિ:

SetUIDSnniyAitfm:oefmSnree{e:srt1teer,{cniO1tcc2p,ie,eo:Dr2ni3a,:{f}t1fi3{,:o,3n}2{s4}1S,,e2t5,}4B,:5b}{3,4,5}

સેટ મેથડ્સ ટેબલ:

Methodહેતુઉદાહરણ
add()single item ઉમેરોset.add(6)
update()multiple items ઉમેરોset.update([7, 8])
remove()item ડિલીટ કરોset.remove(3)
union()sets જોડોset1.union(set2)
intersection()સામાન્ય itemsset1.intersection(set2)

ઉદાહરણ કોડ:

# Creating and using sets
fruits = {"apple", "banana", "orange"}
fruits.add("mango")              # single item ઉમેરો
fruits.update(["grape", "kiwi"]) # multiple ઉમેરો
fruits.remove("banana")          # item દૂર કરો
print(len(fruits))               # items ગણો
  • આપમેળે ડુપ્લિકેશન હટાવવું: Duplicate values આપમેળે દૂર કરે છે
  • ઝડપી મેમ્બરશિપ: Item exists છે કે નહીં ઝડપથી ચકાસી શકાય
  • ગાણિતિક ઓપરેશન્સ: Union, intersection, difference
  • કોઈ ઇન્ડેક્સિંગ નહીં: Position દ્વારા items access કરી શકાતાં નથી

યાદી કૌશલ્ય: “અનન્ય, અક્રમ, પરિવર્તનશીલ, ગાણિતિક”


પ્રશ્ન 3(અ) [3 ગુણ]
#

math મૉડ્યુલની કોઈ પણ 3 મેથડ સમજાવો.

જવાબ:

Math module જટિલ ગણતરીઓ માટે ગાણિતિક functions પ્રદાન કરે છે.

મેથ મેથડ્સ ટેબલ:

Methodહેતુઉદાહરણપરિણામ
math.sqrt()વર્ગમૂળmath.sqrt(16)4.0
math.pow()પાવર ગણતરીmath.pow(2, 3)8.0
math.ceil()ઉપર રાઉન્ડmath.ceil(4.3)5

ઉપયોગ ઉદાહરણ:

import math
number = 16
result1 = math.sqrt(number)  # વર્ગમૂળ
result2 = math.pow(2, 4)     # 2 ની પાવર 4
result3 = math.ceil(7.2)     # 8 સુધી રાઉન્ડ અપ
  • ચોકસાઈ: Basic operators કરતાં વધુ accurate
  • ઇમ્પોર્ટ જરૂરી: પહેલા math module import કરવું પડે
  • રિટર્ન વેલ્યુઝ: સામાન્ય રીતે float numbers return કરે છે

યાદી કૌશલ્ય: “વર્ગમૂળ, પાવર, સીલિંગ”


પ્રશ્ન 3(બ) [4 ગુણ]
#

for loop નો ઉપયોગ કરીને લિસ્ટમાં આવેલ તમામ ઘટકોનો સરવાળો શોધવા માટેનો Python પ્રોગ્રામ વિકસાવો.

જવાબ:

પ્રોગ્રામ list દ્વારા iterate કરે છે અને બધા elements નો sum accumulate કરે છે.

એલ્ગોરિધમ ટેબલ:

પગલુંક્રિયાકોડ
1Sum initialize કરોtotal = 0
2List માં loop કરોfor element in list:
3Sum માં ઉમેરોtotal += element
4પરિણામ દર્શાવોprint(total)

સંપૂર્ણ પ્રોગ્રામ:

# Sum of list elements
numbers = [10, 20, 30, 40, 50]
total = 0
for element in numbers:
    total += element
print(f"Sum of all elements: {total}")

ટેસ્ટ કેસ:

  • Input: [1, 2, 3, 4, 5] → Output: 15

  • એક્યુમ્યુલેટર: Variable running total store કરે છે

  • પુનરાવર્તન: Loop દરેક element ની એકવાર મુલાકાત લે છે

  • ઉમેરો: દરેક element ને running sum માં ઉમેરે છે

યાદી કૌશલ્ય: “Initialize, Loop, Add, Display”


પ્રશ્ન 3(ક) [7 ગુણ]
#

બે list ની લંબાઈ સમાન છે કે નહીં તે ચકાસવા, અને જો હોય તો તેમને ભેગા કરીને તેમાંથી એક dictionary બનાવવાનો Python પ્રોગ્રામ વિકસાવો.

જવાબ:

પ્રોગ્રામ list lengths ની સરખામણી કરે છે અને જો તે match કરે તો dictionary બનાવે છે.

લૉજિક ફ્લો ટેબલ:

પગલુંશરતક્રિયા
1લંબાઈ ચકાસોlen(list1) == len(list2)
2જો સમાનMerge અને dictionary બનાવો
3જો અસમાનError message દર્શાવો

પ્રક્રિયા આકૃતિ:

LC{ilracsee::tna131(Yt,}:Leeisb[sD:ati2,1c,)tb,=N=oEMcre]lrseosnra(gLeiLsits2t)2:?[1,2,3]

સંપૂર્ણ પ્રોગ્રામ:

# Merge lists into dictionary
list1 = ['name', 'age', 'city']
list2 = ['John', 25, 'Mumbai']

if len(list1) == len(list2):
    # Create dictionary using zip
    result_dict = dict(zip(list1, list2))
    print("Dictionary created:", result_dict)
else:
    print("Lists have different lengths, cannot merge")

અપેક્ષિત આઉટપુટ:

Dictionary created: {'name': 'John', 'age': 25, 'city': 'Mumbai'}
  • લંબાઈ સરખામણી: લિસ્ટ્સ યોગ્ય રીતે pair કરી શકાય છે તેની ખાતરી કરે છે
  • zip() ફંક્શન: બંને lists ના elements ને pair કરે છે
  • dict() કન્સ્ટ્રક્ટર: Paired elements માંથી dictionary બનાવે છે
  • એરર હેન્ડલિંગ: ખોટા pairing ને અટકાવે છે

વૈકલ્પિક મેથડ:

# Manual dictionary creation
result_dict = {}
for i in range(len(list1)):
    result_dict[list1[i]] = list2[i]

યાદી કૌશલ્ય: “લંબાઈ ચકાસો, Zip કરો, Dictionary બનાવો”


પ્રશ્ન 3(અ OR) [3 ગુણ]
#

statistics મૉડ્યુલની કોઈ પણ 3 મેથડ સમજાવો.

જવાબ:

Statistics module numeric data પર statistical calculations માટે functions પ્રદાન કરે છે.

સ્ટેટિસ્ટિક્સ મેથડ્સ ટેબલ:

Methodહેતુઉદાહરણપરિણામ
statistics.mean()સરેરાશ valuemean([1,2,3,4,5])3.0
statistics.median()મધ્ય valuemedian([1,2,3,4,5])3
statistics.mode()સૌથી વધુ વારંવારmode([1,1,2,3])1

ઉપયોગ ઉદાહરણ:

import statistics
data = [10, 20, 30, 40, 50]
avg = statistics.mean(data)      # સરેરાશ કેલ્ક્યુલેટ કરો
mid = statistics.median(data)    # મધ્ય value શોધો
  • ડેટા એનાલિસિસ: ડેટા patterns સમજવામાં મદદ કરે છે
  • બિલ્ટ-ઇન ફંક્શન્સ: જટિલ formulas લખવાની જરૂર નથી
  • ચોક્કસ પરિણામો: Edge cases ને યોગ્ય રીતે handle કરે છે

યાદી કૌશલ્ય: “Mean, Median, Mode”


પ્રશ્ન 3(ક OR) [7 ગુણ]
#

આપેલ સ્ટ્રિંગમાં કોઈ અક્ષર કેટલી વાર આવે છે તે ગણવા માટેની dictionary બનાવવાનો Python પ્રોગ્રામ વિકસાવો.

જવાબ:

પ્રોગ્રામ dictionary બનાવે છે જ્યાં keys અક્ષરો છે અને values તેમની counts છે.

અક્ષર ગણતરી એલ્ગોરિધમ:

પગલુંક્રિયાકોડ
1Dictionary initialize કરોchar_count = {}
2String માં loop કરોfor char in string:
3Occurrences ગણોchar_count[char] = char_count.get(char, 0) + 1
4પરિણામો દર્શાવોprint(char_count)

ગણતરી પ્રક્રિયા:

SLDtoirociptnigtho:hnra"orhuyeg:lhle{oe'"ahc'h:l1c,ha'real'c:t1e,r'l':2,':1}

સંપૂર્ણ પ્રોગ્રામ:

# Character frequency counter
text = input("Enter a string: ")
char_count = {}

for char in text:
    if char in char_count:
        char_count[char] += 1
    else:
        char_count[char] = 1

print("Character frequencies:")
for char, count in char_count.items():
    print(f"'{char}': {count}")

વૈકલ્પિક મેથડ (વધુ Pythonic):

# Using get() method
text = "programming"
char_count = {}

for char in text:
    char_count[char] = char_count.get(char, 0) + 1

print(char_count)

ઉદાહરણ આઉટપુટ:

Input: "hello"
Output: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
  • ડિક્શનરી કીઝ: દરેક અનન્ય અક્ષર key બને છે
  • ડિક્શનરી વેલ્યુઝ: અક્ષરની occurrences ની count
  • get() મેથડ: Key exist નથી તો 0 return કરે છે, errors ટાળે છે
  • પુનરાવર્તન: String ના દરેક અક્ષરને એકવાર process કરે છે

યાદી કૌશલ્ય: “Loop, Check, Count, Store”


પ્રશ્ન 4(અ) [3 ગુણ]
#

Python ક્લાસ અને ઓબ્જેક્ટ્સનું કાર્ય ઉદાહરણ સાથે સમજાવો.

જવાબ:

Class એ objects બનાવવા માટેનો blueprint છે. Objects એ classes ના instances છે.

ક્લાસ-ઓબ્જેક્ટ સંબંધ:

કન્સેપ્ટહેતુઉદાહરણ
ClassTemplate/Blueprintclass Car:
ObjectClass નો instancemy_car = Car()
AttributesClass માં ડેટાself.color = "red"
MethodsClass માં functionsdef start(self):

ક્લાસ માળખું:

ObjeA--M--cCtetltcmtss:aroohttsildoaomsboedrpy:urlst(_t:()cCe)iaasnrr:he=riCtasr()

ઉદાહરણ કોડ:

class Student:
    def __init__(self, name, age):
        self.name = name  # Attribute
        self.age = age    # Attribute
    
    def display(self):    # Method
        print(f"Name: {self.name}, Age: {self.age}")

# Creating objects
student1 = Student("Alice", 20)
student1.display()
  • એન્કેપ્સુલેશન: સંબંધિત data અને functions ને સાથે group કરે છે
  • પુનઃઉપયોગીતા: એક class અનેક objects બનાવી શકે છે
  • સંગઠન: બહેતર code structure અને maintenance

યાદી કૌશલ્ય: “ક્લાસ Blueprint, ઓબ્જેક્ટ Instance”


પ્રશ્ન 4(બ) [4 ગુણ]
#

લિસ્ટમાં આવેલી તમામ એકી સંખ્યાઓ પ્રિન્ટ કરવા માટેનો Python પ્રોગ્રામ વિકસાવો.

જવાબ:

પ્રોગ્રામ list elements ને filter કરે છે અને માત્ર odd numbers દર્શાવે છે.

એકી સંખ્યા ચકાસણી ટેબલ:

સંખ્યાnumber % 2પરિણામ
11એકી
20બેકી
31એકી
40બેકી

સંપૂર્ણ પ્રોગ્રામ:

# Print odd numbers from list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print("Odd numbers in the list:")
for number in numbers:
    if number % 2 != 0:
        print(number, end=" ")

વૈકલ્પિક મેથડ્સ:

# Method 2: List comprehension
odd_numbers = [num for num in numbers if num % 2 != 0]
print(odd_numbers)

# Method 3: Using filter
odd_numbers = list(filter(lambda x: x % 2 != 0, numbers))
print(odd_numbers)

અપેક્ષિત આઉટપુટ:

Odd numbers in the list:
1 3 5 7 9
  • મોડ્યુલો ઓપરેશન: % operator remainder શોધે છે
  • કંડિશન ચેક: જો remainder 0 નથી તો number odd છે
  • લૂપ પુનરાવર્તન: List ની દરેક number ચકાસે છે

યાદી કૌશલ્ય: “Loop, Check Remainder, Print Odd”


પ્રશ્ન 4(ક) [7 ગુણ]
#

Python માં યુઝર ડિફાઇન્ડ ફંક્શન્સનું કાર્ય સમજાવો.

જવાબ:

User-defined functions એ programmers દ્વારા બનાવેલા custom functions છે જે વિશિષ્ટ કાર્યો કરે છે.

ફંક્શન ઘટકો ટેબલ:

ઘટકહેતુSyntax
def કીવર્ડFunction declarationdef function_name():
ParametersInput valuesdef func(param1, param2):
BodyFunction codeIndented statements
returnOutput valuereturn value

ફંક્શન માળખું:

dFreuefntcLPCuftoraruicolnnoacccnleurtKsleieBvsasoyoaitunwdrnil_oyigotnranad(blsmilooetnegp(Fodsitpueciandnorcetnatfeamiidleon))tneerIifsndu)pen:unctttiivfoainleures

ફંક્શન પ્રકારો:

પ્રકારવર્ણનઉદાહરણ
કોઈ parameters નહીંકોઈ input લેતું નથીdef greet():
Parameters સાથેInput લે છેdef add(a, b):
Return valueOutput આપે છેreturn a + b
કોઈ return નહીંAction કરે છેprint("Hello")

ઉદાહરણ ફંક્શન્સ:

# Function with no parameters
def greet():
    print("Hello, World!")

# Function with parameters and return value
def calculate_area(length, width):
    area = length * width
    return area

# Function with default parameters
def introduce(name, age=18):
    print(f"My name is {name} and I am {age} years old")

# Using functions
greet()
result = calculate_area(5, 3)
print(f"Area: {result}")
introduce("Alice", 25)
introduce("Bob")  # Uses default age

ફંક્શન લાભો:

  • પુનઃઉપયોગીતા: એકવાર લખો, અનેકવાર વાપરો
  • મોડ્યુલરિટી: જટિલ સમસ્યાઓને નાના ભાગોમાં તોડો
  • જાળવણીયોગ્યતા: Update અને debug કરવું સરળ
  • વાંચવાયોગ્યતા: કોડને વધુ organized અને સમજવાયોગ્ય બનાવે છે
  • ટેસ્ટિંગ: અલગ અલગ functions ને અલગથી test કરી શકાય

વેરિયેબલ સ્કોપ:

  • લોકલ વેરિયેબલ્સ: માત્ર function અંદર exist કરે છે
  • ગ્લોબલ વેરિયેબલ્સ: આખા program માં accessible
  • પેરામીટર્સ: Local variables તરીકે કામ કરે છે

યાદી કૌશલ્ય: “Define, Parameters, Body, Return”


પ્રશ્ન 4(અ OR) [3 ગુણ]
#

Python માં કન્સ્ટ્રક્ટરનું કાર્ય સમજાવો.

જવાબ:

Constructor એ special method છે જે objects બનાવવામાં આવે ત્યારે તેમને initialize કરે છે.

કન્સ્ટ્રક્ટર વિગતો ટેબલ:

પાસુંવર્ણનSyntax
Method nameહંમેશા __init__def __init__(self):
હેતુObject initialize કરવુંInitial values set કરવા
આપમેળે કૉલObject creation દરમ્યાન કૉલ થાયobj = Class()
ParametersArguments લઈ શકે છેdef __init__(self, param):

કન્સ્ટ્રક્ટર ઉદાહરણ:

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print("Student object created")

# Object creation automatically calls constructor
student1 = Student("Alice", 20)
  • આપમેળે એક્ઝિક્યુશન: Object બનાવાતી વખતે તરત જ run થાય છે
  • ઇનિશિયલાઇઝેશન: Object ની શરૂઆતી state set કરે છે
  • self પેરામીટર: હાલનો object જે બનાવાઈ રહ્યો છે તેનો reference

યાદી કૌશલ્ય: “Initialize, Automatic, Self”


પ્રશ્ન 4(બ OR) [4 ગુણ]
#

min ફંક્શનનો ઉપયોગ કર્યા વિના લિસ્ટમાંથી સૌથી નાનો નંબર શોધવા માટેનો Python પ્રોગ્રામ વિકસાવો.

જવાબ:

પ્રોગ્રામ manually બધા elements ની સરખામણી કરીને સૌથી નાની value શોધે છે.

મિનિમમ શોધવાનો એલ્ગોરિધમ:

પગલુંક્રિયાકોડ
1પહેલું smallest માનોsmallest = list[0]
2બીજાઓ સાથે સરખાવોfor num in list[1:]:
3નાનું મળે તો અપડેટ કરોif num < smallest:
4પરિણામ દર્શાવોprint(smallest)

સંપૂર્ણ પ્રોગ્રામ:

# Find smallest number without min()
numbers = [45, 23, 67, 12, 89, 5, 34]

smallest = numbers[0]  # પ્રથમને smallest માનો

for i in range(1, len(numbers)):
    if numbers[i] < smallest:
        smallest = numbers[i]

print(f"Smallest number: {smallest}")

વૈકલ્પિક મેથડ:

# Using for loop with list elements
numbers = [45, 23, 67, 12, 89, 5, 34]
smallest = numbers[0]

for num in numbers[1:]:
    if num < smallest:
        smallest = num

print(f"Smallest number: {smallest}")

અપેક્ષિત આઉટપુટ:

Smallest number: 5
  • કમ્પેરિઝન લૉજિક: દરેક element ને current smallest સાથે compare કરો
  • અપડેટ સ્ટ્રેટેજી: નાનો number મળે ત્યારે smallest replace કરો
  • લિનિયર સર્ચ: બધા elements ને એકવાર ચકાસો

યાદી કૌશલ્ય: “માનો, સરખાવો, અપડેટ કરો, દર્શાવો”


પ્રશ્ન 4(ક OR) [7 ગુણ]
#

Python માં યુઝર ડિફાઇન્ડ મોડ્યુલ્સનું કાર્ય સમજાવો.

જવાબ:

User-defined modules એ custom Python files છે જેમાં functions, classes અને variables હોય છે જે અન્ય programs માં import અને use કરી શકાય છે.

મોડ્યુલ ઘટકો ટેબલ:

ઘટકહેતુઉદાહરણ
Functionsપુનઃઉપયોગી કોડ blocksdef calculate_area():
ClassesObject blueprintsclass Shape:
Variablesસ્પેર્ડ ડેટાPI = 3.14159
Constantsનિશ્ચિત વેલ્યુઝMAX_SIZE = 100

મોડ્યુલ બનાવવાની પ્રક્રિયા:

SSSSSttttteeeeeppppp12345:::::.FFMpuioynldceuftliispeloarenvofsegu/rncaclmtasisosnessimport

ઉદાહરણ મોડ્યુલ બનાવવું:

ફાઇલ: math_operations.py

# User-defined module
PI = 3.14159

def calculate_circle_area(radius):
    return PI * radius * radius

def calculate_rectangle_area(length, width):
    return length * width

class Calculator:
    def add(self, a, b):
        return a + b
    
    def subtract(self, a, b):
        return a - b

મોડ્યુલ વાપરવું:

ઇમ્પોર્ટ મેથડ્સ ટેબલ:

મેથડSyntaxઉપયોગ
સંપૂર્ણ module importimport math_operationsmath_operations.calculate_circle_area(5)
વિશિષ્ટ function importfrom math_operations import calculate_circle_areacalculate_circle_area(5)
Alias સાથે importimport math_operations as math_opsmath_ops.PI
બધું importfrom math_operations import *calculate_circle_area(5)

મુખ્ય પ્રોગ્રામ:

# main.py - Module વાપરવું
import math_operations

# Module functions વાપરવા
radius = 5
area = math_operations.calculate_circle_area(radius)
print(f"Circle area: {area}")

# Module variables વાપરવા
print(f"PI value: {math_operations.PI}")

# Module classes વાપરવા
calc = math_operations.Calculator()
result = calc.add(10, 20)
print(f"Addition result: {result}")

મોડ્યુલ લાભો:

  • કોડ પુનઃઉપયોગીતા: એકવાર લખો, અનેક programs માં વાપરો
  • સંગઠન: સંબંધિત functions એકસાથે રાખો
  • નેમસ્પેસ: Naming conflicts ટાળો
  • જાળવણીયોગ્યતા: Update અને debug કરવું સરળ
  • સહયોગ: અન્ય developers સાથે modules share કરો

મોડ્યુલ સર્ચ પાથ:

  1. વર્તમાન directory
  2. PYTHONPATH environment variable
  3. Standard library directories
  4. Site-packages directory

બેસ્ટ પ્રેક્ટિસિસ:

  • વર્ણનાત્મક module names વાપરો
  • Documentation માટે docstrings include કરો
  • સંબંધિત functionality એકસાથે રાખો
  • Circular imports ટાળો

યાદી કૌશલ્ય: “ફાઇલ બનાવો, ફંક્શન્સ ડિફાઇન કરો, ઇમ્પોર્ટ કરો, વાપરો”


પ્રશ્ન 5(અ) [3 ગુણ]
#

ઉદાહરણ સાથે Python માં સિંગલ ઇન્હેરિટન્સ સમજાવો.

જવાબ:

Single inheritance એ જ્યારે એક class બરાબર એક parent class પાસેથી properties અને methods inherit કરે છે.

ઇન્હેરિટન્સ માળખું ટેબલ:

ઘટકભૂમિકાઉદાહરણ
Parent ClassBase/Super classclass Animal:
Child ClassDerived/Sub classclass Dog(Animal):
Inheritanceclass Child(Parent):class Dog(Animal):

ઇન્હેરિટન્સ આકૃતિ:

PCahriIOelnwnA--M--dh--n-tteetnatesCrneMbCraghalliaaealimeoteatmttrabed(esee(hksus)psd,)o(st:(::,d):e)iassnDgs:A:hoelnegeiremipat(ls)

ઉદાહરણ કોડ:

# Parent class
class Animal:
    def __init__(self, name):
        self.name = name
    
    def eat(self):
        print(f"{self.name} is eating")

# Child class inheriting from Animal
class Dog(Animal):
    def bark(self):
        print(f"{self.name} is barking")

# Using inheritance
my_dog = Dog("Buddy")
my_dog.eat()    # Inherited method
my_dog.bark()   # Own method
  • કોડ પુનઃઉપયોગ: Child class ને parent ની functionality આપમેળે મળે છે
  • વિસ્તરણ: Child નવા methods અને attributes ઉમેરી શકે છે
  • Is-a સંબંધ: Dog is-a Animal

યાદી કૌશલ્ય: “એક Parent, એક Child”


પ્રશ્ન 5(બ) [4 ગુણ]
#

Python માં એબ્સ્ટ્રેક્શનની વિભાવના અને તેના લાભો સમજાવો.

જવાબ:

Abstraction જટિલ implementation details છુપાવે છે અને user ને માત્ર આવશ્યક features બતાવે છે.

એબ્સ્ટ્રેક્શન કન્સેપ્ટ્સ ટેબલ:

કન્સેપ્ટવર્ણનઉદાહરણ
Abstract ClassInstantiate કરી શકાતું નથીclass Shape(ABC):
Abstract MethodImplement કરવું જ પડે@abstractmethod
InterfaceMethod structure define કરેdef area(self):

એબ્સ્ટ્રેક્શન Implementation:

from abc import ABC, abstractmethod

# Abstract class
class Shape(ABC):
    @abstractmethod
    def area(self):
        pass
    
    @abstractmethod
    def perimeter(self):
        pass

# Concrete class
class Rectangle(Shape):
    def __init__(self, length, width):
        self.length = length
        self.width = width
    
    def area(self):
        return self.length * self.width
    
    def perimeter(self):
        return 2 * (self.length + self.width)

લાભો ટેબલ:

લાભવર્ણનફાયદો
સરળતાજટિલ details છુપાવેવાપરવામાં સરળ
સુરક્ષાઆંતરિક implementation છુપાવેડેટા સુરક્ષા
જાળવણીયોગ્યતાImplementation બદલી શકાયલવચીક અપડેટ્સ
કોડ સંગઠનસ્પષ્ટ માળખુંબહેતર ડિઝાઇન
  • જટિલતા છુપાવવી: Users ને આંતરિક workings જાણવાની જરૂર નથી
  • સુસંગત Interface: બધા child classes એક જ structure અનુસરે છે
  • Implementation ફરજિયાત: Abstract methods child classes માં define કરવા જ પડે

યાદી કૌશલ્ય: “વિગતો છુપાવો, Interface બતાવો”


પ્રશ્ન 5(ક) [7 ગુણ]
#

મલ્ટિપલ અને મલ્ટિ-લેવલ ઇન્હેરિટન્સનું કાર્ય દર્શાવતો Python પ્રોગ્રામ વિકસાવો.

જવાબ:

પ્રોગ્રામ બંને inheritance types દર્શાવે છે: multiple (અનેક parents) અને multi-level (inheritance ની chain).

ઇન્હેરિટન્સ પ્રકારો સરખામણી:

પ્રકારમાળખુંઉદાહરણ
MultipleChild અનેક parents પાસેથી inherit કરેclass C(A, B):
Multi-levelGrandparent → Parent → Childclass C(B): જ્યાં class B(A):

ઇન્હેરિટન્સ પદાનુક્રમ:

MMuullttiFiAMpa-naDltlimoehemmgeCvaaIrhellnilhledIrniMhteatrnhicetera:nce:

સંપૂર્ણ પ્રોગ્રામ:

# Multi-level Inheritance Demo
print("=== Multi-level Inheritance ===")

class Animal:
    def __init__(self, name):
        self.name = name
    
    def eat(self):
        print(f"{self.name} can eat")

class Mammal(Animal):  # Animal પાસેથી inherit કરે છે
    def breathe(self):
        print(f"{self.name} breathes air")

class Dog(Mammal):     # Mammal પાસેથી inherit કરે છે (જે Animal પાસેથી inherit કરે છે)
    def bark(self):
        print(f"{self.name} can bark")

# Multi-level inheritance નો ઉપયોગ
my_dog = Dog("Buddy")
my_dog.eat()     # Animal પાસેથી (grandparent)
my_dog.breathe() # Mammal પાસેથી (parent)
my_dog.bark()    # પોતાની method

print("\n=== Multiple Inheritance ===")

class Father:
    def father_method(self):
        print("Method from Father class")

class Mother:
    def mother_method(self):
        print("Method from Mother class")

class Child(Father, Mother):  # Father અને Mother બંને પાસેથી inherit કરે છે
    def child_method(self):
        print("Method from Child class")

# Multiple inheritance નો ઉપયોગ
child = Child()
child.father_method()  # Father પાસેથી
child.mother_method()  # Mother પાસેથી
child.child_method()   # પોતાની method

# Inheritance ચકાસવી
print(f"\nChild inherits from Father: {issubclass(Child, Father)}")
print(f"Child inherits from Mother: {issubclass(Child, Mother)}")

અપેક્ષિત આઉટપુટ:

=== Multi-level Inheritance ===
Buddy can eat
Buddy breathes air
Buddy can bark

=== Multiple Inheritance ===
Method from Father class
Method from Mother class
Method from Child class

Child inherits from Father: True
Child inherits from Mother: True

મુખ્ય તફાવતો:

પાસુંMultipleMulti-level
Parents2 અથવા વધુ direct parentsSingle parent chain
Syntaxclass C(A, B):class C(B): જ્યાં B(A):
Inheritanceઆડીઊભી
જટિલતાવધુ (diamond problem)ઓછી

મેથડ રિઝોલ્યુશન ઓર્ડર (MRO):

  • Multiple: Python ડાબેથી-જમણે order અનુસરે છે
  • Multi-level: Inheritance chain ઉપર જાય છે

યાદી કૌશલ્ય: “અનેક Parents, મલ્ટિ-લેવલ Chain”


પ્રશ્ન 5(અ OR) [3 ગુણ]
#

Python માં આવતી 3 પ્રકારની મેથડ્સનું કાર્ય સમજાવો.

જવાબ:

Python classes માં ત્રણ પ્રકારની methods છે જે class data ને કેવી રીતે access કરે છે તેના આધારે.

મેથડ પ્રકારો ટેબલ:

મેથડ પ્રકારડેકોરેટરપ્રથમ Parameterહેતુ
Instance Methodકોઈ નહીંselfInstance data access
Class Method@classmethodclsClass data access
Static Method@staticmethodકોઈ નહીંUtility functions

ઉદાહરણ કોડ:

class Student:
    school_name = "ABC School"  # Class variable
    
    def __init__(self, name):
        self.name = name        # Instance variable
    
    # Instance method
    def display_info(self):
        print(f"Student: {self.name}")
    
    # Class method
    @classmethod
    def get_school(cls):
        return cls.school_name
    
    # Static method
    @staticmethod
    def is_adult(age):
        return age >= 18

# ઉપયોગ
student = Student("Alice")
student.display_info()           # Instance method
print(Student.get_school())      # Class method
print(Student.is_adult(20))      # Static method
  • ઇન્સ્ટન્સ મેથડ્સ: self વાપરીને object-specific data સાથે કામ કરે છે
  • ક્લાસ મેથડ્સ: cls વાપરીને class-wide data સાથે કામ કરે છે
  • સ્ટેટિક મેથડ્સ: સ્વતંત્ર utility functions

યાદી કૌશલ્ય: “Instance Self, Class Cls, Static કોઈ નહીં”


પ્રશ્ન 5(બ OR) [4 ગુણ]
#

Python માં ઇન્હેરિટન્સ દ્વારા પોલીમોર્ફિઝમ સમજાવો.

જવાબ:

Polymorphism વિવિધ classes ના objects ને સામાન્ય base class ના objects તરીકે treat કરવાની મંજૂરી આપે છે, દરેક પોતાની રીતે methods implement કરે છે.

પોલીમોર્ફિઝમ કન્સેપ્ટ ટેબલ:

પાસુંવર્ણનઉદાહરણ
સમાન Interfaceસામાન્ય method namesarea() method
અલગ Implementationદરેક class નું પોતાનું versionRectangle vs Circle area
Runtime DecisionExecution દરમ્યાન method પસંદDynamic binding

પોલીમોર્ફિઝમ ઉદાહરણ:

# Base class
class Shape:
    def area(self):
        pass

# અલગ implementations
class Rectangle(Shape):
    def __init__(self, length, width):
        self.length = length
        self.width = width
    
    def area(self):
        return self.length * self.width

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius
    
    def area(self):
        return 3.14 * self.radius * self.radius

# Polymorphic behavior
shapes = [Rectangle(5, 3), Circle(4)]

for shape in shapes:
    print(f"Area: {shape.area()}")  # સમાન method, અલગ પરિણામો

લાભો:

  • લવચીકતા: સમાન કોડ વિવિધ object types સાથે કામ કરે છે
  • વિસ્તરણશીલતા: વર્તમાન કોડ બદલ્યા વિના નવા classes ઉમેરવા સરળ
  • જાળવણીયોગ્યતા: એક class માં ફેરફાર અન્યને અસર કરતો નથી

યાદી કૌશલ્ય: “સમાન નામ, અલગ વર્તન”


પ્રશ્ન 5(ક OR) [7 ગુણ]
#

હાઇબ્રિડ ઇન્હેરિટન્સનું કાર્ય દર્શાવતો Python પ્રોગ્રામ વિકસાવો.

જવાબ:

Hybrid inheritance એ single program structure માં multiple અને multi-level inheritance ને combine કરે છે.

હાઇબ્રિડ ઇન્હેરિટન્સ માળખું:

DHgyAMbnarimimmdaallPCe(atBtase)

હાઇબ્રિડ માં ઇન્હેરિટન્સ પ્રકારો:

લેવલપ્રકારClasses
1SingleAnimal → Mammal
2MultipleMammal → Dog, Cat
3MultipleDog, Cat → Pet

સંપૂર્ણ પ્રોગ્રામ:

# Hybrid Inheritance Demonstration

print("=== Hybrid Inheritance Demo ===")

# Base class (Level 1)
class Animal:
    def __init__(self, name):
        self.name = name
    
    def eat(self):
        print(f"{self.name} can eat")
    
    def sleep(self):
        print(f"{self.name} can sleep")

# Single inheritance (Level 2)
class Mammal(Animal):
    def breathe(self):
        print(f"{self.name} breathes air")
    
    def give_birth(self):
        print(f"{self.name} gives birth to babies")

# Multiple inheritance branches (Level 3)
class Dog(Mammal):
    def bark(self):
        print(f"{self.name} barks: Woof!")
    
    def loyalty(self):
        print(f"{self.name} is loyal to owner")

class Cat(Mammal):
    def meow(self):
        print(f"{self.name} meows: Meow!")
    
    def independence(self):
        print(f"{self.name} is independent")

# Hybrid class - Multiple inheritance (Level 4)
class HybridPet(Dog, Cat):
    def __init__(self, name, breed):
        super().__init__(name)
        self.breed = breed
    
    def play(self):
        print(f"{self.name} loves to play")
    
    def show_info(self):
        print(f"Name: {self.name}, Breed: {self.breed}")

# Creating and using hybrid inheritance
print("\n--- Creating Hybrid Pet ---")
pet = HybridPet("Buddy", "Labrador-Persian Mix")

print("\n--- Methods from Animal (Great-grandparent) ---")
pet.eat()
pet.sleep()

print("\n--- Methods from Mammal (Grandparent) ---")
pet.breathe()
pet.give_birth()

print("\n--- Methods from Dog (Parent 1) ---")
pet.bark()
pet.loyalty()

print("\n--- Methods from Cat (Parent 2) ---")
pet.meow()
pet.independence()

print("\n--- Own Methods ---")
pet.play()
pet.show_info()

print("\n--- Inheritance Chain ---")
print(f"MRO (Method Resolution Order): {HybridPet.__mro__}")

# Checking inheritance relationships
print(f"\nIs HybridPet subclass of Animal? {issubclass(HybridPet, Animal)}")
print(f"Is HybridPet subclass of Dog? {issubclass(HybridPet, Dog)}")
print(f"Is HybridPet subclass of Cat? {issubclass(HybridPet, Cat)}")

અપેક્ષિત આઉટપુટ:

=== Hybrid Inheritance Demo ===

--- Creating Hybrid Pet ---

--- Methods from Animal (Great-grandparent) ---
Buddy can eat
Buddy can sleep

--- Methods from Mammal (Grandparent) ---
Buddy breathes air
Buddy gives birth to babies

--- Methods from Dog (Parent 1) ---
Buddy barks: Woof!
Buddy is loyal to owner

--- Methods from Cat (Parent 2) ---
Buddy meows: Meow!
Buddy is independent

--- Own Methods ---
Buddy loves to play
Name: Buddy, Breed: Labrador-Persian Mix

--- Inheritance Chain ---
MRO (Method Resolution Order): (<class '__main__.HybridPet'>, <class '__main__.Dog'>, <class '__main__.Cat'>, <class '__main__.Mammal'>, <class '__main__.Animal'>, <class 'object'>)

Is HybridPet subclass of Animal? True
Is HybridPet subclass of Dog? True
Is HybridPet subclass of Cat? True

હાઇબ્રિડ ઇન્હેરિટન્સની મુખ્ય વિશેષતાઓ:

  • જટિલ માળખું: વિવિધ inheritance types ને combine કરે છે
  • મેથડ રિઝોલ્યુશન ઓર્ડર: Python method lookup માટે વિશિષ્ટ order અનુસરે છે
  • ડાયમંડ પ્રોબ્લેમ: Python ના MRO દ્વારા આપમેળે handle થાય છે
  • લવચીકતા: અનેક parent classes પાસેથી methods ને access

લાભો:

  • સમૃદ્ધ Functionality: અનેક sources પાસેથી inherit કરે છે
  • કોડ પુનઃઉપયોગ: વર્તમાન કોડનો મહત્તમ ઉપયોગ
  • સંબંધ મોડેલિંગ: જટિલ વાસ્તવિક સંબંધો દર્શાવે છે

પડકારો:

  • જટિલતા: સમજવું અને maintain કરવું કઠિન
  • નામ સંઘર્ષ: અનેક parents પાસે સમાન method names હોઈ શકે
  • મેમોરી ઉપયોગ: Objects વધુ overhead carry કરે છે

યાદી કૌશલ્ય: “હાઇબ્રિડ બધા પ્રકારો Combine કરે છે”