Chapter 7 : Python Modules (Practice Sets)
इस Web Page पर Python Modules से संबंधित Practice sets Add किए गए है | Modules Python Language का एक महत्वपूर्ण Topic है | Modules का उपयोग कोड को व्यवस्थित करने और उसे बार-बार उपयोग करने (Code Reusability) के लिए किया जाता है | इस Web Page पर Python Modules से संबंधित Problems Add करने का Purpose आपको Python Modules के हर छोटे - से - छोटे Concept को समझाने से है | बिना समय खराब किए अभी नीचे दि गई Problems को Solve करे |
Problem 1 :
जब किसी व्यक्ति से उसकी Age पूछी जाती है तो वो अपनी Date Of Birth के base पर अपनी
Age को calculate करता है और बताता है |
एक Program लिखे जो ,
- Input मे किसी व्यक्ति की Date Of Birth ले |
- Date Of Birth के Base पर age calculate करे |
Code Explanation :
- from datetime import date = > datetime Module से date Module को import किया गया है |
- year = int(input("Year : ")), month = int(input("")), day = int(input("")) = > year , month और day variable create किए गए है जो input मे integer value को store करते है |
- dob = date(year, month, day) = > user से enter की गई year , month , day value है यानि user की data of birth
- today = date.today() = > today variable मे आज की date store की गई है |
- age = today.year - dob.year = > age variable मे आज की date से date of birth की date घटा करके store की गई है |
- if (today.month, today.day) < (dob.month, dob.day): = > today की date Date Of Birth की date से छोटी होने पर ये if statement True होगी |
- age -= 1 = > if statement के True होने पर age variable से 1 को minus किया गया है |
- print("Your Age is:", age, "years") = > ये print function age variable मे stored value display करता है |
Problem 2 :
websites , apps etc. के पास account create करने के लिए एक systems होता है जो user
को automatically otp generate करके देता है |
एक Program लिखे जो ,
- automatically 4 numbers का code generate करे |
- automatically generated code मे numbers के अलावा कोई और value ना हो |
Code Explanation
- import random = > random module file को import किया गया है |
- x = random.randint(1000 , 9999) = > x एक variable है जो 4 numbers का code store करता है |
- print("Random 3 digit number:", x) print function x variable मे stored value को display करता है |
Problem 3 :
Teacher "student Information" Register मे किसी Student की Information पता करने से पहले check करता है
कि कया वो student school मे पडता है या नही
नीचे दिए गए code editor मे एक Program लिखे जो ,
- user से एक folder और file का Name ले |
- folder मे file को check करे की कया वो file folder मे मौजूद है या नही|
- file के folder मे मिल जाने पर "file Exists" message show हो |
- file के folder मे न मिलने पर "file not Exists" message Show हो |
Code Explanation
- import os = > OS module File को import किया गया है |
- folder_name = input("") = > folder_name एक variable है जो input मे folder के name store करता है |
- file_name = input(" ") = > file_name एक variable है जो input मे file के name store करता है |
- path = os.path.join(folder_name, file_name) = > path variable मे folder_name और file_name दोनों की value add करके store की गई है |
- if os.path.isfile(path): = > folder मे file मिल जाने पर ये if statement true होती है |
- print("File Exists") = > if statement के True होने पर ये print() function चलता है और print() screen पर File Exists message show होता है |
- else: = > if statement के False होने पर ये else block चलेगा |
- print("File Not Exists") = > else block के True होने पर ये print() function चलता है और print() screen पर File Not Exists message show होगा |
Problem 4 :
कोई company manager campany मे काम कर रहे Total Employees पता करने के लिए company के "Employee
info" Register मे check करता है |
ए क Program लि खे जो ,
- user से input मे किसी Folder का name ले |
- Folder मे Available सभी files को count करे |
- Output मे Total कितनी files है show करे |
Code Explanation
- import os = > os module file को import किया गया है |
- folder_name = input("") = > folder_name variable input मे folder के name को store करता है |
- Total = 0 = > Total variable मे 0 value store की गई है |
- for files in os.listdir(folder_name): = > for loop एक - एक करके folder मे stored files को files variable मे store करता है |
- Total += 1 = > for loop से Total variable मे 1 value add की जा रही है |
Problem 5 :
school की sites पर online test देते time test पर Countdown होता है जो हर second पर कम होता जाता है जिससे की Students के बीच competition की भावना जागरूक होती है |
Code Explanation
- import time = > time Module file को import किया गया है |
- for i in range(20, 0, -1): = > ये for loop 20 से start होता है 0 पर stop होता है 1 value को घटाते हुए चलता है और i variable मे value store करता है |
- print(i) = > ये print() i variable मे stored value को print करता है |
- time.sleep(1) = > loop के हर iteration पर 1 second का time लगता है |
Problem 6 :
नीचे एक Program दिया गया है जो 1 से 6 तक कोई एक number randomly print करता है लेकिन Program मे एक logical error है जिसके कारण Program बिना error के run तो होता है लेकिन output wrong होता है , इसलिए नीचे दिए गए Program को सही से लिखे |
Code Explanation
- import random = > random module file को import किया गया है |
- x = random.randint(1 , 6) = > x एक variable है जो 1 से 6 तक का कोई एक Number store करता है |
- print(x) print function x variable मे stored value को display करता है |
Problem 7 :
कोई Teacher कुछ दिनो तक छोटी पर होने के बाद Students की Attendance को randomly set कर देता है |
एक Program लिखे जो ,
- 1 से 40 तक के roll numbers पर randomly Attendance set करे |
- सभी की Attendance show करे |
Code Explanation
- for roll in range(1, 41): = > ये for loop 1 से 41 तक के numbers को एक - एक करके roll_number variable मे store करता है |
- attendance = random.choice(["P", "A"]) = > attendance variable P या A मे से कोई एक value randomly store करता है |
- print(f"Roll No {roll_number}: {attendance}") = > print() function roll_number और attendance variable मे stored value को print करता है |
Problem 8 :
एक school मे कल एक Happy Independance Day आयोजित किया जाने वाला है जिसमे school के students participates करने वाले हर class से लेकिन Teacher के ये समझ नही आ रहा है कि students performance किस sequence मे हो इसलिए आपको एक Program create करना है जिसमे हर class wise students names पहले से stored हो और Program को run करने के बाद stored names और class name random order मे arrange हो जाए |
Code Explanation
- students = { = > dictionary create की गई है |
- for cls, names in students.items(): = > student मे stored data एक - एक करके names variable मे store हो रहा है |
- random.shuffle(names) = > names variable मे stored data को random order मे किया गया है |
- for name in names: = > names variable मे stored value को name value मे stored किया जा रहा है |
- print(" ", name) = > name variable को print किया गया है |
You have completed This Chapter ! 🎉
Now you can choose any one of the following options to test your knowledge :
Help Center
Users and we all help you together .