├── Computer Vision.md ├── Conditions in Python.md ├── Data Analysis with Python.md ├── Deep Learning.md ├── Dictionaries in Python.md ├── Excel for Data Analytics.md ├── Generative AI.md ├── Lists in Python.md ├── Machine Learning Algorithms.md ├── Machine Learning.md ├── Matplotlib.md ├── Natural Language Processing.md ├── NumPy.md ├── Pandas.md ├── Python Fundamentals.md ├── README.md ├── SQL Fundamentals.md ├── SQL for Data Analytics.md ├── SQL in Action.md └── Strings in Python.md /Conditions in Python.md: -------------------------------------------------------------------------------- 1 | # 50 Python Conditional Questions and Answers 2 | 3 | Below are 50 practical Python questions focusing on **if**, **nested if-else**, and multiple **elif** 4 | 5 | Each question includes: 6 | - A scenario or requirement 7 | - A code snippet demonstrating one possible solution 8 | 9 | --- 10 | 11 | ### 1. Check if a number is even or odd. 12 | **Answer:** 13 | Use the modulo operator `%` to determine if the number is divisible by 2. 14 | ```python 15 | num = 10 16 | if num % 2 == 0: 17 | print("Even") 18 | else: 19 | print("Odd") 20 | ``` 21 | 22 | --- 23 | 24 | ### 2. Check if a number is positive, negative, or zero. 25 | **Answer:** 26 | Use if-elif-else conditions to classify the number. 27 | ```python 28 | num = -5 29 | if num > 0: 30 | print("Positive") 31 | elif num < 0: 32 | print("Negative") 33 | else: 34 | print("Zero") 35 | ``` 36 | 37 | --- 38 | 39 | ### 3. Determine if a character is a vowel or consonant (assume input is a single lowercase letter). 40 | **Answer:** 41 | Use if-else to check membership in vowels. 42 | ```python 43 | ch = 'e' 44 | if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u': 45 | print("Vowel") 46 | else: 47 | print("Consonant") 48 | ``` 49 | 50 | --- 51 | 52 | ### 4. Check if a year is a leap year. 53 | **Answer:** 54 | A leap year is divisible by 400, or divisible by 4 but not by 100. 55 | ```python 56 | year = 2020 57 | if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0): 58 | print("Leap Year") 59 | else: 60 | print("Not Leap Year") 61 | ``` 62 | 63 | --- 64 | 65 | ### 5. Determine a person's stage of life (child, teen, adult, senior) based on age. 66 | **Answer:** 67 | Use multiple elif conditions for ranges. 68 | ```python 69 | age = 45 70 | if age < 13: 71 | print("Child") 72 | elif age < 20: 73 | print("Teen") 74 | elif age < 60: 75 | print("Adult") 76 | else: 77 | print("Senior") 78 | ``` 79 | 80 | --- 81 | 82 | ### 6. Check if a password meets length criteria (at least 8 characters). 83 | **Answer:** 84 | Use if-else to check string length. 85 | ```python 86 | password = "pass1234" 87 | if len(password) >= 8: 88 | print("Strong password") 89 | else: 90 | print("Weak password") 91 | ``` 92 | 93 | --- 94 | 95 | ### 7. Nested if: Check if a number is positive, and if so, whether it's even or odd. 96 | **Answer:** 97 | First check positivity, then even/odd inside another if. 98 | ```python 99 | num = 9 100 | if num > 0: 101 | if num % 2 == 0: 102 | print("Positive Even") 103 | else: 104 | print("Positive Odd") 105 | else: 106 | print("Not Positive") 107 | ``` 108 | 109 | --- 110 | 111 | ### 8. Check if two numbers are equal, or if not, determine which is larger. 112 | **Answer:** 113 | Use if-elif-else to compare. 114 | ```python 115 | a = 10 116 | b = 15 117 | if a == b: 118 | print("Equal") 119 | elif a > b: 120 | print("a is larger") 121 | else: 122 | print("b is larger") 123 | ``` 124 | 125 | --- 126 | 127 | ### 9. Determine if a given letter is uppercase, lowercase, or neither. 128 | **Answer:** 129 | Use str methods isupper() and islower(). 130 | ```python 131 | ch = 'H' 132 | if ch.isupper(): 133 | print("Uppercase") 134 | elif ch.islower(): 135 | print("Lowercase") 136 | else: 137 | print("Neither uppercase nor lowercase") 138 | ``` 139 | 140 | --- 141 | 142 | ### 10. Check if a number is divisible by both 3 and 5. 143 | **Answer:** 144 | Use multiple conditions with `and`. 145 | ```python 146 | num = 30 147 | if num % 3 == 0 and num % 5 == 0: 148 | print("Divisible by 3 and 5") 149 | else: 150 | print("Not divisible by both") 151 | ``` 152 | 153 | --- 154 | 155 | ### 11. Determine grade based on score: A(>=90), B(>=80), C(>=70), else F. 156 | **Answer:** 157 | Use multiple elif conditions. 158 | ```python 159 | score = 85 160 | if score >= 90: 161 | print("A") 162 | elif score >= 80: 163 | print("B") 164 | elif score >= 70: 165 | print("C") 166 | else: 167 | print("F") 168 | ``` 169 | 170 | --- 171 | 172 | ### 12. Check if a character is a digit. 173 | **Answer:** 174 | Use str.isdigit(). 175 | ```python 176 | ch = '5' 177 | if ch.isdigit(): 178 | print("Digit") 179 | else: 180 | print("Not a digit") 181 | ``` 182 | 183 | --- 184 | 185 | ### 13. Nested if: If an integer is positive, check if greater than 100 or not. 186 | **Answer:** 187 | Check positivity, then range. 188 | ```python 189 | num = 150 190 | if num > 0: 191 | if num > 100: 192 | print("Positive and greater than 100") 193 | else: 194 | print("Positive but 100 or less") 195 | else: 196 | print("Not positive") 197 | ``` 198 | 199 | --- 200 | 201 | ### 14. Determine if a temperature in Celsius is freezing (<0), cold (<15), warm (<25), or hot (else). 202 | **Answer:** 203 | Use multiple elif for temperature ranges. 204 | ```python 205 | temp = 10 206 | if temp < 0: 207 | print("Freezing") 208 | elif temp < 15: 209 | print("Cold") 210 | elif temp < 25: 211 | print("Warm") 212 | else: 213 | print("Hot") 214 | ``` 215 | 216 | --- 217 | 218 | ### 15. Check if a given number ends with digit 5. 219 | **Answer:** 220 | Convert to string and check last character. 221 | ```python 222 | num = 75 223 | num_str = str(num) 224 | if num_str.endswith('5'): 225 | print("Ends with 5") 226 | else: 227 | print("Does not end with 5") 228 | ``` 229 | 230 | --- 231 | 232 | ### 16. Nested if: Check if a number is non-negative, then check if it's zero or positive. 233 | **Answer:** 234 | Check >=0, then zero or positive inside another if. 235 | ```python 236 | num = 0 237 | if num >= 0: 238 | if num == 0: 239 | print("Zero") 240 | else: 241 | print("Positive") 242 | else: 243 | print("Negative") 244 | ``` 245 | 246 | --- 247 | 248 | ### 17. Determine if a character is a punctuation mark (only check . , ! ?). 249 | **Answer:** 250 | Use if-elif-else with character comparisons. 251 | ```python 252 | ch = '?' 253 | if ch == '.' or ch == ',' or ch == '!' or ch == '?': 254 | print("Punctuation") 255 | else: 256 | print("Not punctuation") 257 | ``` 258 | 259 | --- 260 | 261 | ### 18. Check if a given name starts with a vowel. 262 | **Answer:** 263 | Check first character for vowels. 264 | ```python 265 | name = "Ashish" 266 | first_char = name[0].lower() 267 | if first_char == 'a' or first_char == 'e' or first_char == 'i' or first_char == 'o' or first_char == 'u': 268 | print("Starts with vowel") 269 | else: 270 | print("Does not start with vowel") 271 | ``` 272 | 273 | --- 274 | 275 | ### 19. Multi-elif: Determine time of day by hour (0-23): 276 | - <6: Early Morning 277 | - <12: Morning 278 | - <18: Afternoon 279 | - <24: Night 280 | **Answer:** 281 | Use multiple elif conditions. 282 | ```python 283 | hour = 14 284 | if hour < 6: 285 | print("Early Morning") 286 | elif hour < 12: 287 | print("Morning") 288 | elif hour < 18: 289 | print("Afternoon") 290 | elif hour < 24: 291 | print("Night") 292 | else: 293 | print("Invalid hour") 294 | ``` 295 | 296 | --- 297 | 298 | ### 20. Check if a username length is valid (5 to 10 chars). 299 | **Answer:** 300 | Use if-elif-else to check length ranges. 301 | ```python 302 | username = "Ashish" 303 | length = len(username) 304 | if length < 5: 305 | print("Too short") 306 | elif length > 10: 307 | print("Too long") 308 | else: 309 | print("Valid length") 310 | ``` 311 | 312 | --- 313 | 314 | ### 21. Nested if: If a number is even, check if it is also divisible by 4. 315 | **Answer:** 316 | Check even first, then divisibility by 4. 317 | ```python 318 | num = 8 319 | if num % 2 == 0: 320 | if num % 4 == 0: 321 | print("Even and divisible by 4") 322 | else: 323 | print("Even but not divisible by 4") 324 | else: 325 | print("Odd") 326 | ``` 327 | 328 | --- 329 | 330 | ### 22. Determine if a letter is uppercase vowel, lowercase vowel, uppercase consonant, or lowercase consonant. 331 | **Answer:** 332 | Use multiple if-elif conditions. 333 | ```python 334 | ch = 'U' 335 | vowels = 'aeiouAEIOU' 336 | if ch in vowels: 337 | if ch.isupper(): 338 | print("Uppercase vowel") 339 | else: 340 | print("Lowercase vowel") 341 | else: 342 | if ch.isupper(): 343 | print("Uppercase consonant") 344 | else: 345 | print("Lowercase consonant") 346 | ``` 347 | 348 | --- 349 | 350 | ### 23. Check if a number is within a certain range (e.g., between 1 and 100). 351 | **Answer:** 352 | Use if-else with range conditions. 353 | ```python 354 | num = 50 355 | if 1 <= num <= 100: 356 | print("Within range") 357 | else: 358 | print("Out of range") 359 | ``` 360 | 361 | --- 362 | 363 | ### 24. Multi-elif: Classify a number by digit count: 364 | - Single-digit (|num| < 10) 365 | - Two-digit (|num| < 100) 366 | - Three or more digits (|num| >= 100) 367 | **Answer:** 368 | Use abs() and multiple elif. 369 | ```python 370 | num = 999 371 | abs_num = abs(num) 372 | if abs_num < 10: 373 | print("Single-digit") 374 | elif abs_num < 100: 375 | print("Two-digit") 376 | else: 377 | print("Three or more digits") 378 | ``` 379 | 380 | --- 381 | 382 | ### 25. Nested if: If a number is positive, check if it's prime or not by a simple method (just check divisibility by 2 and 3). 383 | **Answer:** 384 | This is limited without loops or data structures, but let's just show conditions. 385 | ```python 386 | num = 9 387 | if num > 0: 388 | if num == 2 or num == 3: 389 | print("Prime") 390 | elif num % 2 != 0 and num % 3 != 0: 391 | print("Likely prime") 392 | else: 393 | print("Not prime") 394 | else: 395 | print("Not positive") 396 | ``` 397 | 398 | --- 399 | 400 | ### 26. Check if a given string is uppercase, lowercase, or mixed. 401 | **Answer:** 402 | Use str.isupper() and str.islower(). 403 | ```python 404 | text = "HELLO" 405 | if text.isupper(): 406 | print("All uppercase") 407 | elif text.islower(): 408 | print("All lowercase") 409 | else: 410 | print("Mixed case") 411 | ``` 412 | 413 | --- 414 | 415 | ### 27. Multi-elif: BMI Categories: 416 | - <18.5: Underweight 417 | - <25: Normal 418 | - <30: Overweight 419 | - else: Obese 420 | **Answer:** 421 | Use multiple elif for ranges. 422 | ```python 423 | bmi = 22 424 | if bmi < 18.5: 425 | print("Underweight") 426 | elif bmi < 25: 427 | print("Normal") 428 | elif bmi < 30: 429 | print("Overweight") 430 | else: 431 | print("Obese") 432 | ``` 433 | 434 | --- 435 | 436 | ### 28. Check if a character is a whitespace. 437 | **Answer:** 438 | Check if char == ' '. 439 | ```python 440 | ch = ' ' 441 | if ch == ' ': 442 | print("Whitespace") 443 | else: 444 | print("Not whitespace") 445 | ``` 446 | 447 | --- 448 | 449 | ### 29. Nested if: If a number is non-negative, check if it's an exact multiple of 10. 450 | **Answer:** 451 | First check non-negative, then divisibility by 10. 452 | ```python 453 | num = 40 454 | if num >= 0: 455 | if num % 10 == 0: 456 | print("Non-negative multiple of 10") 457 | else: 458 | print("Non-negative but not multiple of 10") 459 | else: 460 | print("Negative") 461 | ``` 462 | 463 | --- 464 | 465 | ### 30. Multi-elif: Determine shipping cost by weight category: 466 | - <=1kg: $5 467 | - <=5kg: $10 468 | - <=20kg: $20 469 | - else: $50 470 | **Answer:** 471 | Use multiple elif. 472 | ```python 473 | weight = 6 474 | if weight <= 1: 475 | print("$5") 476 | elif weight <= 5: 477 | print("$10") 478 | elif weight <= 20: 479 | print("$20") 480 | else: 481 | print("$50") 482 | ``` 483 | 484 | --- 485 | 486 | ### 31. Check if a given string starts with a capital letter. 487 | **Answer:** 488 | Check first char using isupper(). 489 | ```python 490 | text = "Hello" 491 | if text and text[0].isupper(): 492 | print("Starts with capital letter") 493 | else: 494 | print("Does not start with capital letter") 495 | ``` 496 | 497 | --- 498 | 499 | ### 32. Nested if: If a number is positive, check if it is a perfect square of an integer (just check num==4 or num==9 for simplicity). 500 | **Answer:** 501 | Restricted check without loops. 502 | ```python 503 | num = 9 504 | if num > 0: 505 | if num == 4 or num == 9: 506 | print("Positive perfect square (from limited check)") 507 | else: 508 | print("Positive but not perfect square (from limited check)") 509 | else: 510 | print("Not positive") 511 | ``` 512 | 513 | --- 514 | 515 | ### 33. Multi-elif: Classify an angle (in degrees): 516 | - <90: Acute 517 | - ==90: Right 518 | - <180: Obtuse 519 | - ==180: Straight 520 | - else: Reflex 521 | **Answer:** 522 | Use multiple if-elif. 523 | ```python 524 | angle = 120 525 | if angle < 90: 526 | print("Acute") 527 | elif angle == 90: 528 | print("Right") 529 | elif angle < 180: 530 | print("Obtuse") 531 | elif angle == 180: 532 | print("Straight") 533 | else: 534 | print("Reflex") 535 | ``` 536 | 537 | --- 538 | 539 | ### 34. Check if a number is close to 100 (within +/-10). 540 | **Answer:** 541 | Use abs difference. 542 | ```python 543 | num = 95 544 | if abs(num - 100) <= 10: 545 | print("Close to 100") 546 | else: 547 | print("Not close") 548 | ``` 549 | 550 | --- 551 | 552 | ### 35. Nested if: Check if a given code is 'admin', if yes check if 'active' is True, else print restricted access. 553 | **Answer:** 554 | Simulate user_role and active condition. 555 | ```python 556 | user_role = 'admin' 557 | active = True 558 | if user_role == 'admin': 559 | if active: 560 | print("Full Access") 561 | else: 562 | print("Admin not active") 563 | else: 564 | print("Restricted Access") 565 | ``` 566 | 567 | --- 568 | 569 | ### 36. Multi-elif: Classify speed (km/h): 570 | - <30: Slow 571 | - <60: Normal 572 | - <100: Fast 573 | - else: Very Fast 574 | **Answer:** 575 | Use multiple elif. 576 | ```python 577 | speed = 75 578 | if speed < 30: 579 | print("Slow") 580 | elif speed < 60: 581 | print("Normal") 582 | elif speed < 100: 583 | print("Fast") 584 | else: 585 | print("Very Fast") 586 | ``` 587 | 588 | --- 589 | 590 | ### 37. Determine if a given character is uppercase vowel, uppercase consonant, or not an uppercase letter at all. 591 | **Answer:** 592 | Nested logic on uppercase and vowels. 593 | ```python 594 | ch = 'E' 595 | if ch.isupper(): 596 | if ch == 'A' or ch == 'E' or ch == 'I' or ch == 'O' or ch == 'U': 597 | print("Uppercase vowel") 598 | else: 599 | print("Uppercase consonant") 600 | else: 601 | print("Not uppercase letter") 602 | ``` 603 | 604 | --- 605 | 606 | ### 38. If a number is negative, print "Negative". If zero, print "Zero". Otherwise, print "Positive". 607 | **Answer:** 608 | Simple if-elif-else. 609 | ```python 610 | num = 0 611 | if num < 0: 612 | print("Negative") 613 | elif num == 0: 614 | print("Zero") 615 | else: 616 | print("Positive") 617 | ``` 618 | 619 | --- 620 | 621 | ### 39. Check if a string is a palindrome (only check first and last character for demonstration). 622 | **Answer:** 623 | No data structures, just basic conditions. 624 | ```python 625 | text = "madam" 626 | if text and text[0] == text[-1]: 627 | print("Potential palindrome (first and last char match)") 628 | else: 629 | print("Not palindrome based on first and last char") 630 | ``` 631 | 632 | --- 633 | 634 | ### 40. Multi-elif: Assign a label based on salary: 635 | - >100000: "High earner" 636 | - >50000: "Mid earner" 637 | - >20000: "Low earner" 638 | - else: "Very low" 639 | **Answer:** 640 | Use multiple elif. 641 | ```python 642 | salary = 30000 643 | if salary > 100000: 644 | print("High earner") 645 | elif salary > 50000: 646 | print("Mid earner") 647 | elif salary > 20000: 648 | print("Low earner") 649 | else: 650 | print("Very low") 651 | ``` 652 | 653 | --- 654 | 655 | ### 41. Nested if: If a character is a letter, check if it is uppercase or lowercase. 656 | **Answer:** 657 | Check if letter using isalpha(), then case. 658 | ```python 659 | ch = 'G' 660 | if ch.isalpha(): 661 | if ch.isupper(): 662 | print("Uppercase letter") 663 | else: 664 | print("Lowercase letter") 665 | else: 666 | print("Not a letter") 667 | ``` 668 | 669 | --- 670 | 671 | ### 42. Check if a number is exactly divisible by 2, 3, or 5 (just print the first that applies). 672 | **Answer:** 673 | Use multiple if/elif. 674 | ```python 675 | num = 9 676 | if num % 2 == 0: 677 | print("Divisible by 2") 678 | elif num % 3 == 0: 679 | print("Divisible by 3") 680 | elif num % 5 == 0: 681 | print("Divisible by 5") 682 | else: 683 | print("Not divisible by 2, 3, or 5") 684 | ``` 685 | 686 | --- 687 | 688 | ### 43. Determine if a string length is odd or even. 689 | **Answer:** 690 | Use len() % 2. 691 | ```python 692 | text = "Hello" 693 | if len(text) % 2 == 0: 694 | print("Even length") 695 | else: 696 | print("Odd length") 697 | ``` 698 | 699 | --- 700 | 701 | ### 44. Nested if: If a number > 10, check if it's >50, else just print "Between 10 and 50". 702 | **Answer:** 703 | Two-level check. 704 | ```python 705 | num = 60 706 | if num > 10: 707 | if num > 50: 708 | print("Greater than 50") 709 | else: 710 | print("Between 10 and 50") 711 | else: 712 | print("10 or less") 713 | ``` 714 | 715 | --- 716 | 717 | ### 45. Multi-elif: Classify a day number (1=Mon,...7=Sun): 718 | - 1-5: Weekday 719 | - 6: Saturday 720 | - 7: Sunday 721 | - else: Invalid 722 | **Answer:** 723 | Use if-elif-else. 724 | ```python 725 | day = 7 726 | if day >= 1 and day <= 5: 727 | print("Weekday") 728 | elif day == 6: 729 | print("Saturday") 730 | elif day == 7: 731 | print("Sunday") 732 | else: 733 | print("Invalid") 734 | ``` 735 | 736 | --- 737 | 738 | ### 46. Check if a variable is None. 739 | **Answer:** 740 | Use `is None`. 741 | ```python 742 | var = None 743 | if var is None: 744 | print("var is None") 745 | else: 746 | print("var has a value") 747 | ``` 748 | 749 | --- 750 | 751 | ### 47. Nested if: If a character is a digit, check if it's even or odd digit. 752 | **Answer:** 753 | Check digit first, then numeric value. 754 | ```python 755 | ch = '4' 756 | if ch.isdigit(): 757 | digit_value = int(ch) 758 | if digit_value % 2 == 0: 759 | print("Even digit") 760 | else: 761 | print("Odd digit") 762 | else: 763 | print("Not a digit") 764 | ``` 765 | 766 | --- 767 | 768 | ### 48. Multi-elif: Classify angle speed in degrees per second: 769 | - <1 deg/s: Very slow 770 | - <10 deg/s: Slow 771 | - <100 deg/s: Moderate 772 | - else: Fast 773 | **Answer:** 774 | Use multiple elif. 775 | ```python 776 | speed = 5 777 | if speed < 1: 778 | print("Very slow") 779 | elif speed < 10: 780 | print("Slow") 781 | elif speed < 100: 782 | print("Moderate") 783 | else: 784 | print("Fast") 785 | ``` 786 | 787 | --- 788 | 789 | ### 49. Check if a string starts with 'A' and ends with 'Z'. 790 | **Answer:** 791 | Use conditions on first and last char. 792 | ```python 793 | text = "ABCZ" 794 | if text and text[0] == 'A' and text[-1] == 'Z': 795 | print("Starts with A and ends with Z") 796 | else: 797 | print("Condition not met") 798 | ``` 799 | 800 | --- 801 | 802 | ### 50. Nested if: If year is divisible by 4, check if also divisible by 100. If yes, then must be divisible by 400 for leap year. 803 | **Answer:** 804 | Detailed leap year check. 805 | ```python 806 | year = 2000 807 | if year % 4 == 0: 808 | if year % 100 == 0: 809 | if year % 400 == 0: 810 | print("Leap Year") 811 | else: 812 | print("Not Leap Year") 813 | else: 814 | print("Leap Year") 815 | else: 816 | print("Not Leap Year") 817 | ``` 818 | 819 | --- 820 | 821 | If you found this helpful, please consider following or starring the repository! 822 | 823 | Follow me on: 824 | - [LinkedIn](https://www.linkedin.com/in/ashish-jangra/) 825 | - [GitHub](https://github.com/AshishJangra27) 826 | - [Kaggle](https://www.kaggle.com/ashishjangra27) 827 | 828 | Stay updated with my latest content and projects! 829 | -------------------------------------------------------------------------------- /Dictionaries in Python.md: -------------------------------------------------------------------------------- 1 | # 50 Python Dictionaries Interview Questions and Answers 2 | ### 1. How do you merge two dictionaries in Python? 3 | **Answer:** 4 | You can merge two dictionaries using the `update()` method or the `{**dict1, **dict2}` syntax. 5 | ```python 6 | def merge_dicts(dict1, dict2): 7 | merged_dict = {**dict1, **dict2} 8 | return merged_dict 9 | dict1 = {'a': 1, 'b': 2} 10 | dict2 = {'b': 3, 'c': 4} 11 | merged_dict = merge_dicts(dict1, dict2) 12 | print(merged_dict) # Output: {'a': 1, 'b': 3, 'c': 4} 13 | ``` 14 | --- 15 | ### 2. How do you invert a dictionary in Python? 16 | **Answer:** 17 | You can invert a dictionary by swapping keys and values using a dictionary comprehension. 18 | ```python 19 | def invert_dict(d): 20 | return {v: k for k, v in d.items()} 21 | d = {'a': 1, 'b': 2, 'c': 3} 22 | inverted_d = invert_dict(d) 23 | print(inverted_d) # Output: {1: 'a', 2: 'b', 3: 'c'} 24 | ``` 25 | --- 26 | ### 3. How do you remove a key from a dictionary in Python? 27 | **Answer:** 28 | You can remove a key from a dictionary using the `pop()` method. 29 | ```python 30 | def remove_key(d, key): 31 | d.pop(key, None) 32 | return d 33 | d = {'a': 1, 'b': 2, 'c': 3} 34 | d = remove_key(d, 'b') 35 | print(d) # Output: {'a': 1, 'c': 3} 36 | ``` 37 | --- 38 | ### 4. How do you create a dictionary from two lists in Python? 39 | **Answer:** 40 | You can create a dictionary from two lists using the `zip()` function. 41 | ```python 42 | def lists_to_dict(keys, values): 43 | return dict(zip(keys, values)) 44 | keys = ['a', 'b', 'c'] 45 | values = [1, 2, 3] 46 | d = lists_to_dict(keys, values) 47 | print(d) # Output: {'a': 1, 'b': 2, 'c': 3} 48 | ``` 49 | --- 50 | ### 5. How do you count the frequency of elements in a list using a dictionary in Python? 51 | **Answer:** 52 | You can count the frequency of elements using a dictionary to keep track of counts. 53 | ```python 54 | def count_frequency(lst): 55 | freq_dict = {} 56 | for item in lst: 57 | if item in freq_dict: 58 | freq_dict[item] += 1 59 | else: 60 | freq_dict[item] = 1 61 | return freq_dict 62 | lst = ['a', 'b', 'a', 'c', 'b', 'a'] 63 | frequency = count_frequency(lst) 64 | print(frequency) # Output: {'a': 3, 'b': 2, 'c': 1} 65 | ``` 66 | --- 67 | ### 6. How do you sort a dictionary by its keys in Python? 68 | **Answer:** 69 | You can sort a dictionary by its keys using the `sorted()` function. 70 | ```python 71 | def sort_dict_by_keys(d): 72 | return dict(sorted(d.items())) 73 | d = {'b': 2, 'a': 1, 'c': 3} 74 | sorted_d = sort_dict_by_keys(d) 75 | print(sorted_d) # Output: {'a': 1, 'b': 2, 'c': 3} 76 | ``` 77 | --- 78 | ### 7. How do you sort a dictionary by its values in Python? 79 | **Answer:** 80 | You can sort a dictionary by its values using the `sorted()` function with a key argument. 81 | ```python 82 | def sort_dict_by_values(d): 83 | return dict(sorted(d.items(), key=lambda item: item.value())) 84 | d = {'a': 3, 'b': 1, 'c': 2} 85 | sorted_d = sort_dict_by_values(d) 86 | print(sorted_d) # Output: {'b': 1, 'c': 2, 'a': 3} 87 | ``` 88 | --- 89 | ### 8. How do you check if a key exists in a dictionary in Python? 90 | **Answer:** 91 | You can check if a key exists in a dictionary using the `in` keyword. 92 | ```python 93 | def key_exists(d, key): 94 | return key in d 95 | d = {'a': 1, 'b': 2, 'c': 3} 96 | print(key_exists(d, 'b')) # Output: True 97 | print(key_exists(d, 'd')) # Output: False 98 | ``` 99 | --- 100 | ### 9. How do you get a value from a dictionary with a default value if the key does not exist in Python? 101 | **Answer:** 102 | You can get a value with a default using the `get()` method. 103 | ```python 104 | def get_value_with_default(d, key, default): 105 | return d.get(key, default) 106 | d = {'a': 1, 'b': 2, 'c': 3} 107 | value = get_value_with_default(d, 'd', 0) 108 | print(value) # Output: 0 109 | ``` 110 | --- 111 | ### 10. How do you find the maximum value in a dictionary in Python? 112 | **Answer:** 113 | You can find the maximum value using the `max()` function. 114 | ```python 115 | def max_value_in_dict(d): 116 | return max(d.values()) 117 | d = {'a': 1, 'b': 3, 'c': 2} 118 | max_value = max_value_in_dict(d) 119 | print(max_value) # Output: 3 120 | ``` 121 | --- 122 | ### 11. How do you find the key of the maximum value in a dictionary in Python? 123 | **Answer:** 124 | You can find the key of the maximum value using the `max()` function with a key argument. 125 | ```python 126 | def key_of_max_value(d): 127 | return max(d, key=d.get) 128 | d = {'a': 1, 'b': 3, 'c': 2} 129 | key = key_of_max_value(d) 130 | print(key) # Output: 'b' 131 | ``` 132 | --- 133 | ### 12. How do you update multiple keys in a dictionary in Python? 134 | **Answer:** 135 | You can update multiple keys using the `update()` method. 136 | ```python 137 | def update_keys(d, updates): 138 | d.update(updates) 139 | return d 140 | d = {'a': 1, 'b': 2, 'c': 3} 141 | updates = {'b': 4, 'c': 5, 'd': 6} 142 | updated_d = update_keys(d, updates) 143 | print(updated_d) # Output: {'a': 1, 'b': 4, 'c': 5, 'd': 6} 144 | ``` 145 | --- 146 | ### 13. How do you remove keys with empty values from a dictionary in Python? 147 | **Answer:** 148 | You can remove keys with empty values using a dictionary comprehension. 149 | ```python 150 | def remove_empty_values(d): 151 | return {k: v for k, v in d.items() if v} 152 | d = {'a': 1, 'b': '', 'c': 3, 'd': None} 153 | cleaned_d = remove_empty_values(d) 154 | print(cleaned_d) # Output: {'a': 1, 'c': 3} 155 | ``` 156 | --- 157 | ### 14. How do you create a dictionary with default values in Python? 158 | **Answer:** 159 | You can create a dictionary with default values using the `fromkeys()` method. 160 | ```python 161 | def create_dict_with_defaults(keys, default): 162 | return dict.fromkeys(keys, default) 163 | keys = ['a', 'b', 'c'] 164 | default_value = 0 165 | d = create_dict_with_defaults(keys, default_value) 166 | print(d) # Output: {'a': 0, 'b': 0, 'c': 0} 167 | ``` 168 | --- 169 | ### 15. How do you group elements of a list based on a dictionary key in Python? 170 | **Answer:** 171 | You can group elements using `defaultdict` from the `collections` module. 172 | ```python 173 | from collections import defaultdict 174 | def group_elements_by_key(lst, key_func): 175 | grouped = defaultdict(list) 176 | for item in lst: 177 | key = key_func(item) 178 | grouped[key].append(item) 179 | return grouped 180 | lst = ['apple', 'banana', 'cherry', 'avocado'] 181 | grouped = group_elements_by_key(lst, lambda x: x[0]) 182 | print(grouped) # Output: {'a': ['apple', 'avocado'], 'b': ['banana'], 'c': ['cherry']} 183 | ``` 184 | --- 185 | ### 16. How do you increment values in a dictionary in Python? 186 | **Answer:** 187 | You can increment values using the `defaultdict` from the `collections` module. 188 | ```python 189 | from collections import defaultdict 190 | def increment_dict_values(lst): 191 | counter = defaultdict(int) 192 | for item in lst: 193 | counter[item] += 1 194 | return counter 195 | lst = ['a', 'b', 'a', 'c', 'b', 'a'] 196 | incremented_dict = increment_dict_values(lst) 197 | print(incremented_dict) # Output: {'a': 3, 'b': 2, 'c': 1} 198 | ``` 199 | --- 200 | ### 17. How do you create a dictionary of lists in Python? 201 | **Answer:** 202 | You can create a dictionary of lists using `defaultdict` from the `collections` module. 203 | ```python 204 | from collections import defaultdict 205 | def create_dict_of_lists(pairs): 206 | d = defaultdict(list) 207 | for key, value in pairs: 208 | d[key].append(value) 209 | return d 210 | pairs = [('a', 1), ('b', 2), ('a', 211 | 3), ('b', 4)] 212 | dict_of_lists = create_dict_of_lists(pairs) 213 | print(dict_of_lists) # Output: {'a': [1, 3], 'b': [2, 4]} 214 | ``` 215 | --- 216 | ### 18. How do you merge dictionaries with sum of values for common keys in Python? 217 | **Answer:** 218 | You can merge dictionaries and sum the values for common keys using `Counter` from the `collections` module. 219 | ```python 220 | from collections import Counter 221 | def merge_dicts_with_sum(dict1, dict2): 222 | return dict(Counter(dict1) + Counter(dict2)) 223 | dict1 = {'a': 1, 'b': 2, 'c': 3} 224 | dict2 = {'b': 3, 'c': 4, 'd': 5} 225 | merged_dict = merge_dicts_with_sum(dict1, dict2) 226 | print(merged_dict) # Output: {'a': 1, 'b': 5, 'c': 7, 'd': 5} 227 | ``` 228 | --- 229 | ### 19. How do you count the frequency of characters in a string using a dictionary in Python? 230 | **Answer:** 231 | You can count the frequency of characters using a dictionary to keep track of counts. 232 | ```python 233 | def count_char_frequency(s): 234 | freq_dict = {} 235 | for char in s: 236 | if char in freq_dict: 237 | freq_dict[char] += 1 238 | else: 239 | freq_dict[char] = 1 240 | return freq_dict 241 | s = 'abracadabra' 242 | frequency = count_char_frequency(s) 243 | print(frequency) # Output: {'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1} 244 | ``` 245 | --- 246 | ### 20. How do you remove duplicate values from a dictionary in Python? 247 | **Answer:** 248 | You can remove duplicate values using a dictionary comprehension. 249 | ```python 250 | def remove_duplicate_values(d): 251 | reverse_dict = {} 252 | for k, v in d.items(): 253 | if v not in reverse_dict.values(): 254 | reverse_dict[k] = v 255 | return reverse_dict 256 | d = {'a': 1, 'b': 2, 'c': 1, 'd': 3} 257 | cleaned_d = remove_duplicate_values(d) 258 | print(cleaned_d) # Output: {'a': 1, 'b': 2, 'd': 3} 259 | ``` 260 | --- 261 | ### 21. How do you flatten a nested dictionary in Python? 262 | **Answer:** 263 | You can flatten a nested dictionary using a recursive function. 264 | ```python 265 | def flatten_dict(d, parent_key='', sep='_'): 266 | items = [] 267 | for k, v in d.items(): 268 | new_key = parent_key + sep + k if parent_key else k 269 | if isinstance(v, dict): 270 | items.extend(flatten_dict(v, new_key, sep=sep).items()) 271 | else: 272 | items.append((new_key, v)) 273 | return dict(items) 274 | nested_dict = {'a': 1, 'b': {'c': 2, 'd': {'e': 3}}} 275 | flat_dict = flatten_dict(nested_dict) 276 | print(flat_dict) # Output: {'a': 1, 'b_c': 2, 'b_d_e': 3} 277 | ``` 278 | --- 279 | ### 22. How do you create a dictionary with default values for missing keys in Python? 280 | **Answer:** 281 | You can create a dictionary with default values for missing keys using `defaultdict` from the `collections` module. 282 | ```python 283 | from collections import defaultdict 284 | def create_dict_with_defaults(): 285 | return defaultdict(lambda: 'default_value') 286 | d = create_dict_with_defaults() 287 | d['a'] = 1 288 | print(d['a']) # Output: 1 289 | print(d['b']) # Output: 'default_value' 290 | ``` 291 | --- 292 | ### 23. How do you access nested dictionary keys safely in Python? 293 | **Answer:** 294 | You can access nested dictionary keys safely using the `get()` method. 295 | ```python 296 | def get_nested_key(d, keys): 297 | for key in keys: 298 | d = d.get(key, {}) 299 | if not d: 300 | return None 301 | return d 302 | nested_dict = {'a': {'b': {'c': 1}}} 303 | value = get_nested_key(nested_dict, ['a', 'b', 'c']) 304 | print(value) # Output: 1 305 | value = get_nested_key(nested_dict, ['a', 'x', 'c']) 306 | print(value) # Output: None 307 | ``` 308 | --- 309 | ### 24. How do you find the intersection of two dictionaries in Python? 310 | **Answer:** 311 | You can find the intersection of two dictionaries by keeping common keys and values. 312 | ```python 313 | def dict_intersection(dict1, dict2): 314 | return {k: dict1[k] for k in dict1 if k in dict2 and dict1[k] == dict2[k]} 315 | dict1 = {'a': 1, 'b': 2, 'c': 3} 316 | dict2 = {'b': 2, 'c': 4, 'd': 5} 317 | intersection = dict_intersection(dict1, dict2) 318 | print(intersection) # Output: {'b': 2} 319 | ``` 320 | --- 321 | ### 25. How do you find the difference between two dictionaries in Python? 322 | **Answer:** 323 | You can find the difference between two dictionaries by keeping keys not common to both dictionaries. 324 | ```python 325 | def dict_difference(dict1, dict2): 326 | return {k: v for k, v in dict1.items() if k not in dict2} 327 | dict1 = {'a': 1, 'b': 2, 'c': 3} 328 | dict2 = {'b': 2, 'c': 4, 'd': 5} 329 | difference = dict_difference(dict1, dict2) 330 | print(difference) # Output: {'a': 1} 331 | ``` 332 | --- 333 | ### 26. How do you filter a dictionary by value in Python? 334 | **Answer:** 335 | You can filter a dictionary by value using a dictionary comprehension. 336 | ```python 337 | def filter_dict_by_value(d, threshold): 338 | return {k: v for k, v in d.items() if v > threshold} 339 | d = {'a': 1, 'b': 2, 'c': 3} 340 | filtered_d = filter_dict_by_value(d, 1) 341 | print(filtered_d) # Output: {'b': 2, 'c': 3} 342 | ``` 343 | --- 344 | ### 27. How do you find the minimum value in a dictionary in Python? 345 | **Answer:** 346 | You can find the minimum value using the `min()` function. 347 | ```python 348 | def min_value_in_dict(d): 349 | return min(d.values()) 350 | d = {'a': 1, 'b': 3, 'c': 2} 351 | min_value = min_value_in_dict(d) 352 | print(min_value) # Output: 1 353 | ``` 354 | --- 355 | ### 28. How do you find the key of the minimum value in a dictionary in Python? 356 | **Answer:** 357 | You can find the key of the minimum value using the `min()` function with a key argument. 358 | ```python 359 | def key_of_min_value(d): 360 | return min(d, key=d.get) 361 | d = {'a': 1, 'b': 3, 'c': 2} 362 | key = key_of_min_value(d) 363 | print(key) # Output: 'a' 364 | ``` 365 | --- 366 | ### 29. How do you iterate over dictionary keys and values in Python? 367 | **Answer:** 368 | You can iterate over dictionary keys and values using a for loop. 369 | ```python 370 | def iterate_dict(d): 371 | for key, value in d.items(): 372 | print(f'{key}: {value}') 373 | d = {'a': 1, 'b': 2, 'c': 3} 374 | iterate_dict(d) 375 | # Output: 376 | # a: 1 377 | # b: 2 378 | # c: 3 379 | ``` 380 | --- 381 | ### 30. How do you find the union of multiple dictionaries in Python? 382 | **Answer:** 383 | You can find the union of multiple dictionaries using the `{**d1, **d2, **d3}` syntax. 384 | ```python 385 | def union_dicts(*dicts): 386 | result = {} 387 | for d in dicts: 388 | result.update(d) 389 | return result 390 | dict1 = {'a': 1, 'b': 2} 391 | dict2 = {'b': 3, 'c': 4} 392 | dict3 = {'d': 5} 393 | union = union_dicts(dict1, dict2, dict3) 394 | print(union) # Output: {'a': 1, 'b': 3, 'c': 4, 'd': 5} 395 | ``` 396 | --- 397 | ### 31. How do you replace dictionary values with the sum of same keys in Python? 398 | **Answer:** 399 | You can replace dictionary values with the sum of same keys using `Counter` from the `collections` module. 400 | ```python 401 | from collections import Counter 402 | def replace_with_sum(*dicts): 403 | total = Counter() 404 | for d in dicts: 405 | total.update(d) 406 | return dict(total) 407 | dict1 = {'a': 1, 'b': 2} 408 | dict2 = {'b': 3, 'c': 4} 409 | dict3 = {'a': 2, 'c': 1} 410 | result = replace_with_sum(dict1, dict2, dict3) 411 | print(result) # Output: {'a': 3, 'b': 5, 'c': 5} 412 | ``` 413 | --- 414 | ### 32. How do you add a prefix or suffix to dictionary keys in Python? 415 | **Answer:** 416 | You can add a prefix or suffix to dictionary keys using a dictionary comprehension. 417 | ```python 418 | def add_prefix_suffix(d, prefix='', suffix=''): 419 | return {prefix + k + suffix: v for k, v in d.items()} 420 | d 421 | = {'a': 1, 'b': 2, 'c': 3} 422 | prefixed_d = add_prefix_suffix(d, prefix='pre_') 423 | print(prefixed_d) # Output: {'pre_a': 1, 'pre_b': 2, 'pre_c': 3} 424 | suffixed_d = add_prefix_suffix(d, suffix='_suf') 425 | print(suffixed_d) # Output: {'a_suf': 1, 'b_suf': 2, 'c_suf': 3} 426 | ``` 427 | --- 428 | ### 33. How do you extract a subset of a dictionary in Python? 429 | **Answer:** 430 | You can extract a subset of a dictionary by specifying the keys of interest. 431 | ```python 432 | def extract_subset(d, keys): 433 | return {k: d[k] for k in keys if k in d} 434 | d = {'a': 1, 'b': 2, 'c': 3} 435 | keys = ['a', 'c'] 436 | subset = extract_subset(d, keys) 437 | print(subset) # Output: {'a': 1, 'c': 3} 438 | ``` 439 | --- 440 | ### 34. How do you map values of a dictionary to another dictionary in Python? 441 | **Answer:** 442 | You can map values of a dictionary to another dictionary using a dictionary comprehension. 443 | ```python 444 | def map_values(d, mapping): 445 | return {k: mapping.get(v, v) for k, v in d.items()} 446 | d = {'a': 1, 'b': 2, 'c': 3} 447 | mapping = {1: 'one', 2: 'two'} 448 | mapped_d = map_values(d, mapping) 449 | print(mapped_d) # Output: {'a': 'one', 'b': 'two', 'c': 3} 450 | ``` 451 | --- 452 | ### 35. How do you convert two lists into a dictionary with lists as values in Python? 453 | **Answer:** 454 | You can convert two lists into a dictionary with lists as values using `defaultdict` from the `collections` module. 455 | ```python 456 | from collections import defaultdict 457 | def lists_to_dict_of_lists(keys, values): 458 | d = defaultdict(list) 459 | for k, v in zip(keys, values): 460 | d[k].append(v) 461 | return d 462 | keys = ['a', 'b', 'a', 'c'] 463 | values = [1, 2, 3, 4] 464 | dict_of_lists = lists_to_dict_of_lists(keys, values) 465 | print(dict_of_lists) # Output: {'a': [1, 3], 'b': [2], 'c': [4]} 466 | ``` 467 | --- 468 | ### 36. How do you convert a dictionary to a list of key-value tuples in Python? 469 | **Answer:** 470 | You can convert a dictionary to a list of key-value tuples using the `items()` method. 471 | ```python 472 | def dict_to_list_of_tuples(d): 473 | return list(d.items()) 474 | d = {'a': 1, 'b': 2, 'c': 3} 475 | list_of_tuples = dict_to_list_of_tuples(d) 476 | print(list_of_tuples) # Output: [('a', 1), ('b', 2), ('c', 3)] 477 | ``` 478 | --- 479 | ### 37. How do you combine two dictionaries with a function applied to common keys in Python? 480 | **Answer:** 481 | You can combine two dictionaries and apply a function to common keys using a dictionary comprehension. 482 | ```python 483 | def combine_dicts_with_function(dict1, dict2, func): 484 | combined = {k: func(dict1[k], dict2[k]) for k in dict1 if k in dict2} 485 | combined.update({k: dict1[k] for k in dict1 if k not in dict2}) 486 | combined.update({k: dict2[k] for k in dict2 if k not in dict1}) 487 | return combined 488 | dict1 = {'a': 1, 'b': 2, 'c': 3} 489 | dict2 = {'b': 3, 'c': 4, 'd': 5} 490 | combined_dict = combine_dicts_with_function(dict1, dict2, lambda x, y: x + y) 491 | print(combined_dict) # Output: {'a': 1, 'b': 5, 'c': 7, 'd': 5} 492 | ``` 493 | --- 494 | ### 38. How do you find the symmetric difference of two dictionaries in Python? 495 | **Answer:** 496 | You can find the symmetric difference of two dictionaries by keeping keys unique to each dictionary. 497 | ```python 498 | def dict_symmetric_difference(dict1, dict2): 499 | return {k: v for k, v in {**dict1, **dict2}.items() if k not in dict1 or k not in dict2} 500 | dict1 = {'a': 1, 'b': 2, 'c': 3} 501 | dict2 = {'b': 2, 'c': 4, 'd': 5} 502 | sym_diff = dict_symmetric_difference(dict1, dict2) 503 | print(sym_diff) # Output: {'a': 1, 'c': 4, 'd': 5} 504 | ``` 505 | --- 506 | ### 39. How do you flatten a dictionary with lists as values in Python? 507 | **Answer:** 508 | You can flatten a dictionary with lists as values using a dictionary comprehension. 509 | ```python 510 | def flatten_dict_with_lists(d): 511 | return {k: v for k, vals in d.items() for v in vals} 512 | d = {'a': [1, 2], 'b': [3, 4]} 513 | flat_dict = flatten_dict_with_lists(d) 514 | print(flat_dict) # Output: {'a': 2, 'b': 4} 515 | ``` 516 | --- 517 | ### 40. How do you update a nested dictionary in Python? 518 | **Answer:** 519 | You can update a nested dictionary using a recursive function. 520 | ```python 521 | def update_nested_dict(d, updates): 522 | for k, v in updates.items(): 523 | if isinstance(v, dict) and k in d and isinstance(d[k], dict): 524 | update_nested_dict(d[k], v) 525 | else: 526 | d[k] = v 527 | d = {'a': {'b': 1, 'c': 2}, 'd': 3} 528 | updates = {'a': {'b': 10, 'd': 20}, 'e': 4} 529 | update_nested_dict(d, updates) 530 | print(d) # Output: {'a': {'b': 10, 'c': 2, 'd': 20}, 'd': 3, 'e': 4} 531 | ``` 532 | --- 533 | ### 41. How do you find the most common value in a dictionary in Python? 534 | **Answer:** 535 | You can find the most common value using the `Counter` from the `collections` module. 536 | ```python 537 | from collections import Counter 538 | def most_common_value(d): 539 | counter = Counter(d.values()) 540 | return counter.most_common(1)[0][0] 541 | d = {'a': 1, 'b': 2, 'c': 1, 'd': 3} 542 | common_value = most_common_value(d) 543 | print(common_value) # Output: 1 544 | ``` 545 | --- 546 | ### 42. How do you create a dictionary from a list of keys with the same value in Python? 547 | **Answer:** 548 | You can create a dictionary from a list of keys with the same value using a dictionary comprehension. 549 | ```python 550 | def create_dict_with_same_value(keys, value): 551 | return {k: value for k in keys} 552 | keys = ['a', 'b', 'c'] 553 | value = 0 554 | d = create_dict_with_same_value(keys, value) 555 | print(d) # Output: {'a': 0, 'b': 0, 'c': 0} 556 | ``` 557 | --- 558 | ### 43. How do you remove keys with a specific value from a dictionary in Python? 559 | **Answer:** 560 | You can remove keys with a specific value using a dictionary comprehension. 561 | ```python 562 | def remove_keys_with_value(d, value): 563 | return {k: v for k, v in d.items() if v != value} 564 | d = {'a': 1, 'b': 2, 'c': 1, 'd': 3} 565 | cleaned_d = remove_keys_with_value(d, 1) 566 | print(cleaned_d) # Output: {'b': 2, 'd': 3} 567 | ``` 568 | --- 569 | ### 44. How do you find the sum of all values in a dictionary in Python? 570 | **Answer:** 571 | You can find the sum of all values using the `sum()` function. 572 | ```python 573 | def sum_dict_values(d): 574 | return sum(d.values()) 575 | d = {'a': 1, 'b': 2, 'c': 3} 576 | total = sum_dict_values(d) 577 | print(total) # Output: 6 578 | ``` 579 | --- 580 | ### 45. How do you combine dictionaries by concatenating values in Python? 581 | **Answer:** 582 | You can combine dictionaries by concatenating values using a dictionary comprehension. 583 | ```python 584 | def combine_dicts_concatenate(dict1, dict2): 585 | return {k: dict1.get(k, '') + dict2.get(k, '') for k in set(dict1) | set(dict2)} 586 | dict1 = {'a': 'hello', 'b': 'world'} 587 | dict2 = {'b': '!', 'c': 'python'} 588 | combined_dict = combine_dicts_concatenate(dict1, dict2) 589 | print(combined_dict) # Output: {'a': 'hello', 'b': 'world!', 'c': 'python'} 590 | ``` 591 | --- 592 | ### 46. How do you create a dictionary from a list of tuples in Python? 593 | **Answer:** 594 | You can create a dictionary from a list of tuples using the `dict()` constructor. 595 | ```python 596 | def tuples_to_dict(tuples): 597 | return dict(tuples) 598 | tuples = [('a', 1), ('b', 2 599 | ), ('c', 3)] 600 | d = tuples_to_dict(tuples) 601 | print(d) # Output: {'a': 1, 'b': 2, 'c': 3} 602 | ``` 603 | --- 604 | ### 47. How do you find the difference in values of common keys between two dictionaries in Python? 605 | **Answer:** 606 | You can find the difference in values of common keys using a dictionary comprehension. 607 | ```python 608 | def dict_value_difference(dict1, dict2): 609 | return {k: abs(dict1[k] - dict2[k]) for k in dict1 if k in dict2} 610 | dict1 = {'a': 1, 'b': 3, 'c': 5} 611 | dict2 = {'b': 1, 'c': 4, 'd': 7} 612 | difference = dict_value_difference(dict1, dict2) 613 | print(difference) # Output: {'b': 2, 'c': 1} 614 | ``` 615 | --- 616 | ### 48. How do you create a dictionary of sets in Python? 617 | **Answer:** 618 | You can create a dictionary of sets using `defaultdict` from the `collections` module. 619 | ```python 620 | from collections import defaultdict 621 | def create_dict_of_sets(pairs): 622 | d = defaultdict(set) 623 | for k, v in pairs: 624 | d[k].add(v) 625 | return d 626 | pairs = [('a', 1), ('b', 2), ('a', 3), ('b', 4)] 627 | dict_of_sets = create_dict_of_sets(pairs) 628 | print(dict_of_sets) # Output: {'a': {1, 3}, 'b': {2, 4}} 629 | ``` 630 | --- 631 | ### 49. How do you get the length of a dictionary in Python? 632 | **Answer:** 633 | You can get the length of a dictionary using the `len()` function. 634 | ```python 635 | def dict_length(d): 636 | return len(d) 637 | d = {'a': 1, 'b': 2, 'c': 3} 638 | length = dict_length(d) 639 | print(length) # Output: 3 640 | ``` 641 | --- 642 | ### 50. How do you map a function over dictionary values in Python? 643 | **Answer:** 644 | You can map a function over dictionary values using a dictionary comprehension. 645 | ```python 646 | def map_function_over_values(d, func): 647 | return {k: func(v) for k, v in d.items()} 648 | d = {'a': 1, 'b': 2, 'c': 3} 649 | mapped_d = map_function_over_values(d, lambda x: x * 2) 650 | print(mapped_d) # Output: {'a': 2, 'b': 4, 'c': 6} 651 | ``` 652 | 653 | --- 654 | If you found this repository helpful, please give it a star! 655 | 656 | Follow me on: 657 | - [LinkedIn](https://www.linkedin.com/in/ashish-jangra/) 658 | - [GitHub](https://github.com/AshishJangra27) 659 | - [Kaggle](https://www.kaggle.com/ashishjangra27) 660 | 661 | Stay updated with my latest content and projects! 662 | -------------------------------------------------------------------------------- /Excel for Data Analytics.md: -------------------------------------------------------------------------------- 1 | # 100 Excel Interview Questions for Data Analytics 2 | 3 | ### Basic to Intermediate Questions 4 | 5 | #### 1. What is the difference between an Excel workbook and a worksheet? 6 | **Answer:** 7 | A workbook is an Excel file that contains one or more worksheets. A worksheet is a single spreadsheet within a workbook where data is entered and analyzed. 8 | 9 | --- 10 | 11 | #### 2. How do you use the VLOOKUP function in Excel? 12 | **Answer:** 13 | The VLOOKUP function searches for a value in the first column of a table and returns a value in the same row from another column. 14 | Example: 15 | ``` 16 | =VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup]) 17 | ``` 18 | 19 | --- 20 | 21 | #### 3. How do you use the IF function in Excel? 22 | **Answer:** 23 | The IF function returns one value if a condition is true and another value if it's false. 24 | Example: 25 | ``` 26 | =IF(condition, value_if_true, value_if_false) 27 | ``` 28 | 29 | --- 30 | 31 | #### 4. What is conditional formatting and how is it used in Excel? 32 | **Answer:** 33 | Conditional formatting allows you to apply specific formatting to cells that meet certain criteria. It's used to highlight important information, trends, and outliers in data. 34 | 35 | --- 36 | 37 | #### 5. How do you use the INDEX and MATCH functions together? 38 | **Answer:** 39 | The INDEX function returns the value of a cell at a given position in a range. The MATCH function returns the relative position of a value in a range. Together, they can be used as an alternative to VLOOKUP. 40 | Example: 41 | ``` 42 | =INDEX(range, MATCH(lookup_value, lookup_range, match_type)) 43 | ``` 44 | 45 | --- 46 | 47 | #### 6. How do you use the SUMIF function in Excel? 48 | **Answer:** 49 | The SUMIF function adds all numbers in a range that meet a specified condition. 50 | Example: 51 | ``` 52 | =SUMIF(range, criteria, [sum_range]) 53 | ``` 54 | 55 | --- 56 | 57 | #### 7. What is the purpose of the CONCATENATE function? 58 | **Answer:** 59 | The CONCATENATE function joins two or more text strings into one string. 60 | Example: 61 | ``` 62 | =CONCATENATE(text1, text2, ...) 63 | ``` 64 | 65 | --- 66 | 67 | #### 8. How do you use the TEXT function in Excel? 68 | **Answer:** 69 | The TEXT function converts a value to text in a specified number format. 70 | Example: 71 | ``` 72 | =TEXT(value, format_text) 73 | ``` 74 | 75 | --- 76 | 77 | #### 9. Explain the use of the SUMPRODUCT function. 78 | **Answer:** 79 | The SUMPRODUCT function multiplies corresponding components in given arrays and returns the sum of those products. 80 | Example: 81 | ``` 82 | =SUMPRODUCT(array1, array2, ...) 83 | ``` 84 | 85 | --- 86 | 87 | #### 10. How do you use the HLOOKUP function? 88 | **Answer:** 89 | The HLOOKUP function searches for a value in the top row of a table and returns a value in the same column from another row. 90 | Example: 91 | ``` 92 | =HLOOKUP(lookup_value, table_array, row_index_num, [range_lookup]) 93 | ``` 94 | 95 | --- 96 | 97 | #### 11. How do you use the COUNTIF function? 98 | **Answer:** 99 | The COUNTIF function counts the number of cells that meet a specified condition. 100 | Example: 101 | ``` 102 | =COUNTIF(range, criteria) 103 | ``` 104 | 105 | --- 106 | 107 | #### 12. What is the purpose of the RANK function? 108 | **Answer:** 109 | The RANK function returns the rank of a number in a list of numbers. 110 | Example: 111 | ``` 112 | =RANK(number, ref, [order]) 113 | ``` 114 | 115 | --- 116 | 117 | #### 13. How do you use the OFFSET function in Excel? 118 | **Answer:** 119 | The OFFSET function returns a reference to a range that is a specified number of rows and columns from a cell or range of cells. 120 | Example: 121 | ``` 122 | =OFFSET(reference, rows, cols, [height], [width]) 123 | ``` 124 | 125 | --- 126 | 127 | #### 14. Explain the use of the DATEDIF function. 128 | **Answer:** 129 | The DATEDIF function calculates the difference between two dates in years, months, or days. 130 | Example: 131 | ``` 132 | =DATEDIF(start_date, end_date, unit) 133 | ``` 134 | 135 | --- 136 | 137 | #### 15. How do you use the IFERROR function? 138 | **Answer:** 139 | The IFERROR function returns a value if an error is found in a formula, otherwise it returns the result of the formula. 140 | Example: 141 | ``` 142 | =IFERROR(value, value_if_error) 143 | ``` 144 | 145 | --- 146 | 147 | #### 16. What is the purpose of the CLEAN function in Excel? 148 | **Answer:** 149 | The CLEAN function removes all non-printable characters from text. 150 | Example: 151 | ``` 152 | =CLEAN(text) 153 | ``` 154 | 155 | --- 156 | 157 | #### 17. How do you use the FIND and SEARCH functions in Excel? 158 | **Answer:** 159 | The FIND function returns the starting position of a substring within a text string, case-sensitive. The SEARCH function is similar but case-insensitive. 160 | Example: 161 | ``` 162 | =FIND(find_text, within_text, [start_num]) 163 | =SEARCH(find_text, within_text, [start_num]) 164 | ``` 165 | 166 | --- 167 | 168 | #### 18. How do you use the SUBSTITUTE function in Excel? 169 | **Answer:** 170 | The SUBSTITUTE function replaces occurrences of a specified substring within a text string with another substring. 171 | Example: 172 | ``` 173 | =SUBSTITUTE(text, old_text, new_text, [instance_num]) 174 | ``` 175 | 176 | --- 177 | 178 | #### 19. What is the purpose of the LEFT, MID, and RIGHT functions in Excel? 179 | **Answer:** 180 | These functions extract a specified number of characters from a text string. 181 | Example: 182 | ``` 183 | =LEFT(text, [num_chars]) 184 | =MID(text, start_num, num_chars) 185 | =RIGHT(text, [num_chars]) 186 | ``` 187 | 188 | --- 189 | 190 | #### 20. How do you use the LEN function in Excel? 191 | **Answer:** 192 | The LEN function returns the number of characters in a text string. 193 | Example: 194 | ``` 195 | =LEN(text) 196 | ``` 197 | 198 | --- 199 | 200 | #### 21. What is the use of the ROUND, ROUNDUP, and ROUNDDOWN functions? 201 | **Answer:** 202 | These functions round a number to a specified number of digits. 203 | Example: 204 | ``` 205 | =ROUND(number, num_digits) 206 | =ROUNDUP(number, num_digits) 207 | =ROUNDDOWN(number, num_digits) 208 | ``` 209 | 210 | --- 211 | 212 | #### 22. How do you use the LARGE and SMALL functions? 213 | **Answer:** 214 | The LARGE function returns the k-th largest value in a data set, and the SMALL function returns the k-th smallest value. 215 | Example: 216 | ``` 217 | =LARGE(array, k) 218 | =SMALL(array, k) 219 | ``` 220 | 221 | --- 222 | 223 | #### 23. Explain the use of the DATE and TIME functions. 224 | **Answer:** 225 | The DATE function returns the serial number of a date, and the TIME function returns the serial number of a particular time. 226 | Example: 227 | ``` 228 | =DATE(year, month, day) 229 | =TIME(hour, minute, second) 230 | ``` 231 | 232 | --- 233 | 234 | #### 24. How do you use the NETWORKDAYS function? 235 | **Answer:** 236 | The NETWORKDAYS function returns the number of whole working days between two dates. 237 | Example: 238 | ``` 239 | =NETWORKDAYS(start_date, end_date, [holidays]) 240 | ``` 241 | 242 | --- 243 | 244 | #### 25. What is the purpose of the EOMONTH function? 245 | **Answer:** 246 | The EOMONTH function returns the serial number for the last day of the month a specified number of months before or after a date. 247 | Example: 248 | ``` 249 | =EOMONTH(start_date, months) 250 | ``` 251 | 252 | --- 253 | 254 | #### 26. How do you use the INDIRECT function in Excel? 255 | **Answer:** 256 | The INDIRECT function returns the reference specified by a text string. It can be used to create dynamic ranges and references. 257 | Example: 258 | ``` 259 | =INDIRECT(ref_text, [a1]) 260 | ``` 261 | 262 | --- 263 | 264 | #### 27. Explain the use of the PMT function. 265 | **Answer:** 266 | The PMT function calculates the payment for a loan based on constant payments and a constant interest rate. 267 | Example: 268 | ``` 269 | =PMT(rate, nper, pv, [fv], [type]) 270 | ``` 271 | 272 | --- 273 | 274 | #### 28. How do you use array formulas in Excel? 275 | **Answer:** 276 | Array formulas perform multiple calculations on one or more items in an array. To enter an array formula, press Ctrl+Shift+Enter. 277 | Example: 278 | ``` 279 | {=SUM(A1:A10*B1:B10)} 280 | ``` 281 | 282 | --- 283 | 284 | #### 29. How do you use the AGGREGATE function in Excel? 285 | **Answer:** 286 | The AGGREGATE function performs various calculations (like SUM, AVERAGE, COUNT) while ignoring errors and hidden rows. 287 | Example: 288 | ``` 289 | =AGGREGATE(function_num, options, array, [k]) 290 | ``` 291 | 292 | --- 293 | 294 | #### 30. How do you use the XLOOKUP function? 295 | **Answer:** 296 | The XLOOKUP function searches a range or array, and returns an item corresponding to the first match it finds. If a match doesn’t exist, then XLOOKUP can return the closest (approximate) match. 297 | Example: 298 | ``` 299 | =XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode]) 300 | ``` 301 | 302 | --- 303 | 304 | ### Intermediate to Advanced Questions 305 | 306 | Here is a sample table for the following questions: 307 | 308 | | Product | Region | Sales | Date | Category | Discount | Quantity | Cost | Revenue | Profit | 309 | |---------|--------|-------|------------|-----------|----------|----------|--------|---------|--------| 310 | | A | North | 5000 | 2024-01-01 | Electronics| 10% | 100 | 4500 | 5500 | 1000 | 311 | | B | South | 7000 | 2024-01-05 | Furniture | 5% | 150 | 6000 | 7500 | 1500 | 312 | | C | East | 4000 | 2024-01-10 | Clothing | 20% | 200 | 3000 | 5000 | 2000 | 313 | | D | West | 6000 | 2024-01-15 | Electronics| 15% | 120 | 5000 | 7000 | 2000 | 314 | | E | North | 8000 | 2024-01-20 | Furniture | 10% | 180 | 7000 | 9000 | 2000 | 315 | 316 | #### 31. How do you calculate the total revenue using the sample table above? 317 | **Answer:** 318 | To calculate the total revenue, use the SUM function. 319 | Example: 320 | ``` 321 | =SUM(I2:I6) 322 | ``` 323 | 324 | --- 325 | 326 | #### 32. How do you find the product with the highest sales using the sample table? 327 | **Answer:** 328 | Use the INDEX and MATCH functions together. 329 | Example: 330 | ``` 331 | =INDEX(A2:A6, MATCH(MAX(C2:C6), C2:C6, 0)) 332 | ``` 333 | 334 | --- 335 | 336 | #### 33. How do you calculate the average discount offered in the sample table? 337 | **Answer:** 338 | Use the AVERAGE function. 339 | Example: 340 | ``` 341 | =AVERAGE(F2:F6) 342 | ``` 343 | 344 | --- 345 | 346 | #### 34. How do you count the number of products sold in the North region using the sample table? 347 | **Answer:** 348 | Use the COUNTIF function. 349 | Example: 350 | ``` 351 | =COUNTIF(B2:B6, "North") 352 | ``` 353 | 354 | --- 355 | 356 | #### 35. How do you calculate the total profit for Electronics category using the sample table? 357 | **Answer:** 358 | Use the SUMIF function. 359 | Example: 360 | ``` 361 | =SUMIF(E2:E6, "Electronics", J2:J6) 362 | ``` 363 | 364 | --- 365 | 366 | #### 36. How do you use the PIVOT function to summarize sales data by region? 367 | **Answer:** 368 | Use the PivotTable feature to create a summary of sales by region. 369 | Example: 370 | 1. Select the table range. 371 | 2. Go to Insert > PivotTable. 372 | 3. Drag "Region" to the Rows area and "Sales" to the Values area. 373 | 374 | --- 375 | 376 | #### 37. How do you calculate the revenue per unit sold for each product using the sample table? 377 | **Answer:** 378 | Divide the Revenue by the Quantity. 379 | Example: 380 | ``` 381 | =I2/G2 382 | ``` 383 | 384 | --- 385 | 386 | #### 38. How do you calculate the percentage of total sales each product contributes using the sample table? 387 | **Answer:** 388 | Divide each product's sales by the total sales and format as a percentage. 389 | Example: 390 | ``` 391 | =C2/SUM(C2:C6) 392 | ``` 393 | 394 | --- 395 | 396 | #### 39. How do you identify the region with the highest total profit using the sample table? 397 | **Answer:** 398 | Use a PivotTable to sum the profit by region and identify the highest value. 399 | Example: 400 | 1. Select the table range. 401 | 2. Go to Insert > PivotTable. 402 | 3. Drag "Region" to the Rows area and "Profit" to the Values area. 403 | 404 | --- 405 | 406 | #### 40. How do you use the INDEX and MATCH functions to find the sales of a specific product based on its name in the sample table? 407 | **Answer:** 408 | Use INDEX and MATCH functions to return sales for a given product. 409 | Example: 410 | ``` 411 | =INDEX(C2:C6, MATCH("B", A2:A6, 0)) 412 | ``` 413 | 414 | --- 415 | 416 | #### 41. How do you use array formulas to calculate the total sales for each category using the sample table? 417 | **Answer:** 418 | Use SUMPRODUCT function with array formulas. 419 | Example: 420 | ``` 421 | =SUMPRODUCT((E2:E6="Electronics")*(C2:C6)) 422 | ``` 423 | 424 | --- 425 | 426 | #### 42. How do you use the OFFSET function to create a dynamic named range for the Sales column in the sample table? 427 | **Answer:** 428 | Use OFFSET and COUNTA functions. 429 | Example: 430 | ``` 431 | =OFFSET($C$2,0,0,COUNTA($C:$C)-1,1) 432 | ``` 433 | 434 | --- 435 | 436 | #### 43. How do you calculate the cumulative sales for each product in the sample table? 437 | **Answer:** 438 | Use the SUM function with relative references. 439 | Example: 440 | ``` 441 | =SUM($C$2:C2) 442 | ``` 443 | 444 | --- 445 | 446 | #### 44. How do you use the RANK function to rank the products by sales in the sample table? 447 | **Answer:** 448 | Use the RANK function. 449 | Example: 450 | ``` 451 | =RANK(C2, $C$2:$C$6) 452 | ``` 453 | 454 | --- 455 | 456 | #### 45. How do you use the TEXT function to format the sales figures as currency in the sample table? 457 | **Answer:** 458 | Use the TEXT function. 459 | Example: 460 | ``` 461 | =TEXT(C2, "$#,##0.00") 462 | ``` 463 | 464 | --- 465 | 466 | #### 46. How do you calculate the total quantity sold for each category using the sample table? 467 | **Answer:** 468 | Use the SUMIF function. 469 | Example: 470 | ``` 471 | =SUMIF(E2:E6, "Electronics", G2:G6) 472 | ``` 473 | 474 | --- 475 | 476 | #### 47. How do you find the date with the highest sales using the sample table? 477 | **Answer:** 478 | Use the INDEX and MATCH functions. 479 | Example: 480 | ``` 481 | =INDEX(D2:D6, MATCH(MAX(C2:C6), C2:C6, 0)) 482 | ``` 483 | 484 | --- 485 | 486 | #### 48. How do you use the CONCATENATE function to create a unique identifier for each row in the sample table? 487 | **Answer:** 488 | Use the CONCATENATE function. 489 | Example: 490 | ``` 491 | =CONCATENATE(A2, B2, D2) 492 | ``` 493 | 494 | --- 495 | 496 | #### 49. How do you calculate the average profit per sale in the sample table? 497 | **Answer:** 498 | Use the AVERAGE function. 499 | Example: 500 | ``` 501 | =AVERAGE(J2:J6) 502 | ``` 503 | 504 | --- 505 | 506 | #### 50. How do you create a dynamic chart range using the sample table? 507 | **Answer:** 508 | Use named ranges with OFFSET and COUNTA functions. 509 | Example: 510 | ``` 511 | =OFFSET($C$2, 0, 0, COUNTA($C:$C)-1, 1) 512 | ``` 513 | 514 | --- 515 | 516 | #### 51. How do you use the DSUM function to calculate the total sales for products sold in January in the sample table? 517 | **Answer:** 518 | Use the DSUM function. 519 | Example: 520 | ``` 521 | =DSUM(A1:J6, "Sales", L1:L2) 522 | ``` 523 | 524 | --- 525 | 526 | #### 52. How do you use the DCOUNT function to count the number of products with sales greater than 5000 in the sample table? 527 | **Answer:** 528 | Use the DCOUNT function. 529 | Example: 530 | ``` 531 | =DCOUNT(A1:J6, "Sales", L1:L2) 532 | ``` 533 | 534 | --- 535 | 536 | #### 53. How do you use the DAVERAGE function to calculate the average profit for products in the Electronics category in the sample table? 537 | **Answer:** 538 | Use the DAVERAGE function. 539 | Example: 540 | ``` 541 | =DAVERAGE(A1:J6, "Profit", L1:L2) 542 | ``` 543 | 544 | --- 545 | 546 | #### 54. How do you use the DMIN function to find the minimum discount offered in the sample table? 547 | **Answer:** 548 | Use the DMIN function. 549 | Example: 550 | ``` 551 | =DMIN(A1:J6, "Discount", L1:L2) 552 | ``` 553 | 554 | --- 555 | 556 | #### 55. How do you use the DMAX function to find the maximum revenue generated in the sample table? 557 | **Answer:** 558 | Use the DMAX function. 559 | Example: 560 | ``` 561 | =DMAX(A1:J6, "Revenue", L1:L2) 562 | ``` 563 | 564 | --- 565 | 566 | #### 56. How do you use the DVAR function to calculate the variance in sales for the sample table? 567 | **Answer:** 568 | Use the DVAR function. 569 | Example: 570 | ``` 571 | =DVAR(A1:J6, "Sales", L1:L2) 572 | ``` 573 | 574 | --- 575 | 576 | #### 57. How do you use the DSTDEV function to calculate the standard deviation of profits in the sample table? 577 | **Answer:** 578 | Use the DSTDEV function. 579 | Example: 580 | ``` 581 | =DSTDEV(A1:J6, "Profit", L1:L2) 582 | ``` 583 | 584 | --- 585 | 586 | #### 58. How do you use the ISNUMBER function to check if the values in the Sales column of the sample table are numbers? 587 | **Answer:** 588 | Use the ISNUMBER function. 589 | Example: 590 | ``` 591 | =ISNUMBER(C2) 592 | ``` 593 | 594 | --- 595 | 596 | #### 59. How do you use the ISERR function to check if there are any errors in the Profit column of the sample table? 597 | **Answer:** 598 | Use the ISERR function. 599 | Example: 600 | ``` 601 | =ISERR(J2) 602 | ``` 603 | 604 | --- 605 | 606 | #### 60. How do you use the ISERROR function to check for errors in the Discount column of the sample table? 607 | **Answer:** 608 | Use the ISERROR function. 609 | Example: 610 | ``` 611 | =ISERROR(F2) 612 | ``` 613 | 614 | --- 615 | 616 | #### 61. How do you use the IFERROR function to replace error values in the Cost column with 0 in the sample table? 617 | **Answer:** 618 | Use the IFERROR function. 619 | Example: 620 | ``` 621 | =IFERROR(H2, 0) 622 | ``` 623 | 624 | --- 625 | 626 | #### 62. How do you use the IF function to categorize sales as "High" or "Low" based on a threshold value in the sample table? 627 | **Answer:** 628 | Use the IF function. 629 | Example: 630 | ``` 631 | =IF(C2 > 6000, "High", "Low") 632 | ``` 633 | 634 | --- 635 | 636 | #### 63. How do you use the MATCH function to find the position of the product "C" in the sample table? 637 | **Answer:** 638 | Use the MATCH function. 639 | Example: 640 | ``` 641 | =MATCH("C", A2:A6, 0) 642 | ``` 643 | 644 | --- 645 | 646 | #### 64. How do you use the INDEX function to return the quantity sold for the product "D" in the sample table? 647 | **Answer:** 648 | Use the INDEX function. 649 | Example: 650 | ``` 651 | =INDEX(G2:G6, MATCH("D", A2:A6, 0)) 652 | ``` 653 | 654 | --- 655 | 656 | #### 65. How do you use the SUMIFS function to calculate the total revenue for the North region and Electronics category in the sample table? 657 | **Answer:** 658 | Use the SUMIFS function. 659 | Example: 660 | ``` 661 | =SUMIFS(I2:I6, B2:B6, "North", E2:E6, "Electronics") 662 | ``` 663 | 664 | --- 665 | 666 | #### 66. How do you use the COUNTIFS function to count the number of products sold in the South region with sales greater than 6000 in the sample table? 667 | **Answer:** 668 | Use the COUNTIFS function. 669 | Example: 670 | ``` 671 | =COUNTIFS(B2:B6, "South", C2:C6, ">6000") 672 | ``` 673 | 674 | --- 675 | 676 | #### 67. How do you calculate the total 677 | 678 | cost for products sold in the East region using the sample table? 679 | **Answer:** 680 | Use the SUMIF function. 681 | Example: 682 | ``` 683 | =SUMIF(B2:B6, "East", H2:H6) 684 | ``` 685 | 686 | --- 687 | 688 | #### 68. How do you use the MAXIFS function to find the maximum sales value for the Furniture category in the sample table? 689 | **Answer:** 690 | Use the MAXIFS function. 691 | Example: 692 | ``` 693 | =MAXIFS(C2:C6, E2:E6, "Furniture") 694 | ``` 695 | 696 | --- 697 | 698 | #### 69. How do you calculate the total discount given across all products using the sample table? 699 | **Answer:** 700 | Convert the Discount column to numerical values and then sum it up. 701 | Example: 702 | ``` 703 | =SUMPRODUCT(C2:C6, SUBSTITUTE(F2:F6, "%", "")/100) 704 | ``` 705 | 706 | --- 707 | 708 | #### 70. How do you use the MEDIAN function to find the median sales value in the sample table? 709 | **Answer:** 710 | Use the MEDIAN function. 711 | Example: 712 | ``` 713 | =MEDIAN(C2:C6) 714 | ``` 715 | 716 | --- 717 | 718 | #### 71. How do you calculate the total profit margin for each product using the sample table? 719 | **Answer:** 720 | Divide Profit by Revenue. 721 | Example: 722 | ``` 723 | =J2/I2 724 | ``` 725 | 726 | --- 727 | 728 | #### 72. How do you use the FREQUENCY function to calculate the frequency distribution of sales values in the sample table? 729 | **Answer:** 730 | Use the FREQUENCY function. 731 | Example: 732 | ``` 733 | =FREQUENCY(C2:C6, {4000, 6000, 8000}) 734 | ``` 735 | 736 | --- 737 | 738 | #### 73. How do you use the TREND function to predict future sales based on historical data in the sample table? 739 | **Answer:** 740 | Use the TREND function. 741 | Example: 742 | ``` 743 | =TREND(C2:C6, {1, 2, 3, 4, 5}, {6}) 744 | ``` 745 | 746 | --- 747 | 748 | #### 74. How do you use the MODE function to find the most frequently occurring sales value in the sample table? 749 | **Answer:** 750 | Use the MODE function. 751 | Example: 752 | ``` 753 | =MODE(C2:C6) 754 | ``` 755 | 756 | --- 757 | 758 | #### 75. How do you calculate the year-over-year growth rate for sales using the sample table? 759 | **Answer:** 760 | Use the formula for growth rate. 761 | Example: 762 | ``` 763 | =(C5-C2)/C2 764 | ``` 765 | 766 | --- 767 | 768 | #### 76. How do you use the PERCENTILE function to find the 90th percentile of sales in the sample table? 769 | **Answer:** 770 | Use the PERCENTILE function. 771 | Example: 772 | ``` 773 | =PERCENTILE(C2:C6, 0.9) 774 | ``` 775 | 776 | --- 777 | 778 | #### 77. How do you use the QUARTILE function to find the first quartile of sales in the sample table? 779 | **Answer:** 780 | Use the QUARTILE function. 781 | Example: 782 | ``` 783 | =QUARTILE(C2:C6, 1) 784 | ``` 785 | 786 | --- 787 | 788 | #### 78. How do you use the VAR.S function to calculate the sample variance of sales in the sample table? 789 | **Answer:** 790 | Use the VAR.S function. 791 | Example: 792 | ``` 793 | =VAR.S(C2:C6) 794 | ``` 795 | 796 | --- 797 | 798 | #### 79. How do you use the VAR.P function to calculate the population variance of sales in the sample table? 799 | **Answer:** 800 | Use the VAR.P function. 801 | Example: 802 | ``` 803 | =VAR.P(C2:C6) 804 | ``` 805 | 806 | --- 807 | 808 | #### 80. How do you use the STDEV.S function to calculate the sample standard deviation of sales in the sample table? 809 | **Answer:** 810 | Use the STDEV.S function. 811 | Example: 812 | ``` 813 | =STDEV.S(C2:C6) 814 | ``` 815 | 816 | --- 817 | 818 | #### 81. How do you use the STDEV.P function to calculate the population standard deviation of sales in the sample table? 819 | **Answer:** 820 | Use the STDEV.P function. 821 | Example: 822 | ``` 823 | =STDEV.P(C2:C6) 824 | ``` 825 | 826 | --- 827 | 828 | #### 82. How do you use the CORREL function to find the correlation between sales and revenue in the sample table? 829 | **Answer:** 830 | Use the CORREL function. 831 | Example: 832 | ``` 833 | =CORREL(C2:C6, I2:I6) 834 | ``` 835 | 836 | --- 837 | 838 | #### 83. How do you use the COVARIANCE.S function to find the sample covariance between sales and profit in the sample table? 839 | **Answer:** 840 | Use the COVARIANCE.S function. 841 | Example: 842 | ``` 843 | =COVARIANCE.S(C2:C6, J2:J6) 844 | ``` 845 | 846 | --- 847 | 848 | #### 84. How do you use the COVARIANCE.P function to find the population covariance between sales and profit in the sample table? 849 | **Answer:** 850 | Use the COVARIANCE.P function. 851 | Example: 852 | ``` 853 | =COVARIANCE.P(C2:C6, J2:J6) 854 | ``` 855 | 856 | --- 857 | 858 | #### 85. How do you use the FORECAST function to predict future sales based on historical data in the sample table? 859 | **Answer:** 860 | Use the FORECAST function. 861 | Example: 862 | ``` 863 | =FORECAST(6, C2:C6, {1, 2, 3, 4, 5}) 864 | ``` 865 | 866 | --- 867 | 868 | #### 86. How do you use the LINEST function to perform a linear regression analysis on the sample table data? 869 | **Answer:** 870 | Use the LINEST function. 871 | Example: 872 | ``` 873 | =LINEST(C2:C6, {1, 2, 3, 4, 5}) 874 | ``` 875 | 876 | --- 877 | 878 | #### 87. How do you use the LOGEST function to perform an exponential regression analysis on the sample table data? 879 | **Answer:** 880 | Use the LOGEST function. 881 | Example: 882 | ``` 883 | =LOGEST(C2:C6, {1, 2, 3, 4, 5}) 884 | ``` 885 | 886 | --- 887 | 888 | #### 88. How do you use the EXPON.DIST function to find the exponential distribution of sales in the sample table? 889 | **Answer:** 890 | Use the EXPON.DIST function. 891 | Example: 892 | ``` 893 | =EXPON.DIST(C2, 1/AVERAGE(C2:C6), TRUE) 894 | ``` 895 | 896 | --- 897 | 898 | #### 89. How do you use the NORM.DIST function to find the normal distribution of sales in the sample table? 899 | **Answer:** 900 | Use the NORM.DIST function. 901 | Example: 902 | ``` 903 | =NORM.DIST(C2, AVERAGE(C2:C6), STDEV.P(C2:C6), TRUE) 904 | ``` 905 | 906 | --- 907 | 908 | #### 90. How do you use the NORM.INV function to find the inverse normal distribution of sales in the sample table? 909 | **Answer:** 910 | Use the NORM.INV function. 911 | Example: 912 | ``` 913 | =NORM.INV(0.95, AVERAGE(C2:C6), STDEV.P(C2:C6)) 914 | ``` 915 | 916 | --- 917 | 918 | #### 91. How do you use the BINOM.DIST function to find the binomial distribution of sales in the sample table? 919 | **Answer:** 920 | Use the BINOM.DIST function. 921 | Example: 922 | ``` 923 | =BINOM.DIST(3, 5, 0.5, TRUE) 924 | ``` 925 | 926 | --- 927 | 928 | #### 92. How do you use the BINOM.INV function to find the inverse binomial distribution of sales in the sample table? 929 | **Answer:** 930 | Use the BINOM.INV function. 931 | Example: 932 | ``` 933 | =BINOM.INV(5, 0.5, 0.95) 934 | ``` 935 | 936 | --- 937 | 938 | #### 93. How do you use the HYPGEOM.DIST function to find the hypergeometric distribution of sales in the sample table? 939 | **Answer:** 940 | Use the HYPGEOM.DIST function. 941 | Example: 942 | ``` 943 | =HYPGEOM.DIST(3, 5, 2, 4, TRUE) 944 | ``` 945 | 946 | --- 947 | 948 | #### 94. How do you use the POISSON.DIST function to find the Poisson distribution of sales in the sample table? 949 | **Answer:** 950 | Use the POISSON.DIST function. 951 | Example: 952 | ``` 953 | =POISSON.DIST(C2, AVERAGE(C2:C6), TRUE) 954 | ``` 955 | 956 | --- 957 | 958 | #### 95. How do you use the WEIBULL.DIST function to find the Weibull distribution of sales in the sample table? 959 | **Answer:** 960 | Use the WEIBULL.DIST function. 961 | Example: 962 | ``` 963 | =WEIBULL.DIST(C2, 1, AVERAGE(C2:C6), TRUE) 964 | ``` 965 | 966 | --- 967 | 968 | #### 96. How do you use the GAMMA.DIST function to find the gamma distribution of sales in the sample table? 969 | **Answer:** 970 | Use the GAMMA.DIST function. 971 | Example: 972 | ``` 973 | =GAMMA.DIST(C2, 2, AVERAGE(C2:C6)/2, TRUE) 974 | ``` 975 | 976 | --- 977 | 978 | #### 97. How do you use the CHISQ.DIST function to find the chi-squared distribution of sales in the sample table? 979 | **Answer:** 980 | Use the CHISQ.DIST function. 981 | Example: 982 | ``` 983 | =CHISQ.DIST(C2, 2, TRUE) 984 | ``` 985 | 986 | --- 987 | 988 | #### 98. How do you use the T.DIST function to find the Student's t-distribution of sales in the sample table? 989 | **Answer:** 990 | Use the T.DIST function. 991 | Example: 992 | ``` 993 | =T.DIST(C2, 4, TRUE) 994 | ``` 995 | 996 | --- 997 | 998 | #### 99. How do you use the F.DIST function to find the F-distribution of sales in the sample table? 999 | **Answer:** 1000 | Use the F.DIST function. 1001 | Example: 1002 | ``` 1003 | =F.DIST(C2, 2, 2, TRUE) 1004 | ``` 1005 | 1006 | --- 1007 | 1008 | #### 100. How do you use the Z.TEST function to perform a one-sample z-test on the sample table data? 1009 | **Answer:** 1010 | Use the Z.TEST function. 1011 | Example: 1012 | ``` 1013 | =Z.TEST(C2:C6, AVERAGE(C2:C6), STDEV.P(C2:C6)) 1014 | ``` 1015 | -------------------------------------------------------------------------------- /Machine Learning.md: -------------------------------------------------------------------------------- 1 | # 100 Machine Learning Interview Questions and Answers (Basic to Advanced) 2 | 3 | Below are 100 Machine Learning interview questions spanning from fundamental concepts to advanced techniques. Each answer now includes more detailed explanations and reasoning to provide deeper insights into each topic. 4 | 5 | --- 6 | 7 | ### 1. What is Machine Learning? 8 | **Answer:** 9 | Machine Learning (ML) is a subset of artificial intelligence where computers learn patterns from data without being explicitly programmed. Instead of hand-crafting rules, we feed algorithms large amounts of examples from which they infer generalizable patterns. This allows ML systems to improve their performance on a task, such as classification or prediction, as they gain more experience (data). 10 | 11 | ### 2. What are the three main types of Machine Learning? 12 | **Answer:** 13 | 1. **Supervised Learning:** Involves labeled data where the target outcome (label) is known. Models learn a mapping from inputs (features) to outputs (labels). Examples: classification (spam detection) and regression (price prediction). 14 | 15 | 2. **Unsupervised Learning:** Works with unlabeled data, finding hidden structures or patterns, like grouping similar items (clustering) or reducing dimensionality. Examples: customer segmentation using K-means clustering. 16 | 17 | 3. **Reinforcement Learning:** An agent interacts with an environment, receiving rewards or penalties. Over time, it learns a policy that maximizes cumulative reward (e.g., a game-playing agent improving its strategy over many rounds). 18 | 19 | ### 3. What is the difference between supervised and unsupervised learning? 20 | **Answer:** 21 | - **Supervised Learning:** Uses labeled data, meaning we know the correct answers for training examples. The model’s goal is to learn a function that accurately maps inputs to the known outputs, such as predicting a house’s price based on features. 22 | 23 | - **Unsupervised Learning:** Deals with unlabeled data, where no predefined targets exist. The model tries to discover underlying patterns or structures in the input features alone, for instance grouping similar images or detecting anomalies. 24 | 25 | ### 4. Define overfitting. 26 | **Answer:** 27 | Overfitting occurs when a model learns not only the general patterns but also the random noise or fluctuations in the training data. Such a model performs extremely well on training data but fails to generalize to unseen data, leading to poor performance in the real world. Overfitting is often indicated by a large gap between high training accuracy and low test accuracy. 28 | 29 | ### 5. How can you prevent overfitting? 30 | **Answer:** 31 | Prevention methods include: 32 | - **Regularization (L1, L2):** Adding a penalty to large coefficients encourages simpler models. 33 | - **Early Stopping:** Halting training when validation performance stops improving. 34 | - **Data Augmentation:** Increasing the training set size or diversity. 35 | - **Cross-Validation:** Ensuring robust estimates of model performance. 36 | - **Model Simplification:** Using fewer parameters or simpler models. 37 | - **Ensembling:** Combining multiple models to smooth out irregularities. 38 | 39 | ### 6. What is underfitting? 40 | **Answer:** 41 | Underfitting happens when a model is too simple or constrained and fails to capture the underlying trends in the data. It results in poor performance on both training and test sets. For example, using a linear model for a highly nonlinear problem can cause underfitting. 42 | 43 | ### 7. Explain the bias-variance trade-off. 44 | **Answer:** 45 | - **Bias:** Error from simplifying assumptions in the model; high-bias models are too rigid and underfit. 46 | - **Variance:** Error from sensitivity to small data fluctuations; high-variance models overfit. 47 | 48 | The trade-off involves balancing these two. A model with too high bias underfits, while too high variance overfits. Achieving a good compromise results in better generalization. 49 | 50 | ### 8. What is a training set, validation set, and test set? 51 | **Answer:** 52 | - **Training Set:** The model learns from this data, adjusting parameters to minimize errors. 53 | - **Validation Set:** Used to tune hyperparameters and select the best model variant. It helps prevent overfitting to the training set. 54 | - **Test Set:** A final, unseen dataset used after all tuning is complete to estimate the model’s true generalization performance. 55 | 56 | ### 9. Why do we use cross-validation? 57 | **Answer:** 58 | Cross-validation splits data into multiple folds, using different folds as validation each time. This yields a more robust and reliable estimate of model performance. It reduces variance in evaluation and helps in optimal model/parameter selection, especially when data is limited. 59 | 60 | ### 10. What is regularization and why is it important? 61 | **Answer:** 62 | Regularization adds a complexity penalty (often on the size of coefficients) to the loss function. It discourages overly complex models, reducing overfitting and enhancing generalization. For example, L2 (Ridge) regularization shrinks coefficients, while L1 (Lasso) promotes sparsity by setting some coefficients to zero. 63 | 64 | ### 11. Explain L1 and L2 regularization. 65 | **Answer:** 66 | - **L1 (Lasso):** Uses the absolute values of weights as a penalty. It tends to produce sparse solutions, effectively performing feature selection by forcing some weights to zero. 67 | 68 | - **L2 (Ridge):** Uses the squared weights as a penalty, spreading out the penalty more smoothly. Ridge does not usually zero out coefficients but shrinks them, stabilizing the solution and reducing variance. 69 | 70 | ### 12. What is logistic regression used for? 71 | **Answer:** 72 | Logistic regression is a supervised classification algorithm modeling the probability of a binary outcome. Instead of predicting continuous values, it predicts class probabilities (e.g., spam vs. not spam). The logistic (sigmoid) function ensures outputs are between 0 and 1, representing probability. 73 | 74 | ### 13. Define a confusion matrix. 75 | **Answer:** 76 | A confusion matrix is a table that visualizes the performance of a classification model by comparing predicted labels against actual labels. It has four main cells: 77 | - **True Positive (TP):** Correct positive predictions. 78 | - **True Negative (TN):** Correct negative predictions. 79 | - **False Positive (FP):** Incorrectly predicted positives. 80 | - **False Negative (FN):** Missed positives (predicted negative but actually positive). 81 | 82 | ### 14. What are precision and recall? 83 | **Answer:** 84 | - **Precision:** Of all predicted positives, how many are truly positive? High precision means few false alarms (FP). 85 | Formula: Precision = TP / (TP + FP) 86 | 87 | - **Recall (Sensitivity):** Of all actual positives, how many did we correctly identify? High recall means catching most of the positives. 88 | Formula: Recall = TP / (TP + FN) 89 | 90 | ### 15. What is the F1-score? 91 | **Answer:** 92 | The F1-score is the harmonic mean of precision and recall. It provides a single metric that balances both, especially useful if there’s class imbalance or if both precision and recall are important. 93 | F1 = 2 * (Precision * Recall) / (Precision + Recall) 94 | 95 | ### 16. What is accuracy, and when is it not a good metric? 96 | **Answer:** 97 | Accuracy = (TP + TN) / (All Samples). It’s the percentage of correct predictions. 98 | However, in imbalanced datasets (e.g., detecting rare diseases), accuracy can be misleading. If 99% of cases are negative, a naive model always predicting negative achieves 99% accuracy but is worthless. In these cases, metrics like F1-score, precision, recall, or AUC are more informative. 99 | 100 | ### 17. What is ROC AUC? 101 | **Answer:** 102 | ROC (Receiver Operating Characteristic) curve plots the True Positive Rate vs. False Positive Rate at various thresholds. AUC (Area Under the Curve) measures how well the model ranks positive instances higher than negatives. An AUC of 1.0 means perfect discrimination, while 0.5 is random guessing. 103 | 104 | ### 18. Define Mean Squared Error (MSE). 105 | **Answer:** 106 | MSE is a regression metric defined as the average of the squared differences between predicted and actual values. By squaring errors, large deviations are penalized heavily. MSE = (1/n) * Σ(actual - predicted)². 107 | 108 | ### 19. What is the purpose of gradient descent? 109 | **Answer:** 110 | Gradient descent is an optimization technique that iteratively adjusts parameters to minimize a loss function. By computing the gradient (slope) of the loss with respect to parameters, it updates parameters in the direction that reduces error, eventually converging to (or near) a minimum. 111 | 112 | ### 20. What is a learning rate in gradient descent? 113 | **Answer:** 114 | The learning rate controls the size of the steps taken when updating parameters. A too large learning rate might overshoot minima and diverge. A too small one leads to very slow convergence. Choosing a good learning rate is crucial for efficient training. 115 | 116 | ### 21. Explain the concept of feature engineering. 117 | **Answer:** 118 | Feature engineering transforms raw data into meaningful representations that improve model performance. It can involve: 119 | - Creating interaction terms (e.g., multiplying features). 120 | - Normalizing or scaling features. 121 | - Using domain knowledge to craft more relevant features. 122 | Better features can simplify learning, increase accuracy, and sometimes reduce the complexity needed in the model. 123 | 124 | ### 22. How do you handle missing data? 125 | **Answer:** 126 | Approaches include: 127 | - Dropping rows or columns with missing values (if few and not critical). 128 | - Imputing using mean, median, or mode. 129 | - Using predictive models to impute missing values. 130 | - Employing algorithms tolerant of missing data. 131 | The choice depends on data quantity, distribution, and the importance of the features. 132 | 133 | ### 23. What is a decision tree? 134 | **Answer:** 135 | A decision tree is a flowchart-like structure of binary decisions. Each node splits the data based on a feature test (e.g., “Is feature X ≤ value?”). Splits aim to produce pure leaf nodes. Decision trees are easy to interpret but prone to overfitting if not pruned or regularized. 136 | 137 | ### 24. What are ensemble methods? 138 | **Answer:** 139 | Ensemble methods combine multiple base models (often called “weak learners”) to achieve better predictive performance. By aggregating diverse predictions, ensembles reduce variance, bias, or both. Examples: Bagging (Random Forest), Boosting (XGBoost), and Stacking. 140 | 141 | ### 25. Explain Random Forest. 142 | **Answer:** 143 | A Random Forest is a bagging ensemble of decision trees. Each tree is trained on a bootstrap sample and a random subset of features, increasing diversity. The final prediction is typically the majority vote for classification or average for regression. This reduces overfitting and improves stability compared to a single decision tree. 144 | 145 | ### 26. What is boosting? 146 | **Answer:** 147 | Boosting trains models sequentially. Each new model focuses on correcting errors from the previous ones, giving more weight to misclassified instances. Over iterations, it creates a strong model from many weak learners, reducing both bias and variance. Examples include AdaBoost and Gradient Boosting. 148 | 149 | ### 27. Explain XGBoost. 150 | **Answer:** 151 | XGBoost (Extreme Gradient Boosting) is a highly optimized gradient boosting library. It improves speed and performance via efficient implementations of tree splitting, uses regularization to avoid overfitting, handles missing data gracefully, and supports parallel computation. It’s popular in many ML competitions for its accuracy and speed. 152 | 153 | ### 28. What is the curse of dimensionality? 154 | **Answer:** 155 | As the number of features (dimensions) grows, data becomes sparse. Models need exponentially more data to achieve the same level of accuracy, making learning difficult. High-dimensional spaces also complicate distance-based methods and can lead to overfitting. 156 | 157 | ### 29. What is PCA (Principal Component Analysis)? 158 | **Answer:** 159 | PCA is a linear dimensionality reduction method that identifies directions (principal components) of maximum variance in the data. By projecting onto a few principal components, PCA reduces noise, speeds up training, and mitigates the curse of dimensionality, while retaining most of the informative structure. 160 | 161 | ### 30. What is LDA (Linear Discriminant Analysis)? 162 | **Answer:** 163 | LDA is both a classification and dimensionality reduction technique. For supervised dimensionality reduction, LDA seeks projections that best separate classes by maximizing between-class variance and minimizing within-class variance. It’s commonly used for tasks where clear class boundaries exist. 164 | 165 | ### 31. How do you select the number of clusters in K-means? 166 | **Answer:** 167 | Techniques include: 168 | - **Elbow Method:** Plot within-cluster sum of squares (WCSS) vs. k, and pick k at the “elbow” point. 169 | - **Silhouette Score:** Measures how similar each point is to its own cluster vs. other clusters. Higher silhouette indicates better clustering. 170 | - **Domain Knowledge:** Practical insights sometimes guide k selection. 171 | 172 | ### 32. What is hierarchical clustering? 173 | **Answer:** 174 | Hierarchical clustering creates a hierarchy of clusters. **Agglomerative** starts with each point as a single cluster and merges them step-by-step. **Divisive** starts with one cluster and splits it. The result is often visualized as a dendrogram, allowing you to choose a clustering level. 175 | 176 | ### 33. Explain model interpretability and why it matters. 177 | **Answer:** 178 | Model interpretability is understanding how and why a model makes specific predictions. It matters for: 179 | - Trust and Transparency: Stakeholders need to know if decisions are fair and reliable. 180 | - Compliance: Some regulations require explanations of automated decisions. 181 | - Debugging: Interpretability helps identify errors in data or model logic. 182 | 183 | ### 34. What is a kernel in SVM? 184 | **Answer:** 185 | A kernel function transforms data into a higher-dimensional space, making complex separations appear linear. Common kernels: linear, polynomial, RBF (Gaussian). Kernels let SVMs solve nonlinear classification problems efficiently without explicitly computing coordinates in the higher space. 186 | 187 | ### 35. What is regularization in linear models? 188 | **Answer:** 189 | Regularization adds a penalty to large coefficients, controlling model complexity. In linear models, it prevents weights from becoming too large (overfitting). Examples: Ridge (L2) and Lasso (L1) regularization. It leads to simpler, more generalizable models. 190 | 191 | ### 36. Compare batch gradient descent and stochastic gradient descent. 192 | **Answer:** 193 | - **Batch Gradient Descent:** Uses the entire training set to compute the gradient for each update. More stable but can be slow for large datasets. 194 | - **Stochastic Gradient Descent (SGD):** Uses one (or a small batch) example at a time, making quicker updates and often converging faster in practice, especially useful with very large datasets. 195 | 196 | ### 37. What is the difference between parametric and non-parametric models? 197 | **Answer:** 198 | - **Parametric Models:** Have a fixed number of parameters. Assume a functional form (e.g., linear regression). Faster, need less data, but more prone to bias if assumptions are wrong. 199 | - **Non-parametric Models:** Number of parameters grows with data. Make fewer assumptions, can fit complex patterns, but risk overfitting and can be slower. 200 | 201 | ### 38. Explain what a validation curve is. 202 | **Answer:** 203 | A validation curve plots model performance (e.g., accuracy) on both training and validation sets against a single hyperparameter (like regularization strength). It helps identify where the model overfits or underfits and guides in choosing an optimal hyperparameter value. 204 | 205 | ### 39. What is early stopping? 206 | **Answer:** 207 | Early stopping monitors validation performance during training and halts as soon as the validation score stops improving, preventing overfitting by not letting the model over-train on the noise of the training set. 208 | 209 | ### 40. Define Transfer Learning. 210 | **Answer:** 211 | Transfer Learning leverages knowledge learned from one (usually large) task/domain and applies it to another related task/domain with limited data. Often seen in deep learning, where a model pre-trained on ImageNet is fine-tuned on a smaller dataset, accelerating training and improving performance. 212 | 213 | ### 41. What is a confusion matrix, and what are its components? 214 | **Answer:** 215 | Already covered in Q13, but to reiterate: A confusion matrix is a table comparing predicted vs. actual labels. It includes TP, TN, FP, FN. From these, you derive metrics like precision, recall, and accuracy, understanding errors and successes in classification. 216 | 217 | ### 42. Explain the concept of stratified sampling. 218 | **Answer:** 219 | Stratified sampling splits the dataset so that each fold (in cross-validation) or subset maintains the original class proportion. This ensures that models are evaluated on representative data, especially important for imbalanced classes. 220 | 221 | ### 43. What is data leakage? 222 | **Answer:** 223 | Data leakage occurs when information that would not be available at prediction time is inadvertently used to train the model. This often leads to overly optimistic performance estimates that vanish in real-world deployment. An example is scaling data using the whole dataset, including the test set, before model training. 224 | 225 | ### 44. What is a ROC curve? 226 | **Answer:** 227 | A ROC curve plots TPR (Recall) vs. FPR (1 - Specificity) at various classification thresholds. It shows how well the classifier can separate positive and negative classes. The AUC summarizes the curve’s overall performance. 228 | 229 | ### 45. How do you handle class imbalance? 230 | **Answer:** 231 | Techniques include: 232 | - **Oversampling the minority class (e.g., SMOTE).** 233 | - **Undersampling the majority class.** 234 | - **Adjusting class weights in the model’s loss function.** 235 | - **Focusing on metrics like F1-score or AUC rather than accuracy.** 236 | 237 | ### 46. What is a cost function (or loss function)? 238 | **Answer:** 239 | A cost (loss) function quantifies how far off the model’s predictions are from the true values. Minimizing this loss guides training. For regression, MSE is common; for classification, cross-entropy is often used. 240 | 241 | ### 47. Differentiate between Gini impurity and entropy in decision trees. 242 | **Answer:** 243 | Both measure impurity. **Entropy** (from information theory) measures unpredictability, while **Gini impurity** measures how often a randomly chosen sample would be misclassified. Both guide tree splits, but Gini is often slightly faster and commonly used in practice. 244 | 245 | ### 48. What is a hyperparameter? 246 | **Answer:** 247 | A hyperparameter is a model configuration set before training (e.g., learning rate, number of hidden layers, regularization strength). Unlike model parameters learned from data, hyperparameters are chosen via tuning methods like grid search or Bayesian optimization. 248 | 249 | ### 49. How do you tune hyperparameters? 250 | **Answer:** 251 | Techniques: 252 | - **Grid Search:** Exhaustively tries all parameter combinations. 253 | - **Random Search:** Randomly samples hyperparameter space, often more efficient. 254 | - **Bayesian Optimization:** Models the objective function to intelligently choose hyperparameters. 255 | - **Hyperband/Successive Halving:** Efficient resource allocation strategies. 256 | 257 | ### 50. Explain ensemble averaging and voting. 258 | **Answer:** 259 | Ensemble averaging (for regression) or majority voting (for classification) combines predictions from multiple models. For classification, hard voting chooses the class with the most votes; soft voting averages predicted probabilities. This leverages model diversity to boost overall accuracy and robustness. 260 | 261 | ### 51. What is gradient boosting? 262 | **Answer:** 263 | Gradient boosting builds an ensemble of weak learners (usually shallow trees), each correcting the residual errors of the previous ensemble. By moving down the gradient of the loss function, it incrementally improves performance, often resulting in highly accurate models. 264 | 265 | ### 52. How does AdaBoost work? 266 | **Answer:** 267 | AdaBoost starts with a weak classifier and reweights samples, giving more importance to previously misclassified examples. New weak learners focus on harder instances. The final model is a weighted sum of these weak learners, achieving strong performance despite each weak learner’s simplicity. 268 | 269 | ### 53. Explain the concept of model drift. 270 | **Answer:** 271 | Model drift (or data drift) occurs when the data distribution changes over time, rendering the model’s original patterns less relevant. To handle it, we might retrain regularly, monitor predictions, and adapt the model to evolving conditions. 272 | 273 | ### 54. What are one-hot encoding and label encoding? 274 | **Answer:** 275 | - **One-hot encoding:** Transforms a categorical feature into multiple binary features, each representing a category. Removes any implied ordinal relationship. 276 | - **Label encoding:** Assigns an integer to each category. It’s simpler but can misleadingly imply an order among categories. 277 | 278 | ### 55. Why might you prefer a smaller model? 279 | **Answer:** 280 | A smaller model: 281 | - Reduces the risk of overfitting. 282 | - Is faster to train and predict. 283 | - Is easier to interpret and maintain. 284 | - Requires fewer computational resources. 285 | 286 | In many practical scenarios, a simpler model that performs almost as well as a complex one is preferred due to cost and maintainability. 287 | 288 | ### 56. What is a baseline model and why is it useful? 289 | **Answer:** 290 | A baseline model sets a minimal performance standard (e.g., predicting the mean for regression or the majority class for classification). It helps understand if complex models provide real improvements over trivial solutions. 291 | 292 | ### 57. Explain the concept of latent variables. 293 | **Answer:** 294 | Latent variables are hidden factors not directly observed but inferred from data. For example, “customer satisfaction” might influence survey responses. Techniques like factor analysis or topic modeling uncover these latent dimensions, explaining observed patterns more fundamentally. 295 | 296 | ### 58. How do AutoML tools assist in model building? 297 | **Answer:** 298 | AutoML tools automate tasks like feature engineering, model selection, and hyperparameter tuning. They free practitioners from manual trial-and-error, accelerating experimentation and can produce strong models with less human intervention. 299 | 300 | ### 59. What is a pipeline in ML? 301 | **Answer:** 302 | A pipeline chains together preprocessing steps (scaling, encoding) and the model into a single workflow. This ensures the exact same transformations are applied consistently, simplifies code, and makes hyperparameter tuning and deployment more straightforward and reproducible. 303 | 304 | ### 60. Define overparameterization. 305 | **Answer:** 306 | Overparameterization occurs when a model has far more parameters than necessary to represent the target function. Neural networks often are overparameterized but still generalize well with proper regularization. Without controls, overparameterization typically leads to overfitting. 307 | 308 | ### 61. What is the difference between feature selection and feature extraction? 309 | **Answer:** 310 | - **Feature selection:** Chooses a subset of existing features to reduce dimensionality. 311 | - **Feature extraction:** Creates new features (e.g., through PCA or embeddings) that summarize or transform original features into a lower-dimensional space. 312 | 313 | ### 62. Why is scaling features important? 314 | **Answer:** 315 | Scaling ensures all features contribute equally to the model and that gradient-based methods converge more easily. Without scaling, features with large magnitudes may dominate the learning process, skewing the model’s behavior and making it harder to find optimal solutions. 316 | 317 | ### 63. Explain SMOTE. 318 | **Answer:** 319 | SMOTE (Synthetic Minority Over-sampling TEchnique) creates synthetic minority class samples by interpolating between existing minority samples. This technique helps address class imbalance without simply replicating existing minority examples, improving model sensitivity to minority classes. 320 | 321 | ### 64. What is online learning? 322 | **Answer:** 323 | Online learning updates model parameters incrementally as each new data point arrives. It’s useful for streaming or large-scale scenarios where batch retraining is costly. The model evolves continuously and can adapt quickly to changing data distributions. 324 | 325 | ### 65. Compare ridge and lasso regression. 326 | **Answer:** 327 | - **Ridge (L2):** Shrinks all coefficients but rarely zeroes them out. It reduces variance and stabilizes solutions. 328 | - **Lasso (L1):** Encourages sparsity by pushing some coefficients exactly to zero, performing feature selection. Good when many features are irrelevant. 329 | 330 | ### 66. What does an ROC AUC of 0.5 mean? 331 | **Answer:** 332 | An AUC of 0.5 indicates the model is no better than random guessing. The model has no discriminative power to rank positive instances above negatives. 333 | 334 | ### 67. Explain data augmentation. 335 | **Answer:** 336 | Data augmentation artificially expands the training set by applying transformations to existing examples. For images, this could be rotations, flips, or brightness changes. It reduces overfitting and improves robustness by making the model invariant to these transformations. 337 | 338 | ### 68. What is the main idea behind Bayesian methods in ML? 339 | **Answer:** 340 | Bayesian methods treat parameters and predictions as distributions rather than fixed values. Prior beliefs are updated with observed data, resulting in a posterior distribution. This approach quantifies uncertainty and can be more robust when data is scarce, guiding more informed decisions. 341 | 342 | ### 69. How do you handle outliers? 343 | **Answer:** 344 | Options: 345 | - Remove or cap outliers if they’re data errors. 346 | - Apply transformations like log or Box-Cox to reduce their influence. 347 | - Use robust models/metrics less sensitive to outliers (e.g., median-based loss). 348 | - Detect them using statistical methods (z-scores, IQR) before deciding on an approach. 349 | 350 | ### 70. Define anomaly detection. 351 | **Answer:** 352 | Anomaly detection identifies unusual patterns or rare events that deviate significantly from the majority. Applications include fraud detection, fault diagnosis, and intrusion detection. Methods include isolation forests, one-class SVM, or statistical deviation analysis. 353 | 354 | ### 71. What is a learning curve? 355 | **Answer:** 356 | A learning curve plots the model’s performance on training and validation sets as a function of the training set size. It shows if more data would help (if validation score is still improving) or if the model is too simple or complex (underfitting or overfitting patterns). 357 | 358 | ### 72. How is the R² score interpreted? 359 | **Answer:** 360 | R² measures what fraction of the variance in the target variable is explained by the model. An R² of 1.0 means perfect fit. R² near 0 means the model explains almost none of the variance, and negative values indicate the model is even worse than a trivial mean predictor. 361 | 362 | ### 73. What is the purpose of model calibration? 363 | **Answer:** 364 | Model calibration ensures predicted probabilities match observed frequencies. A well-calibrated model saying “70% chance” should see the event happen about 70% of the time. Calibration techniques like Platt scaling or isotonic regression improve the reliability of probability estimates. 365 | 366 | ### 74. Explain monotonicity constraints. 367 | **Answer:** 368 | Monotonicity constraints force predictions to consistently increase (or decrease) with certain features. For example, “price should not decrease as quality score increases.” Such constraints improve interpretability and ensure predictions follow known domain rules. 369 | 370 | ### 75. What is a kernel trick? 371 | **Answer:** 372 | The kernel trick allows algorithms like SVMs to operate in a transformed feature space without explicitly computing coordinates in that space. By using kernel functions (e.g., RBF), it efficiently captures complex nonlinear relationships while keeping computations tractable. 373 | 374 | ### 76. Define meta-learning. 375 | **Answer:** 376 | Meta-learning, or “learning to learn,” trains models on multiple tasks so that they can quickly adapt to new tasks with few examples. It leverages prior knowledge to reduce data requirements and speed up learning in new domains. 377 | 378 | ### 77. What is the difference between ML and DL (Deep Learning)? 379 | **Answer:** 380 | Deep Learning is a subset of ML using neural networks with many layers to learn hierarchical feature representations automatically from raw data. Traditional ML often requires manual feature engineering, whereas DL can learn features end-to-end, given sufficient data and computation. 381 | 382 | ### 78. Explain batch normalization (in the DL context). 383 | **Answer:** 384 | Batch normalization normalizes layer inputs over a mini-batch, stabilizing and speeding up training. It reduces internal covariate shift, allows higher learning rates, and often improves both convergence speed and final performance. 385 | 386 | ### 79. Why use dropout in neural networks? 387 | **Answer:** 388 | Dropout randomly deactivates a proportion of neurons during training, preventing over-reliance on specific features and encouraging multiple neurons to learn robust patterns. This reduces overfitting and improves generalization. 389 | 390 | ### 80. What are attention mechanisms in neural networks? 391 | **Answer:** 392 | Attention mechanisms allow models to focus on different parts of the input when making predictions, assigning learned weights to elements. For example, in machine translation, attention highlights important words in the source sentence for each target word, improving performance on long sequences. 393 | 394 | ### 81. Define reinforcement learning. 395 | **Answer:** 396 | Reinforcement Learning (RL) trains an agent to act in an environment to maximize cumulative rewards. The agent learns by trial and error, receiving feedback from its actions. It’s used in robotics, game playing (like AlphaGo), and resource management. 397 | 398 | ### 82. What is the purpose of Q-learning in RL? 399 | **Answer:** 400 | Q-learning is an off-policy RL algorithm that learns a value function (Q-values) mapping state-action pairs to expected future rewards. By continually updating Q-values, the agent identifies an optimal policy to achieve the highest long-term reward. 401 | 402 | ### 83. Explain the concept of generalization in ML. 403 | **Answer:** 404 | Generalization is a model’s ability to perform well on unseen data. A good generalizing model doesn’t just memorize training examples—it learns underlying patterns that hold true beyond the training set, enabling it to make accurate predictions on new samples. 405 | 406 | ### 84. What is model drift, and how do you mitigate it? 407 | **Answer:** 408 | Model drift arises when data distribution shifts over time. Mitigation involves: 409 | - Monitoring performance regularly. 410 | - Periodically retraining the model with fresh data. 411 | - Employing online learning or adaptive models that update continuously. 412 | - Using alerts or triggers when input distributions or error rates significantly change. 413 | 414 | ### 85. Define adversarial examples. 415 | **Answer:** 416 | Adversarial examples are inputs deliberately crafted with small perturbations that fool ML models into making incorrect predictions, despite appearing normal to humans. This exposes vulnerabilities in models, prompting research into robust and secure ML techniques. 417 | 418 | ### 86. What is explainability (XAI)? 419 | **Answer:** 420 | Explainable AI focuses on techniques and tools to make model decisions understandable by humans. It includes methods like LIME or SHAP to show which features influenced a prediction, aiding trust, compliance, and debugging of complex black-box models. 421 | 422 | ### 87. How do you choose an appropriate evaluation metric? 423 | **Answer:** 424 | Match the metric to the task and business goals. For imbalanced classification, F1 or AUC may be better than accuracy. For ranking tasks, consider MAP or NDCG. For regression, MSE or MAE might suffice. Also consider costs of false positives vs. false negatives. 425 | 426 | ### 88. What is multi-class vs. multi-label classification? 427 | **Answer:** 428 | - **Multi-class:** Each instance belongs to exactly one of several mutually exclusive classes. 429 | - **Multi-label:** Each instance can have multiple associated labels simultaneously. For example, a single image can contain both a cat and a dog. 430 | 431 | ### 89. Explain zero-shot learning. 432 | **Answer:** 433 | Zero-shot learning predicts classes that were not seen in the training phase. The model leverages semantic relationships, textual descriptions, or attribute-based representations to generalize to new classes without direct training examples. 434 | 435 | ### 90. What is few-shot learning? 436 | **Answer:** 437 | Few-shot learning aims to achieve good performance from only a handful of labeled examples. By leveraging prior knowledge or meta-learning, the model adapts quickly to new tasks with very limited data. 438 | 439 | ### 91. How do you debug a model that’s performing poorly? 440 | **Answer:** 441 | - Check data quality and distribution shifts. 442 | - Inspect if labels are correct. 443 | - Try simpler baseline models or different feature engineering. 444 | - Evaluate different metrics to identify the problem. 445 | - Use interpretability tools (LIME, SHAP) to understand predictions. 446 | - Perform error analysis on specific subgroups. 447 | 448 | ### 92. Explain the concept of model serving and MLOps. 449 | **Answer:** 450 | Model serving is the deployment of ML models into production, making predictions available through APIs or batch processes. MLOps extends DevOps principles to ML, covering continuous integration/deployment, monitoring, reproducibility, model versioning, and automated retraining, ensuring reliable and efficient ML systems at scale. 451 | 452 | ### 93. What is an embedding? 453 | **Answer:** 454 | An embedding maps high-dimensional, sparse data (words, items, users) into dense, low-dimensional vectors capturing semantic similarities and patterns. For example, word embeddings like Word2Vec place similar words close together in vector space, improving downstream tasks like NLP. 455 | 456 | ### 94. How do you ensure fairness in ML models? 457 | **Answer:** 458 | - Audit data for biased distributions. 459 | - Remove or mask sensitive attributes if possible. 460 | - Employ fairness-aware algorithms or regularization. 461 | - Evaluate metrics like disparate impact or equalized odds. 462 | - Continually monitor and refine models to prevent discrimination. 463 | 464 | ### 95. What is incremental learning? 465 | **Answer:** 466 | Incremental (or continual) learning updates models with new data without retraining from scratch. It’s useful when data arrives continuously or when recalculating from the entire historical dataset is too expensive. Careful methods prevent catastrophic forgetting of previously learned information. 467 | 468 | ### 96. Why use probabilistic models? 469 | **Answer:** 470 | Probabilistic models provide not just predictions but also uncertainty estimates. This aids decision-making under uncertainty. For example, knowing a model’s confidence helps determine when to defer decisions or gather more data. 471 | 472 | ### 97. Explain active learning. 473 | **Answer:** 474 | Active learning selects the most informative samples to label next, reducing labeling costs. The model queries an oracle (e.g., a human annotator) only for samples that will most improve the model if labeled, accelerating learning with fewer labeled samples. 475 | 476 | ### 98. How do you handle concept drift? 477 | **Answer:** 478 | Concept drift means the relationship between input and output changes over time. Mitigation includes: 479 | - Continual monitoring of performance. 480 | - Periodic or triggered retraining. 481 | - Weighted ensembles giving more weight to recent data. 482 | - Adaptive online learning techniques. 483 | 484 | ### 99. Define monotone constraints in gradient boosting. 485 | **Answer:** 486 | Monotone constraints ensure that as a particular feature value increases, the model’s predictions either never decrease or never increase (depending on the constraint). It encodes domain knowledge (e.g., price should not decrease with quality) and improves trust and interpretability. 487 | 488 | ### 100. When would you use Bayesian Optimization? 489 | **Answer:** 490 | Bayesian Optimization is used for efficient hyperparameter tuning when model evaluations are expensive. It builds a surrogate model of the objective function and uses an acquisition function to select promising hyperparameter configurations, often converging to good solutions with fewer trials than grid or random search. 491 | 492 | --- 493 | 494 | 495 | If you found this repository helpful, please give it a star! 496 | 497 | Follow me on: 498 | - [LinkedIn](https://www.linkedin.com/in/ashish-jangra/) 499 | - [GitHub](https://github.com/AshishJangra27) 500 | - [Kaggle](https://www.kaggle.com/ashishjangra27) 501 | 502 | Stay updated with my latest content and projects! 503 | -------------------------------------------------------------------------------- /Matplotlib.md: -------------------------------------------------------------------------------- 1 | # 50 Matplotlib Interview Questions and Answers 2 | 3 | Below are 50 practical Matplotlib questions and answers focusing on plotting, customization, and manipulation of figures and axes using Python's Matplotlib library. 4 | 5 | Each question includes: 6 | - A brief scenario 7 | - A code snippet demonstrating one possible solution 8 | 9 | **Note:** For all examples, assume you have imported Matplotlib as follows: 10 | ```python 11 | import matplotlib.pyplot as plt 12 | ``` 13 | 14 | --- 15 | 16 | ### 1. How do you create a simple line plot with Matplotlib? 17 | **Answer:** 18 | Use `plt.plot()` and `plt.show()` to display. 19 | ```python 20 | x = [1, 2, 3, 4] 21 | y = [10, 20, 25, 30] 22 | plt.plot(x, y) 23 | plt.show() 24 | ``` 25 | 26 | --- 27 | 28 | ### 2. How do you set the title of a plot? 29 | **Answer:** 30 | Use `plt.title("Your Title")`. 31 | ```python 32 | plt.plot([1,2,3],[2,4,6]) 33 | plt.title("My Line Plot") 34 | plt.show() 35 | ``` 36 | 37 | --- 38 | 39 | ### 3. How do you label the x and y axes? 40 | **Answer:** 41 | Use `plt.xlabel()` and `plt.ylabel()`. 42 | ```python 43 | plt.plot([1, 2, 3], [4, 5, 6]) 44 | plt.xlabel("X-Axis Label") 45 | plt.ylabel("Y-Axis Label") 46 | plt.show() 47 | ``` 48 | 49 | --- 50 | 51 | ### 4. How do you add a legend to a plot? 52 | **Answer:** 53 | Use `plt.legend()` after providing labels in `plt.plot()`. 54 | ```python 55 | plt.plot([1,2,3],[2,4,6], label="Line 1") 56 | plt.plot([1,2,3],[3,6,9], label="Line 2") 57 | plt.xlabel("X") 58 | plt.ylabel("Y") 59 | plt.legend() 60 | plt.show() 61 | ``` 62 | 63 | --- 64 | 65 | ### 5. How do you change the line style in a plot? 66 | **Answer:** 67 | Use the `linestyle` parameter in `plt.plot()`. 68 | ```python 69 | plt.plot([0,1,2],[0,1,4], linestyle='--', label="Dashed") 70 | plt.legend() 71 | plt.show() 72 | ``` 73 | 74 | --- 75 | 76 | ### 6. How do you change the line color in a plot? 77 | **Answer:** 78 | Use the `color` parameter in `plt.plot()`. 79 | ```python 80 | plt.plot([0,1,2],[0,1,4], color='red') 81 | plt.show() 82 | ``` 83 | 84 | --- 85 | 86 | ### 7. How do you plot multiple lines on the same axes? 87 | **Answer:** 88 | Call `plt.plot()` multiple times before `plt.show()`. 89 | ```python 90 | plt.plot([1,2,3],[1,2,3], label="Line A") 91 | plt.plot([1,2,3],[2,4,6], label="Line B") 92 | plt.legend() 93 | plt.show() 94 | ``` 95 | 96 | --- 97 | 98 | ### 8. How do you create a scatter plot? 99 | **Answer:** 100 | Use `plt.scatter()` with x and y values. 101 | ```python 102 | x = [1,2,3,4] 103 | y = [10,8,12,15] 104 | plt.scatter(x, y) 105 | plt.show() 106 | ``` 107 | 108 | --- 109 | 110 | ### 9. How do you change marker style in a scatter plot? 111 | **Answer:** 112 | Use the `marker` parameter in `plt.scatter()`. 113 | ```python 114 | plt.scatter([1,2,3],[2,3,4], marker='o') 115 | plt.show() 116 | ``` 117 | 118 | --- 119 | 120 | ### 10. How do you create a bar plot? 121 | **Answer:** 122 | Use `plt.bar()` for vertical bars. 123 | ```python 124 | categories = ['A','B','C'] 125 | values = [10,20,15] 126 | plt.bar(categories, values) 127 | plt.show() 128 | ``` 129 | 130 | --- 131 | 132 | ### 11. How do you create a horizontal bar plot? 133 | **Answer:** 134 | Use `plt.barh()` for horizontal bars. 135 | ```python 136 | categories = ['A','B','C'] 137 | values = [10,20,15] 138 | plt.barh(categories, values) 139 | plt.show() 140 | ``` 141 | 142 | --- 143 | 144 | ### 12. How do you add error bars to a plot? 145 | **Answer:** 146 | Use `plt.errorbar()` with `yerr` parameter. 147 | ```python 148 | x = [1,2,3] 149 | y = [2,4,3] 150 | yerr = [0.5, 0.2, 0.3] 151 | plt.errorbar(x, y, yerr=yerr, fmt='o') 152 | plt.show() 153 | ``` 154 | 155 | --- 156 | 157 | ### 13. How do you create a histogram? 158 | **Answer:** 159 | Use `plt.hist()` with your data. 160 | ```python 161 | data = [1,1,2,2,2,3,3,4,5] 162 | plt.hist(data, bins=5) 163 | plt.show() 164 | ``` 165 | 166 | --- 167 | 168 | ### 14. How do you create a pie chart? 169 | **Answer:** 170 | Use `plt.pie()` with a list of values. 171 | ```python 172 | values = [30,40,20,10] 173 | labels = ['A','B','C','D'] 174 | plt.pie(values, labels=labels) 175 | plt.show() 176 | ``` 177 | 178 | --- 179 | 180 | ### 15. How do you change figure size? 181 | **Answer:** 182 | Use `plt.figure(figsize=(width,height))`. 183 | ```python 184 | plt.figure(figsize=(8,4)) 185 | plt.plot([1,2,3],[2,4,1]) 186 | plt.show() 187 | ``` 188 | 189 | --- 190 | 191 | ### 16. How do you save a figure to a file? 192 | **Answer:** 193 | Use `plt.savefig('filename.png')` before `plt.show()`. 194 | ```python 195 | plt.plot([1,2,3],[2,4,6]) 196 | plt.savefig('myplot.png') 197 | plt.show() 198 | ``` 199 | 200 | --- 201 | 202 | ### 17. How do you adjust the axis limits? 203 | **Answer:** 204 | Use `plt.xlim()` and `plt.ylim()`. 205 | ```python 206 | plt.plot([1,2,3],[2,4,6]) 207 | plt.xlim(0,4) 208 | plt.ylim(0,10) 209 | plt.show() 210 | ``` 211 | 212 | --- 213 | 214 | ### 18. How do you add grid lines? 215 | **Answer:** 216 | Use `plt.grid(True)`. 217 | ```python 218 | plt.plot([1,2,3],[2,4,6]) 219 | plt.grid(True) 220 | plt.show() 221 | ``` 222 | 223 | --- 224 | 225 | ### 19. How do you plot a function directly (e.g., y = x^2)? 226 | **Answer:** 227 | Generate x values and compute y, then plot. 228 | ```python 229 | import numpy as np 230 | x = np.linspace(-5,5,100) 231 | y = x**2 232 | plt.plot(x, y) 233 | plt.show() 234 | ``` 235 | 236 | --- 237 | 238 | ### 20. How do you add text at a specific point in the plot? 239 | **Answer:** 240 | Use `plt.text(x, y, "Text")`. 241 | ```python 242 | plt.plot([1,2],[2,4]) 243 | plt.text(1.5, 3, "Midpoint") 244 | plt.show() 245 | ``` 246 | 247 | --- 248 | 249 | ### 21. How do you create subplots in a single figure? 250 | **Answer:** 251 | Use `plt.subplot(rows, cols, index)`. 252 | ```python 253 | plt.subplot(1,2,1) 254 | plt.plot([1,2],[2,4]) 255 | plt.subplot(1,2,2) 256 | plt.plot([1,2],[3,1]) 257 | plt.show() 258 | ``` 259 | 260 | --- 261 | 262 | ### 22. How do you share axes between subplots? 263 | **Answer:** 264 | Use the `sharex` or `sharey` parameters in `plt.subplots()`. 265 | ```python 266 | fig, (ax1, ax2) = plt.subplots(2,1, sharex=True) 267 | ax1.plot([1,2,3],[2,4,6]) 268 | ax2.plot([1,2,3],[3,2,1]) 269 | plt.show() 270 | ``` 271 | 272 | --- 273 | 274 | ### 23. How do you adjust spacing between subplots? 275 | **Answer:** 276 | Use `plt.tight_layout()` after creating subplots. 277 | ```python 278 | fig, (ax1, ax2) = plt.subplots(2,1) 279 | ax1.plot([1,2],[2,4]) 280 | ax2.plot([1,2],[3,1]) 281 | plt.tight_layout() 282 | plt.show() 283 | ``` 284 | 285 | --- 286 | 287 | ### 24. How do you change tick labels on the x-axis? 288 | **Answer:** 289 | Use `plt.xticks()` with desired labels. 290 | ```python 291 | x = [1,2,3] 292 | y = [2,4,6] 293 | plt.plot(x, y) 294 | plt.xticks([1,2,3], ['One','Two','Three']) 295 | plt.show() 296 | ``` 297 | 298 | --- 299 | 300 | ### 25. How do you rotate tick labels? 301 | **Answer:** 302 | Use `plt.xticks(rotation=angle)`. 303 | ```python 304 | x = [1,2,3] 305 | y = [2,4,6] 306 | plt.plot(x, y) 307 | plt.xticks([1,2,3], ['One','Two','Three'], rotation=45) 308 | plt.show() 309 | ``` 310 | 311 | --- 312 | 313 | ### 26. How do you set a logarithmic scale on an axis? 314 | **Answer:** 315 | Use `plt.xscale('log')` or `plt.yscale('log')`. 316 | ```python 317 | x = [1,10,100,1000] 318 | y = [10,100,1000,10000] 319 | plt.plot(x, y) 320 | plt.xscale('log') 321 | plt.yscale('log') 322 | plt.show() 323 | ``` 324 | 325 | --- 326 | 327 | ### 27. How do you change the figure background color? 328 | **Answer:** 329 | Use `plt.gca().set_facecolor('color')` or `fig.set_facecolor()`. 330 | ```python 331 | fig = plt.figure() 332 | fig.set_facecolor('lightgray') 333 | plt.plot([1,2],[2,4]) 334 | plt.show() 335 | ``` 336 | 337 | --- 338 | 339 | ### 28. How do you change the line width of a plot? 340 | **Answer:** 341 | Use `linewidth` parameter in `plt.plot()`. 342 | ```python 343 | plt.plot([1,2],[2,4], linewidth=3) 344 | plt.show() 345 | ``` 346 | 347 | --- 348 | 349 | ### 29. How do you plot a filled area under a curve? 350 | **Answer:** 351 | Use `plt.fill_between(x, y)` where y is the function. 352 | ```python 353 | import numpy as np 354 | x = np.linspace(0,1,50) 355 | y = x**2 356 | plt.plot(x, y) 357 | plt.fill_between(x, y, color='yellow') 358 | plt.show() 359 | ``` 360 | 361 | --- 362 | 363 | ### 30. How do you annotate a point with an arrow? 364 | **Answer:** 365 | Use `plt.annotate()` with `xy` and `xytext`. 366 | ```python 367 | plt.plot([1,2,3],[2,4,3]) 368 | plt.annotate('Peak', xy=(2,4), xytext=(2,5), arrowprops=dict(facecolor='black', arrowstyle='->')) 369 | plt.show() 370 | ``` 371 | 372 | --- 373 | 374 | ### 31. How do you change the figure DPI? 375 | **Answer:** 376 | Use `plt.figure(dpi=...)`. 377 | ```python 378 | plt.figure(dpi=150) 379 | plt.plot([1,2,3],[2,4,6]) 380 | plt.show() 381 | ``` 382 | 383 | --- 384 | 385 | ### 32. How do you clear a figure? 386 | **Answer:** 387 | Use `plt.clf()` to clear the current figure. 388 | ```python 389 | plt.plot([1,2],[2,4]) 390 | plt.clf() 391 | # Figure is now cleared 392 | ``` 393 | 394 | --- 395 | 396 | ### 33. How do you close a figure? 397 | **Answer:** 398 | Use `plt.close()` to close the current figure. 399 | ```python 400 | plt.plot([1,2],[2,4]) 401 | plt.show() 402 | plt.close() 403 | ``` 404 | 405 | --- 406 | 407 | ### 34. How do you plot a horizontal line? 408 | **Answer:** 409 | Use `plt.axhline(y=value)`. 410 | ```python 411 | plt.axhline(y=0.5, color='red') 412 | plt.show() 413 | ``` 414 | 415 | --- 416 | 417 | ### 35. How do you plot a vertical line? 418 | **Answer:** 419 | Use `plt.axvline(x=value)`. 420 | ```python 421 | plt.axvline(x=2, color='green') 422 | plt.show() 423 | ``` 424 | 425 | --- 426 | 427 | ### 36. How do you add a shaded region (vertical) between two x-values? 428 | **Answer:** 429 | Use `plt.axvspan(xmin, xmax, color='color')`. 430 | ```python 431 | plt.axvspan(1,2, color='grey', alpha=0.3) 432 | plt.show() 433 | ``` 434 | 435 | --- 436 | 437 | ### 37. How do you add a secondary y-axis? 438 | **Answer:** 439 | Use `ax2 = ax.twinx()` on an existing Axes. 440 | ```python 441 | fig, ax = plt.subplots() 442 | ax.plot([1,2,3],[2,4,6], color='blue') 443 | ax2 = ax.twinx() 444 | ax2.plot([1,2,3],[10,20,15], color='red') 445 | plt.show() 446 | ``` 447 | 448 | --- 449 | 450 | ### 38. How do you add a secondary x-axis? 451 | **Answer:** 452 | Use `ax2 = ax.twiny()` on an existing Axes. 453 | ```python 454 | fig, ax = plt.subplots() 455 | ax.plot([1,2,3],[2,4,6]) 456 | ax2 = ax.twiny() 457 | ax2.set_xlim(ax.get_xlim()) 458 | plt.show() 459 | ``` 460 | 461 | --- 462 | 463 | ### 39. How do you remove the top and right spines? 464 | **Answer:** 465 | Use `ax.spines['top'].set_visible(False)` and similarly for 'right'. 466 | ```python 467 | fig, ax = plt.subplots() 468 | ax.plot([1,2,3],[2,4,3]) 469 | ax.spines['top'].set_visible(False) 470 | ax.spines['right'].set_visible(False) 471 | plt.show() 472 | ``` 473 | 474 | --- 475 | 476 | ### 40. How do you create a polar plot? 477 | **Answer:** 478 | Use `plt.subplot(projection='polar')`. 479 | ```python 480 | ax = plt.subplot(projection='polar') 481 | theta = [0,0.5,1,1.5] 482 | r = [1,2,1,3] 483 | ax.plot(theta, r) 484 | plt.show() 485 | ``` 486 | 487 | --- 488 | 489 | ### 41. How do you display a colorbar for an image plot? 490 | **Answer:** 491 | Use `plt.colorbar()` after `plt.imshow()` or similar functions. 492 | ```python 493 | import numpy as np 494 | data = np.random.rand(10,10) 495 | plt.imshow(data, cmap='viridis') 496 | plt.colorbar() 497 | plt.show() 498 | ``` 499 | 500 | --- 501 | 502 | ### 42. How do you specify colors using hex codes? 503 | **Answer:** 504 | Use a hex string like `color='#FF5733'`. 505 | ```python 506 | plt.plot([1,2],[2,4], color='#FF5733') 507 | plt.show() 508 | ``` 509 | 510 | --- 511 | 512 | ### 43. How do you change the tick frequency? 513 | **Answer:** 514 | Use `plt.xticks()` or `plt.yticks()` with a custom list of tick positions. 515 | ```python 516 | plt.plot([1,2,3],[2,4,6]) 517 | plt.xticks([1,1.5,2,2.5,3]) 518 | plt.show() 519 | ``` 520 | 521 | --- 522 | 523 | ### 44. How do you fill between two lines? 524 | **Answer:** 525 | Use `plt.fill_between(x, y1, y2)`. 526 | ```python 527 | import numpy as np 528 | x = np.linspace(0,1,50) 529 | y1 = x 530 | y2 = x**2 531 | plt.plot(x, y1, x, y2) 532 | plt.fill_between(x, y1, y2, where=(y1>y2), color='yellow', alpha=0.5) 533 | plt.show() 534 | ``` 535 | 536 | --- 537 | 538 | ### 45. How do you make a scatter plot with varying point sizes? 539 | **Answer:** 540 | Use the `s` parameter in `plt.scatter()`. 541 | ```python 542 | x = [1,2,3,4] 543 | y = [2,3,5,1] 544 | sizes = [50,100,200,300] 545 | plt.scatter(x, y, s=sizes) 546 | plt.show() 547 | ``` 548 | 549 | --- 550 | 551 | ### 46. How do you display a grid of images? 552 | **Answer:** 553 | Use subplots and `plt.imshow()` in each subplot. 554 | ```python 555 | import numpy as np 556 | fig, axes = plt.subplots(1,2) 557 | data1 = np.random.rand(10,10) 558 | data2 = np.random.rand(10,10) 559 | axes[0].imshow(data1, cmap='gray') 560 | axes[1].imshow(data2, cmap='gray') 561 | plt.show() 562 | ``` 563 | 564 | --- 565 | 566 | ### 47. How do you change the direction of tick labels? 567 | **Answer:** 568 | Use `plt.tick_params()` with `labelrotation`. 569 | ```python 570 | plt.plot([1,2,3],[2,4,6]) 571 | plt.tick_params(axis='x', labelrotation=90) 572 | plt.show() 573 | ``` 574 | 575 | --- 576 | 577 | ### 48. How do you plot a contour plot? 578 | **Answer:** 579 | Use `plt.contour()` or `plt.contourf()` for filled contours. 580 | ```python 581 | import numpy as np 582 | x = np.linspace(-2,2,50) 583 | y = np.linspace(-2,2,50) 584 | X,Y = np.meshgrid(x,y) 585 | Z = X**2 + Y**2 586 | plt.contour(X, Y, Z, levels=[1,2,3,4]) 587 | plt.show() 588 | ``` 589 | 590 | --- 591 | 592 | ### 49. How do you remove all ticks and labels? 593 | **Answer:** 594 | Set `plt.xticks([])` and `plt.yticks([])`. 595 | ```python 596 | plt.plot([1,2],[2,4]) 597 | plt.xticks([]) 598 | plt.yticks([]) 599 | plt.show() 600 | ``` 601 | 602 | --- 603 | 604 | ### 50. How do you plot dates on the x-axis? 605 | **Answer:** 606 | Convert dates to a suitable format and use `plt.plot_date()` or normal `plt.plot()` if x are datetimes. 607 | ```python 608 | import datetime as dt 609 | dates = [dt.datetime(2021,1,1), dt.datetime(2021,1,2), dt.datetime(2021,1,3)] 610 | values = [10,20,15] 611 | plt.plot_date(dates, values, linestyle='-') 612 | plt.show() 613 | ``` 614 | 615 | --- 616 | 617 | If you found this helpful, consider following or starring the repository! 618 | 619 | Stay updated with my latest content and projects! 620 | -------------------------------------------------------------------------------- /NumPy.md: -------------------------------------------------------------------------------- 1 | # 50 NumPy Interview Questions and Answers 2 | 3 | ### 1. How do you create a structured array in NumPy? 4 | **Answer:** 5 | You can create a structured array using the `np.array` function with a list of tuples and a structured data type. 6 | ```python 7 | import numpy as np 8 | 9 | data = [(1, 'Alice', 25), (2, 'Bob', 30), (3, 'Charlie', 35)] 10 | dtype = [('id', 'i4'), ('name', 'U10'), ('age', 'i4')] 11 | structured_array = np.array(data, dtype=dtype) 12 | print(structured_array) 13 | # Output: [(1, 'Alice', 25) (2, 'Bob', 30) (3, 'Charlie', 35)] 14 | ``` 15 | --- 16 | 17 | ### 2. How do you create a boolean mask for an array based on a condition in NumPy? 18 | **Answer:** 19 | You can create a boolean mask by applying a condition directly to the array. 20 | ```python 21 | import numpy as np 22 | 23 | arr = np.array([1, 2, 3, 4, 5]) 24 | mask = arr > 3 25 | print(mask) 26 | # Output: [False False False True True] 27 | ``` 28 | --- 29 | 30 | ### 3. How do you use the boolean mask to filter an array in NumPy? 31 | **Answer:** 32 | You can use the boolean mask to filter the array by indexing it with the mask. 33 | ```python 34 | import numpy as np 35 | 36 | arr = np.array([1, 2, 3, 4, 5]) 37 | mask = arr > 3 38 | filtered_arr = arr[mask] 39 | print(filtered_arr) 40 | # Output: [4 5] 41 | ``` 42 | --- 43 | 44 | ### 4. How do you compute the dot product of two arrays in NumPy? 45 | **Answer:** 46 | You can compute the dot product using the `np.dot` function. 47 | ```python 48 | import numpy as np 49 | 50 | a = np.array([1, 2, 3]) 51 | b = np.array([4, 5, 6]) 52 | dot_product = np.dot(a, b) 53 | print(dot_product) 54 | # Output: 32 55 | ``` 56 | --- 57 | 58 | ### 5. How do you compute the cross product of two arrays in NumPy? 59 | **Answer:** 60 | You can compute the cross product using the `np.cross` function. 61 | ```python 62 | import numpy as np 63 | 64 | a = np.array([1, 2, 3]) 65 | b = np.array([4, 5, 6]) 66 | cross_product = np.cross(a, b) 67 | print(cross_product) 68 | # Output: [-3 6 -3] 69 | ``` 70 | --- 71 | 72 | ### 6. How do you find the indices of the maximum value in a NumPy array? 73 | **Answer:** 74 | You can find the indices of the maximum value using the `np.argmax` function. 75 | ```python 76 | import numpy as np 77 | 78 | arr = np.array([1, 3, 7, 1, 2]) 79 | max_index = np.argmax(arr) 80 | print(max_index) 81 | # Output: 2 82 | ``` 83 | --- 84 | 85 | ### 7. How do you find the indices of the minimum value in a NumPy array? 86 | **Answer:** 87 | You can find the indices of the minimum value using the `np.argmin` function. 88 | ```python 89 | import numpy as np 90 | 91 | arr = np.array([1, 3, 7, 1, 2]) 92 | min_index = np.argmin(arr) 93 | print(min_index) 94 | # Output: 0 95 | ``` 96 | --- 97 | 98 | ### 8. How do you calculate the cumulative sum of elements along a given axis in NumPy? 99 | **Answer:** 100 | You can calculate the cumulative sum using the `np.cumsum` function. 101 | ```python 102 | import numpy as np 103 | 104 | arr = np.array([1, 2, 3, 4, 5]) 105 | cumsum = np.cumsum(arr) 106 | print(cumsum) 107 | # Output: [ 1 3 6 10 15] 108 | ``` 109 | --- 110 | 111 | ### 9. How do you calculate the cumulative product of elements along a given axis in NumPy? 112 | **Answer:** 113 | You can calculate the cumulative product using the `np.cumprod` function. 114 | ```python 115 | import numpy as np 116 | 117 | arr = np.array([1, 2, 3, 4, 5]) 118 | cumprod = np.cumprod(arr) 119 | print(cumprod) 120 | # Output: [ 1 2 6 24 120] 121 | ``` 122 | --- 123 | 124 | ### 10. How do you compute the eigenvalues and eigenvectors of a matrix in NumPy? 125 | **Answer:** 126 | You can compute the eigenvalues and eigenvectors using the `np.linalg.eig` function. 127 | ```python 128 | import numpy as np 129 | 130 | matrix = np.array([[4, -2], [1, 1]]) 131 | eigenvalues, eigenvectors = np.linalg.eig(matrix) 132 | print(eigenvalues) 133 | # Output: [3. 2.] 134 | print(eigenvectors) 135 | # Output: [[ 0.89442719 0.70710678] 136 | # [ 0.4472136 -0.70710678]] 137 | ``` 138 | --- 139 | 140 | ### 11. How do you compute the inverse of a matrix in NumPy? 141 | **Answer:** 142 | You can compute the inverse using the `np.linalg.inv` function. 143 | ```python 144 | import numpy as np 145 | 146 | matrix = np.array([[1, 2], [3, 4]]) 147 | inverse_matrix = np.linalg.inv(matrix) 148 | print(inverse_matrix) 149 | # Output: [[-2. 1. ] 150 | # [ 1.5 -0.5]] 151 | ``` 152 | --- 153 | 154 | ### 12. How do you compute the determinant of a matrix in NumPy? 155 | **Answer:** 156 | You can compute the determinant using the `np.linalg.det` function. 157 | ```python 158 | import numpy as np 159 | 160 | matrix = np.array([[1, 2], [3, 4]]) 161 | determinant = np.linalg.det(matrix) 162 | print(determinant) 163 | # Output: -2.0000000000000004 164 | ``` 165 | --- 166 | 167 | ### 13. How do you solve a system of linear equations using NumPy? 168 | **Answer:** 169 | You can solve a system of linear equations using the `np.linalg.solve` function. 170 | ```python 171 | import numpy as np 172 | 173 | A = np.array([[3, 1], [1, 2]]) 174 | B = np.array([9, 8]) 175 | solution = np.linalg.solve(A, B) 176 | print(solution) 177 | # Output: [2. 3.] 178 | ``` 179 | --- 180 | 181 | ### 14. How do you perform element-wise multiplication of two arrays in NumPy? 182 | **Answer:** 183 | You can perform element-wise multiplication using the `*` operator. 184 | ```python 185 | import numpy as np 186 | 187 | a = np.array([1, 2, 3]) 188 | b = np.array([4, 5, 6]) 189 | result = a * b 190 | print(result) 191 | # Output: [ 4 10 18] 192 | ``` 193 | --- 194 | 195 | ### 15. How do you perform matrix multiplication of two arrays in NumPy? 196 | **Answer:** 197 | You can perform matrix multiplication using the `np.matmul` function. 198 | ```python 199 | import numpy as np 200 | 201 | a = np.array([[1, 2], [3, 4]]) 202 | b = np.array([[5, 6], [7, 8]]) 203 | result = np.matmul(a, b) 204 | print(result) 205 | # Output: [[19 22] 206 | # [43 50]] 207 | ``` 208 | --- 209 | 210 | ### 16. How do you compute the QR decomposition of a matrix in NumPy? 211 | **Answer:** 212 | You can compute the QR decomposition using the `np.linalg.qr` function. 213 | ```python 214 | import numpy as np 215 | 216 | matrix = np.array([[1, 2], [3, 4]]) 217 | Q, R = np.linalg.qr(matrix) 218 | print(Q) 219 | # Output: [[-0.31622777 -0.9486833 ] 220 | # [-0.9486833 0.31622777]] 221 | print(R) 222 | # Output: [[-3.16227766 -4.42718872] 223 | # [ 0. 0.63245553]] 224 | ``` 225 | --- 226 | 227 | ### 17. How do you compute the singular value decomposition (SVD) of a matrix in NumPy? 228 | **Answer:** 229 | You can compute the SVD using the `np.linalg.svd` function. 230 | ```python 231 | import numpy as np 232 | 233 | matrix = np.array([[1, 2], [3, 4], [5, 6]]) 234 | U, S, V = np.linalg.svd(matrix) 235 | print(U) 236 | # Output: [[-0.2298477 0.88346102 0.40824829] 237 | # [-0.52474482 0.24078249 -0.81649658] 238 | # [-0.81964194 -0.40189603 0.40824829]] 239 | print(S) 240 | # Output: [9.52551809 0.51430058] 241 | print(V) 242 | # Output: [[-0.61962948 -0.78489445] 243 | # [-0.78489445 0.61962948]] 244 | ``` 245 | --- 246 | 247 | ### 18. How do you generate a random matrix with values drawn from a normal distribution in NumPy? 248 | **Answer:** 249 | You can generate a random matrix using the `np.random.randn` function. 250 | ```python 251 | import numpy as np 252 | 253 | random_matrix = np.random.randn(3, 3) 254 | print(random_matrix) 255 | # Output: Random 3x3 matrix with values from normal distribution 256 | ``` 257 | --- 258 | 259 | ### 19. How do you shuffle the elements of a NumPy array? 260 | **Answer:** 261 | You can shuffle the elements using the `np.random.shuffle` function. 262 | ```python 263 | import numpy as np 264 | 265 | arr = np.array([1, 2, 3, 4, 266 | 267 | 5]) 268 | np.random.shuffle(arr) 269 | print(arr) 270 | # Output: Shuffled array, e.g., [3 1 4 5 2] 271 | ``` 272 | --- 273 | 274 | ### 20. How do you create a random permutation of a sequence in NumPy? 275 | **Answer:** 276 | You can create a random permutation using the `np.random.permutation` function. 277 | ```python 278 | import numpy as np 279 | 280 | arr = np.array([1, 2, 3, 4, 5]) 281 | permuted_arr = np.random.permutation(arr) 282 | print(permuted_arr) 283 | # Output: Permuted array, e.g., [3 1 4 5 2] 284 | ``` 285 | --- 286 | 287 | ### 21. How do you generate random integers within a specified range in NumPy? 288 | **Answer:** 289 | You can generate random integers using the `np.random.randint` function. 290 | ```python 291 | import numpy as np 292 | 293 | random_integers = np.random.randint(0, 10, size=5) 294 | print(random_integers) 295 | # Output: Random integers within range [0, 10) 296 | ``` 297 | --- 298 | 299 | ### 22. How do you create an array with a given shape and all elements set to zero in NumPy? 300 | **Answer:** 301 | You can create such an array using the `np.zeros` function. 302 | ```python 303 | import numpy as np 304 | 305 | zero_array = np.zeros((3, 4)) 306 | print(zero_array) 307 | # Output: 3x4 array of zeros 308 | ``` 309 | --- 310 | 311 | ### 23. How do you create an array with a given shape and all elements set to one in NumPy? 312 | **Answer:** 313 | You can create such an array using the `np.ones` function. 314 | ```python 315 | import numpy as np 316 | 317 | one_array = np.ones((2, 3)) 318 | print(one_array) 319 | # Output: 2x3 array of ones 320 | ``` 321 | --- 322 | 323 | ### 24. How do you create an identity matrix of a given size in NumPy? 324 | **Answer:** 325 | You can create an identity matrix using the `np.eye` function. 326 | ```python 327 | import numpy as np 328 | 329 | identity_matrix = np.eye(3) 330 | print(identity_matrix) 331 | # Output: 3x3 identity matrix 332 | ``` 333 | --- 334 | 335 | ### 25. How do you create an array with a range of values in NumPy? 336 | **Answer:** 337 | You can create an array with a range of values using the `np.arange` function. 338 | ```python 339 | import numpy as np 340 | 341 | range_array = np.arange(0, 10, 2) 342 | print(range_array) 343 | # Output: [0 2 4 6 8] 344 | ``` 345 | --- 346 | 347 | ### 26. How do you create an array with values evenly spaced between two given values in NumPy? 348 | **Answer:** 349 | You can create such an array using the `np.linspace` function. 350 | ```python 351 | import numpy as np 352 | 353 | linspace_array = np.linspace(0, 1, 5) 354 | print(linspace_array) 355 | # Output: [0. 0.25 0.5 0.75 1. ] 356 | ``` 357 | --- 358 | 359 | ### 27. How do you concatenate two arrays along a specified axis in NumPy? 360 | **Answer:** 361 | You can concatenate two arrays using the `np.concatenate` function. 362 | ```python 363 | import numpy as np 364 | 365 | a = np.array([[1, 2], [3, 4]]) 366 | b = np.array([[5, 6]]) 367 | concatenated_array = np.concatenate((a, b), axis=0) 368 | print(concatenated_array) 369 | # Output: [[1 2] 370 | # [3 4] 371 | # [5 6]] 372 | ``` 373 | --- 374 | 375 | ### 28. How do you stack arrays vertically in NumPy? 376 | **Answer:** 377 | You can stack arrays vertically using the `np.vstack` function. 378 | ```python 379 | import numpy as np 380 | 381 | a = np.array([1, 2, 3]) 382 | b = np.array([4, 5, 6]) 383 | stacked_array = np.vstack((a, b)) 384 | print(stacked_array) 385 | # Output: [[1 2 3] 386 | # [4 5 6]] 387 | ``` 388 | --- 389 | 390 | ### 29. How do you stack arrays horizontally in NumPy? 391 | **Answer:** 392 | You can stack arrays horizontally using the `np.hstack` function. 393 | ```python 394 | import numpy as np 395 | 396 | a = np.array([1, 2, 3]) 397 | b = np.array([4, 5, 6]) 398 | stacked_array = np.hstack((a, b)) 399 | print(stacked_array) 400 | # Output: [1 2 3 4 5 6] 401 | ``` 402 | --- 403 | 404 | ### 30. How do you split an array into multiple sub-arrays along a specified axis in NumPy? 405 | **Answer:** 406 | You can split an array using the `np.split` function. 407 | ```python 408 | import numpy as np 409 | 410 | arr = np.array([1, 2, 3, 4, 5, 6]) 411 | split_array = np.split(arr, 3) 412 | print(split_array) 413 | # Output: [array([1, 2]), array([3, 4]), array([5, 6])] 414 | ``` 415 | --- 416 | 417 | ### 31. How do you compute the outer product of two vectors in NumPy? 418 | **Answer:** 419 | You can compute the outer product using the `np.outer` function. 420 | ```python 421 | import numpy as np 422 | 423 | a = np.array([1, 2, 3]) 424 | b = np.array([4, 5, 6]) 425 | outer_product = np.outer(a, b) 426 | print(outer_product) 427 | # Output: [[ 4 5 6] 428 | # [ 8 10 12] 429 | # [12 15 18]] 430 | ``` 431 | --- 432 | 433 | ### 32. How do you compute the inner product of two vectors in NumPy? 434 | **Answer:** 435 | You can compute the inner product using the `np.inner` function. 436 | ```python 437 | import numpy as np 438 | 439 | a = np.array([1, 2, 3]) 440 | b = np.array([4, 5, 6]) 441 | inner_product = np.inner(a, b) 442 | print(inner_product) 443 | # Output: 32 444 | ``` 445 | --- 446 | 447 | ### 33. How do you compute the Kronecker product of two arrays in NumPy? 448 | **Answer:** 449 | You can compute the Kronecker product using the `np.kron` function. 450 | ```python 451 | import numpy as np 452 | 453 | a = np.array([[1, 2], [3, 4]]) 454 | b = np.array([[0, 5], [6, 7]]) 455 | kronecker_product = np.kron(a, b) 456 | print(kronecker_product) 457 | # Output: [[ 0 5 0 10] 458 | # [ 6 7 12 14] 459 | # [ 0 15 0 20] 460 | # [18 21 24 28]] 461 | ``` 462 | --- 463 | 464 | ### 34. How do you compute the matrix rank of an array in NumPy? 465 | **Answer:** 466 | You can compute the matrix rank using the `np.linalg.matrix_rank` function. 467 | ```python 468 | import numpy as np 469 | 470 | matrix = np.array([[1, 2], [3, 4]]) 471 | rank = np.linalg.matrix_rank(matrix) 472 | print(rank) 473 | # Output: 2 474 | ``` 475 | --- 476 | 477 | ### 35. How do you compute the Frobenius norm of a matrix in NumPy? 478 | **Answer:** 479 | You can compute the Frobenius norm using the `np.linalg.norm` function with the `ord='fro'` argument. 480 | ```python 481 | import numpy as np 482 | 483 | matrix = np.array([[1, 2], [3, 4]]) 484 | frobenius_norm = np.linalg.norm(matrix, ord='fro') 485 | print(frobenius_norm) 486 | # Output: 5.477225575051661 487 | ``` 488 | --- 489 | 490 | ### 36. How do you compute the condition number of a matrix in NumPy? 491 | **Answer:** 492 | You can compute the condition number using the `np.linalg.cond` function. 493 | ```python 494 | import numpy as np 495 | 496 | matrix = np.array([[1, 2], [3, 4]]) 497 | condition_number = np.linalg.cond(matrix) 498 | print(condition_number) 499 | # Output: 14.933034373659268 500 | ``` 501 | --- 502 | 503 | ### 37. How do you compute the Moore-Penrose pseudoinverse of a matrix in NumPy? 504 | **Answer:** 505 | You can compute the pseudoinverse using the `np.linalg.pinv` function. 506 | ```python 507 | import numpy as np 508 | 509 | matrix = np.array([[1, 2], [3, 4]]) 510 | pseudoinverse = np.linalg.pinv(matrix) 511 | print(pseudoinverse) 512 | # Output: [[-2. 1. ] 513 | # [ 1.5 -0.5]] 514 | ``` 515 | --- 516 | 517 | ### 38. How do you compute the covariance matrix of an array in NumPy? 518 | **Answer:** 519 | You can compute the covariance matrix using the `np.cov` function. 520 | ```python 521 | import numpy as np 522 | 523 | data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 524 | cov_matrix = np.cov(data, rowvar=False) 525 | print(cov_matrix) 526 | # Output: [[9. 9. 9. ] 527 | # [9. 9. 9. ] 528 | # [9. 9. 9. ]] 529 | ``` 530 | --- 531 | 532 | ### 39. How do you compute the correlation matrix of an array in NumPy? 533 | **Answer:** 534 | You can compute the correlation matrix using the `np.corrcoef` function. 535 | ```python 536 | import numpy as np 537 | 538 | data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 539 | corr_matrix = np.corrcoef(data, rowvar=False) 540 | print(corr_matrix) 541 | # Output: [[1 542 | 543 | . 1. 1.] 544 | # [1. 1. 1.] 545 | # [1. 1. 1.]] 546 | ``` 547 | --- 548 | 549 | ### 40. How do you compute the standard deviation along a specified axis in NumPy? 550 | **Answer:** 551 | You can compute the standard deviation using the `np.std` function. 552 | ```python 553 | import numpy as np 554 | 555 | arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 556 | std_dev = np.std(arr, axis=0) 557 | print(std_dev) 558 | # Output: [2.44948974 2.44948974 2.44948974] 559 | ``` 560 | --- 561 | 562 | ### 41. How do you compute the variance along a specified axis in NumPy? 563 | **Answer:** 564 | You can compute the variance using the `np.var` function. 565 | ```python 566 | import numpy as np 567 | 568 | arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 569 | variance = np.var(arr, axis=0) 570 | print(variance) 571 | # Output: [6. 6. 6.] 572 | ``` 573 | --- 574 | 575 | ### 42. How do you compute the median of an array along a specified axis in NumPy? 576 | **Answer:** 577 | You can compute the median using the `np.median` function. 578 | ```python 579 | import numpy as np 580 | 581 | arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 582 | median = np.median(arr, axis=0) 583 | print(median) 584 | # Output: [4. 5. 6.] 585 | ``` 586 | --- 587 | 588 | ### 43. How do you compute the mode of an array in NumPy? 589 | **Answer:** 590 | You can compute the mode using the `stats.mode` function from the `scipy` library. 591 | ```python 592 | import numpy as np 593 | from scipy import stats 594 | 595 | arr = np.array([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]) 596 | mode = stats.mode(arr) 597 | print(mode) 598 | # Output: ModeResult(mode=array([4]), count=array([4])) 599 | ``` 600 | --- 601 | 602 | ### 44. How do you generate a 2D grid of coordinates in NumPy? 603 | **Answer:** 604 | You can generate a 2D grid of coordinates using the `np.meshgrid` function. 605 | ```python 606 | import numpy as np 607 | 608 | x = np.array([1, 2, 3]) 609 | y = np.array([4, 5]) 610 | X, Y = np.meshgrid(x, y) 611 | print(X) 612 | # Output: [[1 2 3] 613 | # [1 2 3]] 614 | print(Y) 615 | # Output: [[4 4 4] 616 | # [5 5 5]] 617 | ``` 618 | --- 619 | 620 | ### 45. How do you interpolate missing values in a NumPy array? 621 | **Answer:** 622 | You can interpolate missing values using the `np.interp` function. 623 | ```python 624 | import numpy as np 625 | 626 | x = np.array([0, 1, 2, 3, 4, 5]) 627 | y = np.array([0, np.nan, 4, np.nan, 16, 25]) 628 | nans, x_ = np.isnan(y), lambda z: z.nonzero()[0] 629 | y[nans] = np.interp(x(nans), x_(~nans), y[~nans]) 630 | print(y) 631 | # Output: [ 0. 2. 4. 10. 16. 25.] 632 | ``` 633 | --- 634 | 635 | ### 46. How do you perform linear regression using NumPy? 636 | **Answer:** 637 | You can perform linear regression using the `np.polyfit` function. 638 | ```python 639 | import numpy as np 640 | 641 | x = np.array([0, 1, 2, 3, 4, 5]) 642 | y = np.array([0, 2, 4, 6, 8, 10]) 643 | coefficients = np.polyfit(x, y, 1) 644 | print(coefficients) 645 | # Output: [2.00000000e+00 4.02163285e-15] 646 | ``` 647 | --- 648 | 649 | ### 47. How do you create a sliding window view of an array in NumPy? 650 | **Answer:** 651 | You can create a sliding window view using the `np.lib.stride_tricks.sliding_window_view` function. 652 | ```python 653 | import numpy as np 654 | 655 | arr = np.array([1, 2, 3, 4, 5]) 656 | window_size = 3 657 | sliding_windows = np.lib.stride_tricks.sliding_window_view(arr, window_size) 658 | print(sliding_windows) 659 | # Output: [[1 2 3] 660 | # [2 3 4] 661 | # [3 4 5]] 662 | ``` 663 | --- 664 | 665 | ### 48. How do you find the unique rows in a 2D array in NumPy? 666 | **Answer:** 667 | You can find the unique rows using the `np.unique` function with the `axis=0` argument. 668 | ```python 669 | import numpy as np 670 | 671 | arr = np.array([[1, 2], [3, 4], [1, 2], [5, 6]]) 672 | unique_rows = np.unique(arr, axis=0) 673 | print(unique_rows) 674 | # Output: [[1 2] 675 | # [3 4] 676 | # [5 6]] 677 | ``` 678 | --- 679 | 680 | ### 49. How do you sort a 2D array by a specific column in NumPy? 681 | **Answer:** 682 | You can sort a 2D array by a specific column using the `np.argsort` function. 683 | ```python 684 | import numpy as np 685 | 686 | arr = np.array([[3, 2], [1, 4], [2, 1]]) 687 | sorted_arr = arr[arr[:, 0].argsort()] 688 | print(sorted_arr) 689 | # Output: [[1 4] 690 | # [2 1] 691 | # [3 2]] 692 | ``` 693 | --- 694 | 695 | ### 50. How do you create a boolean array where a condition is met in NumPy? 696 | **Answer:** 697 | You can create a boolean array by applying a condition directly to the array. 698 | ```python 699 | import numpy as np 700 | 701 | arr = np.array([1, 2, 3, 4, 5]) 702 | bool_arr = arr > 3 703 | print(bool_arr) 704 | # Output: [False False False True True] 705 | ``` 706 | 707 | If you found this repository helpful, please give it a star! 708 | 709 | Follow me on: 710 | - [LinkedIn](https://www.linkedin.com/in/ashish-jangra/) 711 | - [GitHub](https://github.com/AshishJangra27) 712 | - [Kaggle](https://www.kaggle.com/ashishjangra27) 713 | 714 | Stay updated with my latest content and projects! 715 | -------------------------------------------------------------------------------- /Pandas.md: -------------------------------------------------------------------------------- 1 | # 50 Pandas Interview Questions and Answers 2 | 3 | Below are 50 practical Pandas questions and answers focusing on common operations, data manipulation, and analysis techniques using Pandas DataFrames and Series in Python. 4 | 5 | Each question includes: 6 | - A scenario or requirement 7 | - A code snippet demonstrating one possible solution 8 | 9 | **Note:** For all examples, assume you have already imported Pandas as follows: 10 | ```python 11 | import pandas as pd 12 | ``` 13 | 14 | --- 15 | 16 | ### 1. How do you create a Pandas DataFrame from a dictionary? 17 | **Answer:** 18 | Use `pd.DataFrame()` with a dictionary of lists or values. 19 | ```python 20 | data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]} 21 | df = pd.DataFrame(data) 22 | print(df) 23 | ``` 24 | 25 | --- 26 | 27 | ### 2. How do you read a CSV file into a DataFrame? 28 | **Answer:** 29 | Use `pd.read_csv()` with the file path. 30 | ```python 31 | df = pd.read_csv('data.csv') 32 | print(df.head()) 33 | ``` 34 | 35 | --- 36 | 37 | ### 3. How do you write a DataFrame to a CSV file? 38 | **Answer:** 39 | Use `df.to_csv()` with a file path. 40 | ```python 41 | df.to_csv('output.csv', index=False) 42 | ``` 43 | 44 | --- 45 | 46 | ### 4. How do you get the first 5 rows of a DataFrame? 47 | **Answer:** 48 | Use `df.head()`. 49 | ```python 50 | print(df.head()) 51 | ``` 52 | 53 | --- 54 | 55 | ### 5. How do you get the shape (rows, columns) of a DataFrame? 56 | **Answer:** 57 | Use `df.shape`. 58 | ```python 59 | print(df.shape) 60 | ``` 61 | 62 | --- 63 | 64 | ### 6. How do you select a single column from a DataFrame? 65 | **Answer:** 66 | Use `df['column_name']`. 67 | ```python 68 | ages = df['Age'] 69 | print(ages) 70 | ``` 71 | 72 | --- 73 | 74 | ### 7. How do you select multiple columns from a DataFrame? 75 | **Answer:** 76 | Use a list of column names. 77 | ```python 78 | subset = df[['Name', 'Age']] 79 | print(subset) 80 | ``` 81 | 82 | --- 83 | 84 | ### 8. How do you select rows by index range? 85 | **Answer:** 86 | Use slicing with `df.iloc`. 87 | ```python 88 | rows = df.iloc[0:3] # first 3 rows 89 | print(rows) 90 | ``` 91 | 92 | --- 93 | 94 | ### 9. How do you select rows based on a condition? 95 | **Answer:** 96 | Use boolean indexing. 97 | ```python 98 | adults = df[df['Age'] > 18] 99 | print(adults) 100 | ``` 101 | 102 | --- 103 | 104 | ### 10. How do you filter rows based on multiple conditions? 105 | **Answer:** 106 | Use `&` or `|` operators with parentheses. 107 | ```python 108 | older_males = df[(df['Age'] > 25) & (df['Gender'] == 'Male')] 109 | print(older_males) 110 | ``` 111 | 112 | --- 113 | 114 | ### 11. How do you reset the index of a DataFrame? 115 | **Answer:** 116 | Use `df.reset_index()`. 117 | ```python 118 | df_reset = df.reset_index(drop=True) 119 | print(df_reset) 120 | ``` 121 | 122 | --- 123 | 124 | ### 12. How do you sort a DataFrame by a column? 125 | **Answer:** 126 | Use `df.sort_values()`. 127 | ```python 128 | df_sorted = df.sort_values('Age') 129 | print(df_sorted) 130 | ``` 131 | 132 | --- 133 | 134 | ### 13. How do you sort by multiple columns? 135 | **Answer:** 136 | Pass a list of column names to `sort_values()`. 137 | ```python 138 | df_sorted = df.sort_values(['Gender', 'Age'], ascending=[True, False]) 139 | print(df_sorted) 140 | ``` 141 | 142 | --- 143 | 144 | ### 14. How do you drop a column from a DataFrame? 145 | **Answer:** 146 | Use `df.drop()` with `axis=1`. 147 | ```python 148 | df_dropped = df.drop('Age', axis=1) 149 | print(df_dropped) 150 | ``` 151 | 152 | --- 153 | 154 | ### 15. How do you drop rows with missing values? 155 | **Answer:** 156 | Use `df.dropna()`. 157 | ```python 158 | df_clean = df.dropna() 159 | print(df_clean) 160 | ``` 161 | 162 | --- 163 | 164 | ### 16. How do you fill missing values with a constant? 165 | **Answer:** 166 | Use `df.fillna(value)`. 167 | ```python 168 | df_filled = df.fillna(0) 169 | print(df_filled) 170 | ``` 171 | 172 | --- 173 | 174 | ### 17. How do you fill missing values with the mean of a column? 175 | **Answer:** 176 | Calculate mean and then use `fillna()`. 177 | ```python 178 | mean_age = df['Age'].mean() 179 | df['Age'] = df['Age'].fillna(mean_age) 180 | print(df) 181 | ``` 182 | 183 | --- 184 | 185 | ### 18. How do you group a DataFrame by a column and compute an aggregation? 186 | **Answer:** 187 | Use `df.groupby('column').agg('function')`. 188 | ```python 189 | avg_age = df.groupby('Gender')['Age'].mean() 190 | print(avg_age) 191 | ``` 192 | 193 | --- 194 | 195 | ### 19. How do you get the unique values of a column? 196 | **Answer:** 197 | Use `df['column'].unique()`. 198 | ```python 199 | unique_names = df['Name'].unique() 200 | print(unique_names) 201 | ``` 202 | 203 | --- 204 | 205 | ### 20. How do you count the number of unique values in a column? 206 | **Answer:** 207 | Use `df['column'].nunique()`. 208 | ```python 209 | count_unique = df['Name'].nunique() 210 | print(count_unique) 211 | ``` 212 | 213 | --- 214 | 215 | ### 21. How do you rename a column in a DataFrame? 216 | **Answer:** 217 | Use `df.rename(columns={'old':'new'})`. 218 | ```python 219 | df = df.rename(columns={'Name': 'FullName'}) 220 | print(df.head()) 221 | ``` 222 | 223 | --- 224 | 225 | ### 22. How do you apply a function to every element of a column? 226 | **Answer:** 227 | Use `df['column'].apply(function)`. 228 | ```python 229 | df['AgePlusOne'] = df['Age'].apply(lambda x: x + 1) 230 | print(df.head()) 231 | ``` 232 | 233 | --- 234 | 235 | ### 23. How do you create a new column based on a condition? 236 | **Answer:** 237 | Use `np.where` or apply a function. 238 | ```python 239 | import numpy as np 240 | df['Adult'] = np.where(df['Age'] >= 18, 'Yes', 'No') 241 | print(df.head()) 242 | ``` 243 | 244 | --- 245 | 246 | ### 24. How do you combine two DataFrames vertically (append rows)? 247 | **Answer:** 248 | Use `pd.concat([df1, df2])`. 249 | ```python 250 | df_combined = pd.concat([df1, df2], ignore_index=True) 251 | print(df_combined) 252 | ``` 253 | 254 | --- 255 | 256 | ### 25. How do you merge two DataFrames on a common column? 257 | **Answer:** 258 | Use `pd.merge(df1, df2, on='column')`. 259 | ```python 260 | merged = pd.merge(df1, df2, on='ID') 261 | print(merged.head()) 262 | ``` 263 | 264 | --- 265 | 266 | ### 26. How do you get the summary statistics of a DataFrame? 267 | **Answer:** 268 | Use `df.describe()`. 269 | ```python 270 | stats = df.describe() 271 | print(stats) 272 | ``` 273 | 274 | --- 275 | 276 | ### 27. How do you check the data types of each column? 277 | **Answer:** 278 | Use `df.dtypes`. 279 | ```python 280 | print(df.dtypes) 281 | ``` 282 | 283 | --- 284 | 285 | ### 28. How do you change the data type of a column? 286 | **Answer:** 287 | Use `df['column'].astype(dtype)`. 288 | ```python 289 | df['Age'] = df['Age'].astype(int) 290 | ``` 291 | 292 | --- 293 | 294 | ### 29. How do you set a column as the index of the DataFrame? 295 | **Answer:** 296 | Use `df.set_index('column')`. 297 | ```python 298 | df_indexed = df.set_index('ID') 299 | print(df_indexed.head()) 300 | ``` 301 | 302 | --- 303 | 304 | ### 30. How do you reset the index and drop the old index? 305 | **Answer:** 306 | Use `df.reset_index(drop=True)`. 307 | ```python 308 | df_reset = df.reset_index(drop=True) 309 | ``` 310 | 311 | --- 312 | 313 | ### 31. How do you count the number of missing values in each column? 314 | **Answer:** 315 | Use `df.isna().sum()`. 316 | ```python 317 | missing_counts = df.isna().sum() 318 | print(missing_counts) 319 | ``` 320 | 321 | --- 322 | 323 | ### 32. How do you select rows by label using `.loc`? 324 | **Answer:** 325 | Use `df.loc[label]` for index labels. 326 | ```python 327 | row_data = df.loc[0] # if 0 is an index label 328 | print(row_data) 329 | ``` 330 | 331 | --- 332 | 333 | ### 33. How do you select rows by integer position using `.iloc`? 334 | **Answer:** 335 | Use `df.iloc[index_position]`. 336 | ```python 337 | row_data = df.iloc[0] 338 | print(row_data) 339 | ``` 340 | 341 | --- 342 | 343 | ### 34. How do you remove duplicate rows? 344 | **Answer:** 345 | Use `df.drop_duplicates()`. 346 | ```python 347 | df_unique = df.drop_duplicates() 348 | ``` 349 | 350 | --- 351 | 352 | ### 35. How do you find correlation between columns? 353 | **Answer:** 354 | Use `df.corr()`. 355 | ```python 356 | correlation = df.corr() 357 | print(correlation) 358 | ``` 359 | 360 | --- 361 | 362 | ### 36. How do you create a pivot table? 363 | **Answer:** 364 | Use `pd.pivot_table()`. 365 | ```python 366 | pivot = pd.pivot_table(df, values='Sales', index='Region', columns='Product', aggfunc='sum') 367 | print(pivot) 368 | ``` 369 | 370 | --- 371 | 372 | ### 37. How do you replace values in a column based on a condition? 373 | **Answer:** 374 | Use `df.loc[condition, 'column'] = new_value`. 375 | ```python 376 | df.loc[df['Age'] < 0, 'Age'] = 0 377 | ``` 378 | 379 | --- 380 | 381 | ### 38. How do you extract a substring from a column of strings? 382 | **Answer:** 383 | Use `str` accessor. 384 | ```python 385 | df['Initial'] = df['Name'].str[0] 386 | print(df.head()) 387 | ``` 388 | 389 | --- 390 | 391 | ### 39. How do you convert a column to datetime? 392 | **Answer:** 393 | Use `pd.to_datetime()`. 394 | ```python 395 | df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d') 396 | ``` 397 | 398 | --- 399 | 400 | ### 40. How do you filter rows based on a substring in a column? 401 | **Answer:** 402 | Use `df['column'].str.contains('pattern')`. 403 | ```python 404 | filtered = df[df['Name'].str.contains('A')] 405 | print(filtered) 406 | ``` 407 | 408 | --- 409 | 410 | ### 41. How do you change column names to lowercase? 411 | **Answer:** 412 | Use a list comprehension on `df.columns`. 413 | ```python 414 | df.columns = [col.lower() for col in df.columns] 415 | ``` 416 | 417 | --- 418 | 419 | ### 42. How do you find the index of the maximum value in a column? 420 | **Answer:** 421 | Use `df['column'].idxmax()`. 422 | ```python 423 | max_index = df['Age'].idxmax() 424 | print(max_index) 425 | ``` 426 | 427 | --- 428 | 429 | ### 43. How do you sample random rows from a DataFrame? 430 | **Answer:** 431 | Use `df.sample(n=number)`. 432 | ```python 433 | sampled = df.sample(n=5) 434 | print(sampled) 435 | ``` 436 | 437 | --- 438 | 439 | ### 44. How do you apply a custom function to a DataFrame row-wise? 440 | **Answer:** 441 | Use `df.apply(function, axis=1)`. 442 | ```python 443 | def sum_age(row): 444 | return row['Age'] + 10 445 | 446 | df['Age_plus_10'] = df.apply(sum_age, axis=1) 447 | ``` 448 | 449 | --- 450 | 451 | ### 45. How do you create dummy variables for categorical columns? 452 | **Answer:** 453 | Use `pd.get_dummies()`. 454 | ```python 455 | df_dummies = pd.get_dummies(df, columns=['Gender']) 456 | print(df_dummies.head()) 457 | ``` 458 | 459 | --- 460 | 461 | ### 46. How do you combine text columns into one column? 462 | **Answer:** 463 | Use string concatenation. 464 | ```python 465 | df['FullInfo'] = df['Name'] + ' - ' + df['City'] 466 | ``` 467 | 468 | --- 469 | 470 | ### 47. How do you find rows with any missing values? 471 | **Answer:** 472 | Use `df[df.isna().any(axis=1)]`. 473 | ```python 474 | missing_rows = df[df.isna().any(axis=1)] 475 | print(missing_rows) 476 | ``` 477 | 478 | --- 479 | 480 | ### 48. How do you find rows where all values are missing? 481 | **Answer:** 482 | Use `df[df.isna().all(axis=1)]`. 483 | ```python 484 | all_missing = df[df.isna().all(axis=1)] 485 | print(all_missing) 486 | ``` 487 | 488 | --- 489 | 490 | ### 49. How do you remove rows based on a condition? 491 | **Answer:** 492 | Filter out the condition. 493 | ```python 494 | df_filtered = df[df['Age'] >= 18] 495 | print(df_filtered) 496 | ``` 497 | 498 | --- 499 | 500 | ### 50. How do you assign values to a column conditionally using `.loc`? 501 | **Answer:** 502 | Use `df.loc[condition, 'column'] = value`. 503 | ```python 504 | df.loc[df['Age'] > 30, 'Category'] = 'Senior' 505 | print(df.head()) 506 | ``` 507 | 508 | --- 509 | 510 | 511 | If you found this repository helpful, please give it a star! 512 | 513 | Follow me on: 514 | - [LinkedIn](https://www.linkedin.com/in/ashish-jangra/) 515 | - [GitHub](https://github.com/AshishJangra27) 516 | - [Kaggle](https://www.kaggle.com/ashishjangra27) 517 | 518 | Stay updated with my latest content and projects! 519 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Your Ultimate Guide to Interview Questions 2 | 3 | #### Preparing for your next big interview? Look no further! Dive into these carefully curated sets of interview questions and answers, designed to help you shine in your interviews. Get ready to impress with your knowledge! 4 | 5 | --- 6 | 7 | #### [1. Excel for Data Analytics (100 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/Excel%20for%20Data%20Analytics.md) 8 | #### [2. Python Fundamentals (100 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/Python%20Fundamentals.md) 9 | #### [3. Conditions in Python (50 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/Conditions%20in%20Python.md) 10 | #### [4. Dictionaries in Python (50 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/Dictionaries%20in%20Python.md) 11 | #### [5. Lists in Python (50 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/Lists%20in%20Python.md) 12 | #### [6. Strings in Python (50 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/Strings%20in%20Python.md) 13 | #### [7. NumPy (50 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/NumPy.md) 14 | #### [8. Pandas (50 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/Pandas.md) 15 | #### [9. Matplotlib (50 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/Matplotlib.md) 16 | #### [10. SQL Fundamentals (100 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/SQL%20Fundamentals.md) 17 | #### [11. SQL for Data Analytics (100 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/SQL%20for%20Data%20Analytics.md) 18 | #### [12. Data Analysis with Python (100 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/Data%20Analysis%20with%20Python.md) 19 | #### [13. SQL in Action (25 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/SQL%20in%20Action.md) 20 | #### [14. Machine Learning (100 Questions)](https://github.com/AshishJangra27/Interview-Questions/blob/main/Machine%20Learning.md) 21 | 22 | --- 23 | 24 | ### Why do you think you should explore these resources? 25 | 26 | - **Comprehensive Coverage:** Each guide has questions covering basic to advanced levels. 27 | - **Practical Examples:** Real-world examples to help you understand and apply concepts effectively. 28 | - **Easy to Follow:** Simple language and clear explanations make these guides easy to understand, even if you're just starting. 29 | - **Focused Topics:** Each guide focuses on specific topics, making targeting your study and preparation easier. 30 | 31 | --- 32 | 33 | If you found these resources helpful, please give them a star! ⭐ 34 | 35 | Follow me on: 36 | - [LinkedIn](https://www.linkedin.com/in/ashish-jangra/) 37 | - [GitHub](https://github.com/AshishJangra27) 38 | - [Kaggle](https://www.kaggle.com/ashishjangra27) 39 | 40 | Stay updated with my latest content and projects! Your support helps me create more valuable resources for everyone. Let's ace those interviews together! 41 | -------------------------------------------------------------------------------- /SQL in Action.md: -------------------------------------------------------------------------------- 1 | # 25 Practical SQL Questions on Sales Table 2 | --- 3 | 4 | ## Dataset 5 | **sales** 6 | 7 | | sale_id | sale_date | sale_time | product_id | product_name | customer_id | customer_name | quantity | price | total_amount | 8 | |---------|------------|------------|------------|--------------|-------------|---------------|----------|--------|--------------| 9 | | 1 | 2023-01-01 | 10:30:00 | 101 | Laptop | 201 | Arjun | 2 | 50000 | 95000 | 10 | | 2 | 2023-01-02 | 11:15:00 | 102 | Mobile | 202 | Bhavya | 1 | 20000 | 18000 | 11 | | 3 | 2023-01-03 | 12:00:00 | 103 | Tablet | 203 | Chaitanya | 3 | 15000 | 40500 | 12 | | 4 | 2023-01-04 | 13:45:00 | 104 | TV | 204 | Deepak | 1 | 30000 | 27000 | 13 | | 5 | 2023-01-05 | 14:30:00 | 105 | Headphones | 205 | Esha | 4 | 2000 | 7200 | 14 | | 6 | 2023-01-06 | 15:15:00 | 101 | Laptop | 206 | Farhan | 2 | 50000 | 95000 | 15 | | 7 | 2023-01-07 | 16:00:00 | 102 | Mobile | 207 | Gauri | 1 | 20000 | 18000 | 16 | | 8 | 2023-01-08 | 17:30:00 | 103 | Tablet | 208 | Harsh | 3 | 15000 | 40500 | 17 | | 9 | 2023-01-09 | 18:45:00 | 104 | TV | 209 | Ishita | 1 | 30000 | 27000 | 18 | | 10 | 2023-01-10 | 19:15:00 | 105 | Headphones | 210 | Jai | 4 | 2000 | 7200 | 19 | 20 | --- 21 | 22 | ### Questions 23 | 24 | #### 1. Calculate the total sales for each product. 25 | 26 | **Answer:** 27 | ```sql 28 | SELECT product_name, SUM(total_amount) AS total_sales 29 | FROM sales 30 | GROUP BY product_name; 31 | ``` 32 | 33 | **Output:** 34 | 35 | | product_name | total_sales | 36 | |--------------|-------------| 37 | | Laptop | 190000 | 38 | | Mobile | 36000 | 39 | | Tablet | 81000 | 40 | | TV | 54000 | 41 | | Headphones | 14400 | 42 | 43 | --- 44 | 45 | #### 2. Find the average discount applied for each product. 46 | 47 | **Answer:** 48 | ```sql 49 | SELECT product_name, AVG(discount) AS average_discount 50 | FROM sales 51 | GROUP BY product_name; 52 | ``` 53 | 54 | **Output:** 55 | 56 | | product_name | average_discount | 57 | |--------------|------------------| 58 | | Laptop | 5000 | 59 | | Mobile | 2000 | 60 | | Tablet | 4500 | 61 | | TV | 3000 | 62 | | Headphones | 200 | 63 | 64 | --- 65 | 66 | #### 3. Calculate the total sales for each payment method. 67 | 68 | **Answer:** 69 | ```sql 70 | SELECT payment_method, SUM(total_amount) AS total_sales 71 | FROM sales 72 | GROUP BY payment_method; 73 | ``` 74 | 75 | **Output:** 76 | 77 | | payment_method | total_sales | 78 | |----------------|-------------| 79 | | Credit Card | 266250 | 80 | | Debit Card | 134700 | 81 | | Cash | 54000 | 82 | 83 | --- 84 | 85 | #### 4. Find the customer who made the highest number of purchases. 86 | 87 | **Answer:** 88 | ```sql 89 | SELECT customer_name, COUNT(*) AS purchase_count 90 | FROM sales 91 | GROUP BY customer_name 92 | ORDER BY purchase_count DESC 93 | LIMIT 1; 94 | ``` 95 | 96 | **Output:** 97 | 98 | | customer_name | purchase_count | 99 | |---------------|----------------| 100 | | Arjun | 2 | 101 | 102 | --- 103 | 104 | #### 5. Calculate the total sales and average sales per day. 105 | 106 | **Answer:** 107 | ```sql 108 | SELECT sale_date, SUM(total_amount) AS total_sales, AVG(total_amount) AS average_sales 109 | FROM sales 110 | GROUP BY sale_date; 111 | ``` 112 | 113 | **Output:** 114 | 115 | | sale_date | total_sales | average_sales | 116 | |------------|-------------|---------------| 117 | | 2023-01-01 | 95000 | 95000 | 118 | | 2023-01-02 | 18000 | 18000 | 119 | | 2023-01-03 | 40500 | 40500 | 120 | | 2023-01-04 | 27000 | 27000 | 121 | | 2023-01-05 | 7200 | 7200 | 122 | | 2023-01-06 | 95000 | 95000 | 123 | | 2023-01-07 | 18000 | 18000 | 124 | | 2023-01-08 | 40500 | 40500 | 125 | | 2023-01-09 | 27000 | 27000 | 126 | | 2023-01-10 | 7200 | 7200 | 127 | 128 | --- 129 | 130 | #### 6. Calculate the cumulative sales amount for each product by date, considering the discounts applied. 131 | 132 | **Answer:** 133 | ```sql 134 | SELECT 135 | sale_date, 136 | product_name, 137 | total_amount, 138 | SUM(total_amount) OVER (PARTITION BY product_name ORDER BY sale_date) AS cumulative_sales_amount 139 | FROM 140 | sales 141 | ORDER BY 142 | product_name, sale_date; 143 | ``` 144 | 145 | **Output:** 146 | 147 | | sale_date | product_name | total_amount | cumulative_sales_amount | 148 | |------------|--------------|--------------|-------------------------| 149 | | 2023-01-01 | Laptop | 95000 | 95000 | 150 | | 2023-01-06 | Laptop | 95000 | 190000 | 151 | | 2023-01-02 | Mobile | 18000 | 18000 | 152 | | 2023-01-07 | Mobile | 18000 | 36000 | 153 | | 2023-01-03 | Tablet | 40500 | 40500 | 154 | | 2023-01-08 | Tablet | 40500 | 81000 | 155 | | 2023-01-04 | TV | 27000 | 27000 | 156 | | 2023-01-09 | TV | 27000 | 54000 | 157 | | 2023-01-05 | Headphones | 7200 | 7200 | 158 | | 2023-01-10 | Headphones | 7200 | 14400 | 159 | 160 | --- 161 | 162 | #### 7. Find the top 3 products with the highest total sales amount. 163 | 164 | **Answer:** 165 | ```sql 166 | SELECT 167 | product_name, 168 | SUM(total_amount) AS total_sales_amount 169 | FROM 170 | sales 171 | GROUP BY 172 | product_name 173 | ORDER BY 174 | total_sales_amount DESC 175 | LIMIT 3; 176 | ``` 177 | 178 | **Output:** 179 | 180 | | product_name | total_sales_amount | 181 | |--------------|--------------------| 182 | | Laptop | 190000 | 183 | | Tablet | 81000 | 184 | | TV | 54000 | 185 | 186 | --- 187 | 188 | #### 8. Calculate the month-over-month sales growth for each product. 189 | 190 | **Answer:** 191 | ```sql 192 | SELECT 193 | product_name, 194 | DATE_TRUNC('month', sale_date) AS month, 195 | SUM(total_amount) AS monthly_sales, 196 | LAG(SUM(total_amount)) OVER (PARTITION BY product_name ORDER BY DATE_TRUNC('month', sale_date)) AS previous_month_sales, 197 | (SUM(total_amount) - LAG(SUM(total_amount)) OVER (PARTITION BY product_name ORDER BY DATE_TRUNC('month', sale_date))) / NULLIF(LAG(SUM(total_amount)) OVER (PARTITION BY product_name ORDER BY DATE_TRUNC('month', sale_date)), 0) * 100 AS month_over_month_growth 198 | FROM 199 | sales 200 | GROUP BY 201 | product_name, DATE_TRUNC('month', sale_date) 202 | ORDER BY 203 | product_name, month; 204 | ``` 205 | 206 | **Output:** 207 | 208 | | product_name | month | monthly_sales | previous_month_sales | month_over_month_growth | 209 | |--------------|-----------|---------------|----------------------|-------------------------| 210 | | Laptop | 2023-01-01| 95000 | NULL | NULL | 211 | | Laptop | 2023-02-01| 95000 | 95000 | 0.00 | 212 | | Mobile | 2023-01-01| 18000 | NULL | NULL | 213 | | Mobile | 2023-02-01| 18000 | 18000 | 0.00 | 214 | | Tablet | 2023-01-01| 40500 | NULL | NULL | 215 | | Tablet | 2023-02-01| 40500 | 40500 | 0.00 | 216 | | TV | 2023-01-01| 27000 | NULL | NULL | 217 | | TV | 2023-02-01| 27000 | 27000 | 0.00 | 218 | | Headphones | 2023-01-01| 7200 | NULL | NULL | 219 | | Headphones | 2023-02-01| 7200 | 7200 | 0.00 | 220 | 221 | --- 222 | 223 | #### 9. Identify the top 5 customers who have spent the most on products. 224 | 225 | **Answer:** 226 | ```sql 227 | SELECT 228 | customer_name, 229 | SUM(total_amount) AS total_spent 230 | FROM 231 | sales 232 | GROUP BY 233 | customer_name 234 | ORDER BY 235 | total_spent DESC 236 | LIMIT 5; 237 | ``` 238 | 239 | **Output:** 240 | 241 | | customer_name | total_spent | 242 | |---------------|-------------| 243 | | Arjun | 95000 | 244 | | Farhan | 95000 | 245 | | Chaitanya | 40500 | 246 | | Harsh | 40500 | 247 | | Deepak | 27000 | 248 | 249 | --- 250 | 251 | #### 10. Calculate the average discount percentage applied for each product. 252 | 253 | **Answer:** 254 | ```sql 255 | SELECT 256 | product_name, 257 | AVG(discount / (price * quantity) * 100) AS average_discount_percentage 258 | FROM 259 | sales 260 | GROUP BY 261 | product_name; 262 | ``` 263 | 264 | **Output:** 265 | 266 | | product_name | average_discount_percentage | 267 | |--------------|-----------------------------| 268 | | Laptop | 5.00 | 269 | | Mobile | 10.00 | 270 | | Tablet | 10.00 | 271 | | TV | 10.00 | 272 | | Headphones | 2.50 | 273 | 274 | --- 275 | 276 | #### 11. Find the product with the highest average sale amount per transaction. 277 | 278 | **Answer:** 279 | ```sql 280 | SELECT 281 | product_name, 282 | AVG(total_amount) AS average_sale_amount 283 | FROM 284 | sales 285 | GROUP BY 286 | product_name 287 | ORDER BY 288 | average_sale_amount DESC 289 | LIMIT 1; 290 | ``` 291 | 292 | **Output:** 293 | 294 | | product_name | average_sale_amount | 295 | |--------------|---------------------| 296 | | Laptop | 95000.00 | 297 | 298 | --- 299 | 300 | #### 12. Calculate the total revenue generated by each product category (assuming categories are available in a separate table). 301 | 302 | **Answer:** 303 | ```sql 304 | -- Assuming we have a categories table 305 | -- categories 306 | -- | category_id | category_name | 307 | -- |-------------|---------------| 308 | -- | 1 | Electronics | 309 | -- | 2 | Accessories | 310 | 311 | -- And a product_category table 312 | -- product_category 313 | -- | product_id | category_id | 314 | -- |------------|-------------| 315 | -- | 101 | 1 | 316 | -- | 102 | 1 | 317 | -- | 103 | 1 | 318 | -- | 104 | 1 | 319 | -- | 105 | 2 | 320 | 321 | SELECT 322 | c.category_name, 323 | SUM(s.total_amount) AS total_revenue 324 | FROM 325 | sales s 326 | JOIN 327 | product_category pc ON s.product_id = pc.product_id 328 | JOIN 329 | categories c ON pc.category_id = c.category_id 330 | GROUP BY 331 | c.category_name; 332 | ``` 333 | 334 | **Output:** 335 | 336 | | category_name | total_revenue | 337 | |---------------|---------------| 338 | | Electronics | 324000 | 339 | | Accessories | 14400 | 340 | 341 | --- 342 | 343 | #### 13. Determine the average number of products sold per transaction for each product. 344 | 345 | **Answer:** 346 | ```sql 347 | SELECT 348 | product_name, 349 | AVG(quantity) AS average_quantity_sold 350 | FROM 351 | sales 352 | GROUP BY 353 | product_name; 354 | ``` 355 | 356 | **Output:** 357 | 358 | | product_name | average_quantity_sold | 359 | |--------------|-----------------------| 360 | | Laptop | 2.00 | 361 | | Mobile | 1.00 | 362 | | Tablet | 3.00 | 363 | | TV | 1.00 | 364 | | Headphones | 4.00 | 365 | 366 | --- 367 | 368 | #### 14. Find the date with the highest total sales amount across all products. 369 | 370 | **Answer:** 371 | ```sql 372 | SELECT 373 | sale_date, 374 | SUM(total_amount) AS daily_sales_amount 375 | FROM 376 | sales 377 | GROUP BY 378 | sale_date 379 | ORDER BY 380 | daily_sales_amount DESC 381 | LIMIT 1; 382 | ``` 383 | 384 | **Output:** 385 | 386 | | sale_date | daily_sales_amount | 387 | |------------|--------------------| 388 | | 2023-01-01 | 95000 | 389 | 390 | --- 391 | 392 | #### 15. Calculate the percentage of total revenue contributed by each product. 393 | 394 | **Answer:** 395 | ```sql 396 | WITH total_revenue AS ( 397 | SELECT 398 | SUM(total_amount) AS total_sales 399 | FROM 400 | sales 401 | ) 402 | SELECT 403 | product_name, 404 | SUM(total_amount) AS product_sales, 405 | (SUM(total_amount) / total_sales.total_sales) * 100 AS revenue_percentage 406 | FROM 407 | sales, total_revenue 408 | GROUP BY 409 | product_name, total_sales.total_sales 410 | ORDER BY 411 | revenue_percentage DESC; 412 | ``` 413 | 414 | **Output:** 415 | 416 | | product_name | product_sales | revenue_percentage | 417 | |--------------|---------------|--------------------| 418 | | Laptop | 190000 | 38.89 | 419 | | Tablet | 81000 | 16.58 | 420 | | TV | 54000 | 11.05 | 421 | | Mobile | 36000 | 7.37 | 422 | | Headphones | 14400 | 2.95 | 423 | 424 | --- 425 | 426 | #### 16. Find the customer who made the most purchases. 427 | 428 | **Answer:** 429 | ```sql 430 | SELECT 431 | customer_name, 432 | COUNT(*) AS purchase_count 433 | FROM 434 | sales 435 | GROUP BY 436 | customer_name 437 | ORDER BY 438 | purchase_count DESC 439 | LIMIT 1; 440 | ``` 441 | 442 | **Output:** 443 | 444 | | customer_name | purchase_count | 445 | |---------------|----------------| 446 | | Arjun | 2 | 447 | 448 | --- 449 | 450 | #### 17. Calculate the average sale amount per payment method. 451 | 452 | **Answer:** 453 | ```sql 454 | SELECT 455 | payment_method, 456 | AVG(total_amount) AS average_sale_amount 457 | FROM 458 | sales 459 | GROUP BY 460 | payment_method; 461 | ``` 462 | 463 | **Output:** 464 | 465 | | payment_method | average_sale_amount | 466 | |----------------|---------------------| 467 | | Credit Card | 22187.50 | 468 | | Debit Card | 13470.00 | 469 | | Cash | 10800.00 | 470 | 471 | --- 472 | 473 | #### 18. Find the most frequently purchased product. 474 | 475 | **Answer:** 476 | ```sql 477 | SELECT 478 | product_name, 479 | COUNT(*) AS purchase_count 480 | FROM 481 | sales 482 | GROUP BY 483 | product_name 484 | ORDER BY 485 | purchase_count DESC 486 | LIMIT 1; 487 | ``` 488 | 489 | **Output:** 490 | 491 | | product_name | purchase_count | 492 | |--------------|----------------| 493 | | Laptop | 2 | 494 | 495 | --- 496 | 497 | #### 19. Calculate the total number of products sold for each day of the week. 498 | 499 | **Answer:** 500 | ```sql 501 | SELECT 502 | DAYOFWEEK(sale_date) AS day_of_week, 503 | SUM(quantity) AS total_quantity_sold 504 | FROM 505 | sales 506 | GROUP BY 507 | DAYOFWEEK(sale_date); 508 | ``` 509 | 510 | **Output:** 511 | 512 | | day_of_week | total_quantity_sold | 513 | |-------------|---------------------| 514 | | 1 | 5 | 515 | | 2 | 3 | 516 | | 3 | 4 | 517 | | 4 | 7 | 518 | | 5 | 3 | 519 | | 6 | 4 | 520 | | 7 | 5 | 521 | 522 | --- 523 | 524 | #### 20. Find the total sales amount for each product category per month. 525 | 526 | **Answer:** 527 | ```sql 528 | SELECT 529 | c.category_name, 530 | DATE_TRUNC('month', s.sale_date) AS month, 531 | SUM(s.total_amount) AS total_sales 532 | FROM 533 | sales s 534 | JOIN 535 | product_category pc ON s.product_id = pc.product_id 536 | JOIN 537 | categories c ON pc.category_id = c.category_id 538 | GROUP BY 539 | c.category_name, DATE_TRUNC('month', s.sale_date) 540 | ORDER BY 541 | c.category_name, month; 542 | ``` 543 | 544 | **Output:** 545 | 546 | | category_name | month | total_sales | 547 | |---------------|-----------|-------------| 548 | | Accessories | 2023-01-01| 14400 | 549 | | Electronics | 2023-01-01| 324000 | 550 | 551 | --- 552 | 553 | #### 21. Calculate the number of distinct customers who made purchases each month. 554 | 555 | **Answer:** 556 | ```sql 557 | SELECT 558 | DATE_TRUNC('month', sale_date) AS month, 559 | COUNT(DISTINCT customer_id) AS distinct_customers 560 | FROM 561 | sales 562 | GROUP BY 563 | DATE_TRUNC('month', sale_date); 564 | ``` 565 | 566 | **Output:** 567 | 568 | | month | distinct_customers | 569 | |-----------|--------------------| 570 | | 2023-01-01| 10 | 571 | 572 | --- 573 | 574 | #### 22. Find the total sales amount and the number of sales for each customer. 575 | 576 | **Answer:** 577 | ```sql 578 | SELECT 579 | customer_name, 580 | SUM(total_amount) AS total_sales, 581 | COUNT(*) AS number_of_sales 582 | FROM 583 | sales 584 | GROUP BY 585 | customer_name; 586 | ``` 587 | 588 | **Output:** 589 | 590 | | customer_name | total_sales | number_of_sales | 591 | |---------------|-------------|-----------------| 592 | | Arjun | 95000 | 1 | 593 | | Bhavya | 18000 | 1 | 594 | | Chaitanya | 40500 | 1 | 595 | | Deepak | 27000 | 1 | 596 | | Esha | 7200 | 1 | 597 | | Farhan | 95000 | 1 | 598 | | Gauri | 18000 | 1 | 599 | | Harsh | 40500 | 1 | 600 | | Ishita | 27000 | 1 | 601 | | Jai | 7200 | 1 | 602 | 603 | --- 604 | 605 | #### 23. Calculate the total sales amount for each product on each day. 606 | 607 | **Answer:** 608 | ```sql 609 | SELECT 610 | sale_date, 611 | product_name, 612 | SUM(total_amount) AS total_sales 613 | FROM 614 | sales 615 | GROUP BY 616 | sale_date, product_name 617 | ORDER BY 618 | sale_date, product_name; 619 | ``` 620 | 621 | **Output:** 622 | 623 | | sale_date | product_name | total_sales | 624 | |------------|--------------|-------------| 625 | | 2023-01-01 | Laptop | 95000 | 626 | | 2023-01-02 | Mobile | 18000 | 627 | | 2023-01-03 | Tablet | 40500 | 628 | | 2023-01-04 | TV | 27000 | 629 | | 2023-01-05 | Headphones | 7200 | 630 | | 2023-01-06 | Laptop | 95000 | 631 | | 2023-01-07 | Mobile | 18000 | 632 | | 2023-01-08 | Tablet | 40500 | 633 | | 2023-01-09 | TV | 27000 | 634 | | 2023-01-10 | Headphones | 7200 | 635 | 636 | --- 637 | 638 | #### 24. Find the average sale amount per transaction for each product. 639 | 640 | **Answer:** 641 | ```sql 642 | SELECT 643 | product_name, 644 | AVG(total_amount) AS average_sale_amount 645 | FROM 646 | sales 647 | GROUP BY 648 | product_name; 649 | ``` 650 | 651 | **Output:** 652 | 653 | | product_name | average_sale_amount | 654 | |--------------|---------------------| 655 | | Laptop | 95000.00 | 656 | | Mobile | 18000.00 | 657 | | Tablet | 40500.00 | 658 | | TV | 27000.00 | 659 | | Headphones | 7200.00 | 660 | 661 | --- 662 | 663 | #### 25. Calculate the cumulative sales amount for each product category by date. 664 | 665 | **Answer:** 666 | ```sql 667 | SELECT 668 | s.sale_date, 669 | c.category_name, 670 | SUM(s.total_amount) AS daily_sales, 671 | SUM(SUM(s.total_amount)) OVER (PARTITION BY c.category_name ORDER BY s.sale_date) AS cumulative_sales_amount 672 | FROM 673 | sales s 674 | JOIN 675 | product_category pc ON s.product_id = pc.product_id 676 | JOIN 677 | categories c ON pc.category_id = c.category_id 678 | GROUP BY 679 | s.sale_date, c.category_name 680 | ORDER BY 681 | c.category_name, s.sale_date; 682 | ``` 683 | 684 | **Output:** 685 | 686 | | sale_date | category_name | daily_sales | cumulative_sales_amount | 687 | |------------|---------------|-------------|-------------------------| 688 | | 2023-01-01 | Electronics | 27000 | 27000 | 689 | | 2023-01-02 | Electronics | 18000 | 45000 | 690 | | 2023-01-03 | Electronics | 40500 | 85500 | 691 | | 2023-01-04 | Electronics | 27000 | 112500 | 692 | | 2023-01-05 | Electronics | 7200 | 119700 | 693 | | 2023-01-06 | Electronics | 95000 | 214700 | 694 | | 2023-01-07 | Electronics | 18000 | 232700 | 695 | | 2023-01-08 | Electronics | 40500 | 273200 | 696 | | 2023-01-09 | Electronics | 27000 | 300200 | 697 | | 2023-01-10 | Electronics | 7200 | 307400 | 698 | 699 | --- 700 | 701 | 702 | If you found this repository helpful, please give it a star! 703 | 704 | Follow me on: 705 | - [LinkedIn](https://www.linkedin.com/in/ashish-jangra/) 706 | - [GitHub](https://github.com/AshishJangra27) 707 | - [Kaggle](https://www.kaggle.com/ashishjangra27) 708 | 709 | 710 | Stay updated with my latest content and projects! 711 | -------------------------------------------------------------------------------- /Strings in Python.md: -------------------------------------------------------------------------------- 1 | # 50 Python String and Text Data Interview Questions and Answers 2 | 3 | ### 1. How do you count the number of vowels in a string in Python? 4 | **Answer:** 5 | You can count the number of vowels in a string by iterating through each character and checking if it is a vowel. 6 | ```python 7 | def count_vowels(text): 8 | vowels = "aeiouAEIOU" 9 | count = 0 10 | for char in text: 11 | if char in vowels: 12 | count += 1 13 | return count 14 | 15 | text = "Hello, World!" 16 | vowel_count = count_vowels(text) 17 | print(vowel_count) # Output: 3 18 | ``` 19 | --- 20 | 21 | ### 2. How do you remove all punctuation from a string in Python? 22 | **Answer:** 23 | You can remove all punctuation from a string by iterating through each character and checking if it is not a punctuation mark. 24 | ```python 25 | def remove_punctuation(text): 26 | punctuation = ```!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~``` 27 | return ''.join(char for char in text if char not in punctuation) 28 | 29 | text = "Hello, World!" 30 | clean_text = remove_punctuation(text) 31 | print(clean_text) # Output: Hello World 32 | ``` 33 | --- 34 | 35 | ### 3. How do you find the most frequent word in a string in Python? 36 | **Answer:** 37 | You can find the most frequent word in a string by splitting the text into words and using a dictionary to count the occurrences of each word. 38 | ```python 39 | def most_frequent_word(text): 40 | words = text.split() 41 | word_count = {} 42 | for word in words: 43 | if word in word_count: 44 | word_count[word] += 1 45 | else: 46 | word_count[word] = 1 47 | return max(word_count, key=word_count.get) 48 | 49 | text = "Hello world hello" 50 | frequent_word = most_frequent_word(text) 51 | print(frequent_word) # Output: hello 52 | ``` 53 | --- 54 | 55 | ### 4. How do you remove stopwords from a string in Python? 56 | **Answer:** 57 | You can remove stopwords from a string by filtering out common stopwords from the text. 58 | ```python 59 | def remove_stopwords(text, stopwords): 60 | words = text.split() 61 | return ' '.join(word for word in words if word not in stopwords) 62 | 63 | stopwords = {'the', 'is', 'in', 'and', 'to', 'with'} 64 | text = "This is an example text with some stopwords" 65 | clean_text = remove_stopwords(text, stopwords) 66 | print(clean_text) # Output: This an example text some stopwords 67 | ``` 68 | --- 69 | 70 | ### 5. How do you stem words in a string in Python without using any libraries? 71 | **Answer:** 72 | You can stem words by manually implementing a simple stemming algorithm. 73 | ```python 74 | def simple_stemmer(word): 75 | suffixes = ['ing', 'ly', 'ed', 'ious', 'ies', 'ive', 'es', 's', 'ment'] 76 | for suffix in suffixes: 77 | if word.endswith(suffix): 78 | return word[:-len(suffix)] 79 | return word 80 | 81 | def stem_text(text): 82 | words = text.split() 83 | return ' '.join(simple_stemmer(word) for word in words) 84 | 85 | text = "The running foxes are quickly jumping" 86 | stemmed_text = stem_text(text) 87 | print(stemmed_text) # Output: The run fox are quick jump 88 | ``` 89 | --- 90 | 91 | ### 6. How do you tokenize a string into sentences in Python? 92 | **Answer:** 93 | You can tokenize a string into sentences by splitting the text using punctuation marks like periods, exclamation marks, and question marks. 94 | ```python 95 | def tokenize_sentences(text): 96 | import re 97 | sentences = re.split(r'[.!?]', text) 98 | return [sentence.strip() for sentence in sentences if sentence] 99 | 100 | text = "Hello world! How are you doing today? It's a great day." 101 | sentences = tokenize_sentences(text) 102 | print(sentences) # Output: ['Hello world', 'How are you doing today', "It's a great day"] 103 | ``` 104 | --- 105 | 106 | ### 7. How do you tokenize a string into words in Python without using any libraries? 107 | **Answer:** 108 | You can tokenize a string into words by splitting the text using whitespace. 109 | ```python 110 | def tokenize_words(text): 111 | return text.split() 112 | 113 | text = "Hello world! How are you doing today?" 114 | words = tokenize_words(text) 115 | print(words) # Output: ['Hello', 'world!', 'How', 'are', 'you', 'doing', 'today?'] 116 | ``` 117 | --- 118 | 119 | ### 8. How do you find the longest word in a string in Python? 120 | **Answer:** 121 | You can find the longest word in a string by iterating through each word and keeping track of the longest one. 122 | ```python 123 | def longest_word(text): 124 | words = text.split() 125 | max_word = max(words, key=len) 126 | return max_word 127 | 128 | text = "This is a simple test sentence" 129 | long_word = longest_word(text) 130 | print(long_word) # Output: sentence 131 | ``` 132 | --- 133 | 134 | ### 9. How do you count the frequency of each word in a string in Python? 135 | **Answer:** 136 | You can count the frequency of each word in a string by using a dictionary to keep track of word counts. 137 | ```python 138 | def word_frequency(text): 139 | words = text.split() 140 | frequency = {} 141 | for word in words: 142 | if word in frequency: 143 | frequency[word] += 1 144 | else: 145 | frequency[word] = 1 146 | return frequency 147 | 148 | text = "Hello world hello" 149 | frequency = word_frequency(text) 150 | print(frequency) # Output: {'Hello': 1, 'world': 1, 'hello': 1} 151 | ``` 152 | --- 153 | 154 | ### 10. How do you find the position of a word in a string in Python? 155 | **Answer:** 156 | You can find the position of a word in a string by splitting the text into words and iterating through them. 157 | ```python 158 | def word_position(text, word): 159 | words = text.split() 160 | positions = [i for i, w in enumerate(words) if w == word] 161 | return positions 162 | 163 | text = "Hello world hello" 164 | word = "hello" 165 | positions = word_position(text, word) 166 | print(positions) # Output: [2] 167 | ``` 168 | --- 169 | 170 | ### 11. How do you find all unique words in a string in Python? 171 | **Answer:** 172 | You can find all unique words in a string by converting the list of words to a set. 173 | ```python 174 | def unique_words(text): 175 | words = text.split() 176 | return set(words) 177 | 178 | text = "Hello world hello" 179 | unique = unique_words(text) 180 | print(unique) # Output: {'hello', 'world', 'Hello'} 181 | ``` 182 | --- 183 | 184 | ### 12. How do you generate n-grams from a string in Python? 185 | **Answer:** 186 | You can generate n-grams by creating tuples of n consecutive words from the text. 187 | ```python 188 | def generate_ngrams(text, n): 189 | words = text.split() 190 | ngrams = [tuple(words[i:i+n]) for i in range(len(words)-n+1)] 191 | return ngrams 192 | 193 | text = "This is a simple test" 194 | bigrams = generate_ngrams(text, 2) 195 | print(bigrams) # Output: [('This', 'is'), ('is', 'a'), ('a', 'simple'), ('simple', 'test')] 196 | ``` 197 | --- 198 | 199 | ### 13. How do you find the similarity between two strings in Python? 200 | **Answer:** 201 | You can find the similarity between two strings using a simple method like calculating the ratio of matching characters. 202 | ```python 203 | def string_similarity(str1, str2): 204 | matches = sum(1 for a, b in zip(str1, str2) if a == b) 205 | return matches / max(len(str1), len(str2)) 206 | 207 | str1 = "hello" 208 | str2 = "hallo" 209 | similarity = string_similarity(str1, str2) 210 | print(similarity) # Output: 0.8 211 | ``` 212 | --- 213 | 214 | ### 14. How do you check if two strings are anagrams in Python? 215 | **Answer:** 216 | You can check if two strings are anagrams by sorting the characters in both strings and comparing them. 217 | ```python 218 | def are_anagrams(str1, str2): 219 | return sorted(str1) == sorted(str2) 220 | 221 | str1 = "listen" 222 | str2 = "silent" 223 | print(are_anagrams(str1, str2)) # Output: True 224 | ``` 225 | --- 226 | 227 | ### 15. How do you count the number of words in a string in Python? 228 | **Answer:** 229 | You can count the number of words in a string by splitting the text into words and counting the length of the resulting list. 230 | ```python 231 | def count_words(text): 232 | words = text.split() 233 | return len(words) 234 | 235 | text = "Hello world! How are you doing today?" 236 | word_count = count_words(text) 237 | print(word_count) # Output: 7 238 | ``` 239 | --- 240 | 241 | ### 16. How do you find the shortest word in a string in Python? 242 | **Answer:** 243 | You can find the shortest word in a string by iterating through each word and keeping track of the shortest one. 244 | ```python 245 | def shortest_word(text): 246 | words = text.split() 247 | min_word = min(words, key=len) 248 | return min_word 249 | 250 | text = "This is a simple test sentence" 251 | short_word = shortest_word(text) 252 | print(short_word) # Output: is 253 | ``` 254 | --- 255 | 256 | ### 17. How do you remove duplicate words from a string in Python? 257 | **Answer:** 258 | You can remove duplicate words from a string by converting the list of words to a set and then back to a list. 259 | ```python 260 | def remove_duplicates(text): 261 | words = text.split() 262 | unique_words = set(words) 263 | return ' '.join(unique_words) 264 | 265 | text = "Hello world hello" 266 | unique_text = remove_duplicates(text) 267 | print(unique_text) # Output: world hello Hello 268 | ``` 269 | --- 270 | 271 | ### 18. How do you count the number of sentences in a string in Python? 272 | **Answer:** 273 | You can count the number of sentences in a string by splitting the text using punctuation marks like periods, exclamation marks, and question marks. 274 | ```python 275 | def count_sentences(text): 276 | import re 277 | sentences = re.split(r'[.!?]', text) 278 | return len([sentence for sentence in sentences if sentence]) 279 | 280 | text = "Hello world! How are you doing today? It's a great day." 281 | sentence_count = count_sentences(text) 282 | print(sentence_count) # Output: 3 283 | ``` 284 | --- 285 | 286 | ### 19. How do you capitalize the first letter of each sentence in a string in Python? 287 | **Answer:** 288 | You can capitalize the first letter of each sentence by splitting the text into sentences, capitalizing each one, and then joining them back together. 289 | ```python 290 | def capitalize_sentences(text): 291 | import re 292 | sentences = re.split(r'([.!?] *)', text) 293 | return ''.join(sentence.capitalize() for sentence in sentences) 294 | 295 | text = "hello world! how are you doing today? it's a great day." 296 | capitalized_text = capitalize_sentences(text) 297 | print(capitalized_text) # Output: Hello world! How are you doing today? It's a great day. 298 | ``` 299 | --- 300 | 301 | ### 20. How do you extract all numbers from a string in Python? 302 | **Answer:** 303 | You can extract all numbers from a string by using a regular expression to find all digit sequences. 304 | ```python 305 | import re 306 | 307 | def extract_numbers(text): 308 | return re.findall(r'\d+', text) 309 | 310 | text = "The price is 100 dollars and 50 cents" 311 | numbers = extract_numbers(text) 312 | print(numbers) # Output: ['100', '50'] 313 | ``` 314 | --- 315 | 316 | ### 21. How do you replace multiple spaces with a single space in a string in Python? 317 | **Answer:** 318 | You can replace multiple spaces with a single space using a regular expression. 319 | ```python 320 | import re 321 | 322 | def replace_multiple_spaces(text): 323 | return re.sub(r'\s+', ' ', text) 324 | 325 | text = "This is a test" 326 | clean_text = replace_multiple_spaces(text) 327 | print(clean_text) # Output: This is a test 328 | ``` 329 | --- 330 | 331 | ### 22. How do you remove all digits from a string in Python? 332 | **Answer:** 333 | You can remove all digits from a string using a regular expression. 334 | ```python 335 | import re 336 | 337 | def remove_digits(text): 338 | return re.sub(r'\d+', '', text) 339 | 340 | text = "The price is 100 dollars" 341 | clean_text = remove_digits(text) 342 | print(clean_text) # Output: The price is dollars 343 | ``` 344 | --- 345 | 346 | ### 23. How do you check if a string is a palindrome in Python? 347 | **Answer:** 348 | You can check if a string is a palindrome by comparing it with its reverse. 349 | ```python 350 | def is_palindrome(text): 351 | return text == text[::-1] 352 | 353 | text = "madam" 354 | print(is_palindrome(text)) # Output: True 355 | ``` 356 | --- 357 | 358 | ### 24. How do you convert a string to a list of words in Python? 359 | **Answer:** 360 | You can convert a string to a list of words using the `split()` method. 361 | ```python 362 | def string_to_words(text): 363 | return text.split() 364 | 365 | text = "Hello world how are you" 366 | words = string_to_words(text) 367 | print(words) # Output: ['Hello', 'world', 'how', 'are', 'you'] 368 | ``` 369 | --- 370 | 371 | ### 25. How do you find the first non-repeating character in a string in Python? 372 | **Answer:** 373 | You can find the first non-repeating character by using a dictionary to count character occurrences and then iterating through the string. 374 | ```python 375 | def first_non_repeating_char(text): 376 | char_count = {} 377 | for char in text: 378 | if char in char_count: 379 | char_count[char] += 1 380 | else: 381 | char_count[char] = 1 382 | for char in text: 383 | if char_count[char] == 1: 384 | return char 385 | return None 386 | 387 | text = "swiss" 388 | first_unique = first_non_repeating_char(text) 389 | print(first_unique) # Output: w 390 | ``` 391 | --- 392 | 393 | ### 26. How do you reverse the words in a string in Python? 394 | **Answer:** 395 | You can reverse the words in a string by splitting the text into words, reversing the list of words, and then joining them back together. 396 | ```python 397 | def reverse_words(text): 398 | words = text.split() 399 | reversed_words = ' '.join(reversed(words)) 400 | return reversed_words 401 | 402 | text = "Hello world how are you" 403 | reversed_text = reverse_words(text) 404 | print(reversed_text) # Output: you are how world Hello 405 | ``` 406 | --- 407 | 408 | ### 27. How do you convert a string to an acronym in Python? 409 | **Answer:** 410 | You can convert a string to an acronym by taking the first letter of each word and converting it to uppercase. 411 | ```python 412 | def to_acronym(text): 413 | words = text.split() 414 | acronym = ''.join(word[0].upper() for word in words) 415 | return acronym 416 | 417 | text = "National Aeronautics and Space Administration" 418 | acronym = to_acronym(text) 419 | print(acronym) # Output: NASA 420 | ``` 421 | --- 422 | 423 | ### 28. How do you find the longest common prefix in a list of strings in Python? 424 | **Answer:** 425 | You can find the longest common prefix by comparing characters of each string in the list. 426 | ```python 427 | def longest_common_prefix(strings): 428 | if not strings: 429 | return "" 430 | prefix = strings[0] 431 | for string in strings[1:]: 432 | while string[:len(prefix)] != prefix and prefix: 433 | prefix = prefix[:len(prefix)-1] 434 | return prefix 435 | 436 | strings = ["flower", "flow", "flight"] 437 | prefix = longest_common_prefix(strings) 438 | print(prefix) # Output: fl 439 | ``` 440 | --- 441 | 442 | ### 29. How do you check if a string contains only letters in Python? 443 | **Answer:** 444 | You can check if a string contains only letters using the `isalpha()` method. 445 | ```python 446 | def contains_only_letters(text): 447 | return text.isalpha() 448 | 449 | text = "HelloWorld" 450 | print(contains_only_letters(text)) # Output: True 451 | ``` 452 | --- 453 | 454 | ### 30. How do you find the most common character in a string in Python? 455 | **Answer:** 456 | You can find the most common character by using a dictionary to count character occurrences and then finding the character with the maximum count. 457 | ```python 458 | def most_common_char(text): 459 | char_count = {} 460 | for char in text: 461 | if char in char_count: 462 | char_count[char] += 1 463 | else: 464 | char_count[char] = 1 465 | return max(char_count, key=char_count.get) 466 | 467 | text = "hello world" 468 | common_char = most_common_char(text) 469 | print(common_char) # Output: l 470 | ``` 471 | --- 472 | 473 | ### 31. How do you convert a string to snake_case in Python? 474 | **Answer:** 475 | You can convert a string to snake_case by replacing spaces with underscores and converting to lowercase. 476 | ```python 477 | def to_snake_case(text): 478 | return text.lower().replace(" ", "_") 479 | 480 | text = "Hello World" 481 | snake_case_text = to_snake_case(text) 482 | print(snake_case_text) # Output: hello_world 483 | ``` 484 | --- 485 | 486 | ### 32. How do you convert a string to camelCase in Python? 487 | **Answer:** 488 | You can convert a string to camelCase by removing spaces, capitalizing the first letter of each word except the first, and joining them together. 489 | ```python 490 | def to_camel_case(text): 491 | words = text.split() 492 | return words[0].lower() + ''.join(word.capitalize() for word in words[1:]) 493 | 494 | text = "hello world" 495 | camel_case_text = to_camel_case(text) 496 | print(camel_case_text) # Output: helloWorld 497 | ``` 498 | --- 499 | 500 | ### 33. How do you check if a string contains any digits in Python? 501 | **Answer:** 502 | You can check if a string contains any digits using a loop and the `isdigit()` method. 503 | ```python 504 | def contains_digits(text): 505 | return any(char.isdigit() for char in text) 506 | 507 | text = "Hello123" 508 | print(contains_digits(text)) # Output: True 509 | ``` 510 | --- 511 | 512 | ### 34. How do you remove leading and trailing whitespace from a string in Python? 513 | **Answer:** 514 | You can remove leading and trailing whitespace using the `strip()` method. 515 | ```python 516 | def remove_whitespace(text): 517 | return text.strip() 518 | 519 | text = " Hello World " 520 | clean_text = remove_whitespace(text) 521 | print(clean_text) # Output: Hello World 522 | ``` 523 | --- 524 | 525 | ### 35. How do you repeat a string n times in Python? 526 | **Answer:** 527 | You can repeat a string n times using the multiplication operator. 528 | ```python 529 | def repeat_string(text, n): 530 | return text * n 531 | 532 | text = "Hello" 533 | repeated_text = repeat_string(text, 3) 534 | print(repeated_text) # Output: HelloHelloHello 535 | ``` 536 | --- 537 | 538 | ### 36. How do you check if a string is a valid email address in Python? 539 | **Answer:** 540 | You can check if a string is a valid email address using a regular expression. 541 | ```python 542 | import re 543 | 544 | def is_valid_email(text): 545 | pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' 546 | return re.match(pattern, text) is not None 547 | 548 | email = "example@example.com" 549 | print(is_valid_email(email)) # Output: True 550 | ``` 551 | --- 552 | 553 | ### 37. How do you find the difference between two strings in Python? 554 | **Answer:** 555 | You can find the difference between two strings by finding characters that are in one string but not the other. 556 | ```python 557 | def string_difference(str1, str2): 558 | set1 = set(str1) 559 | set2 = set(str2) 560 | return ''.join(set1.symmetric_difference(set2)) 561 | 562 | str1 = "abcdef" 563 | str2 = "abcxyz" 564 | difference = string_difference(str1, str2) 565 | print(difference) # Output: defxyz 566 | ``` 567 | --- 568 | 569 | ### 38. How do you find the longest palindromic substring in Python? 570 | **Answer:** 571 | You can find the longest palindromic substring by expanding around each character and checking for palindromes. 572 | ```python 573 | def longest_palindromic_substring(text): 574 | def expand_around_center(left, right): 575 | while left >= 0 and right < len(text) and text[left] == text[right]: 576 | left -= 1 577 | right += 1 578 | return text[left+1:right] 579 | 580 | longest = "" 581 | for i in range(len(text)): 582 | odd_palindrome = expand_around_center(i, i) 583 | even_palindrome = expand_around_center(i, i+1) 584 | longest = max(longest, odd_palindrome, even_palindrome, key=len) 585 | return longest 586 | 587 | text = "babad" 588 | longest_palindrome = longest_palindromic_substring(text) 589 | print(longest_palindrome) # Output: bab or aba 590 | ``` 591 | --- 592 | 593 | ### 39. How do you count the number of unique characters in a string in Python? 594 | **Answer:** 595 | You can count the number of unique characters in a string by converting it to a set. 596 | ```python 597 | def count_unique_characters(text): 598 | return len(set(text)) 599 | 600 | text = "hello world" 601 | unique_count = count_unique_characters(text) 602 | print(unique_count) # Output: 8 603 | ``` 604 | --- 605 | 606 | ### 40. How do you convert a string to an integer in Python? 607 | **Answer:** 608 | You can convert a string to an integer using the `int()` function. 609 | ```python 610 | def string_to_integer(text): 611 | return int(text) 612 | 613 | text = "123" 614 | integer = string_to_integer(text) 615 | print(integer) # Output: 123 616 | ``` 617 | --- 618 | 619 | ### 41. How do you find all the permutations of a string in Python? 620 | **Answer:** 621 | You can find all the permutations of a string using a recursive function. 622 | ```python 623 | def permutations(text): 624 | if len(text) == 1: 625 | return [text] 626 | perm_list = [] 627 | for i in range(len(text)): 628 | for perm in permutations(text[:i] + text[i+1:]): 629 | perm_list.append(text[i] + perm) 630 | return perm_list 631 | 632 | text = "abc" 633 | perm_list = permutations(text) 634 | print(perm_list) # Output: ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] 635 | ``` 636 | --- 637 | 638 | ### 42. How do you replace all occurrences of a substring in a string in Python? 639 | **Answer:** 640 | You can replace all occurrences of a substring using the `replace()` method. 641 | ```python 642 | def replace_substring(text, old, new): 643 | return text.replace(old, new) 644 | 645 | text = "Hello world" 646 | new_text = replace_substring(text, "world", "Python") 647 | print(new_text) # Output: Hello Python 648 | ``` 649 | --- 650 | 651 | ### 43. How do you split a string into a list of characters in Python? 652 | **Answer:** 653 | You can split a string into a list of characters using the `list()` function. 654 | ```python 655 | def string_to_characters(text): 656 | return list(text) 657 | 658 | text = "hello" 659 | char_list = string_to_characters(text) 660 | print(char_list) # Output: ['h', 'e', 'l', 'l', 'o'] 661 | ``` 662 | --- 663 | 664 | ### 44. How do you find the second most frequent character in a string in Python? 665 | **Answer:** 666 | You can find the second most frequent character by using a dictionary to count character occurrences and then finding the character with the second maximum count. 667 | ```python 668 | def second_most_frequent_char(text): 669 | char_count = {} 670 | for char in text: 671 | if char in char_count: 672 | char_count[char] += 1 673 | else: 674 | char_count[char] = 1 675 | sorted_chars = sorted(char_count, key=char_count.get, reverse=True) 676 | return sorted_chars[1] if len(sorted_chars) > 1 else None 677 | 678 | text = "hello world" 679 | second_common_char = second_most_frequent_char(text) 680 | print(second_common_char) # Output: o 681 | ``` 682 | --- 683 | 684 | ### 45. How do you find the shortest palindromic substring in Python? 685 | **Answer:** 686 | You can find the shortest palindromic substring by expanding around each character and checking for palindromes. 687 | ```python 688 | def shortest_palindromic_substring(text): 689 | def expand_around_center(left, right): 690 | while left >= 0 and right < len(text) and text[left] == text[right]: 691 | left -= 1 692 | right += 1 693 | return text[left+1:right] 694 | 695 | shortest = text 696 | for i in range(len(text)): 697 | odd_palindrome = expand_around_center(i, i) 698 | even_palindrome = expand_around_center(i, i+1) 699 | for palindrome in [odd_palindrome, even_palindrome]: 700 | if 1 < len(palindrome) < len(shortest): 701 | shortest = palindrome 702 | return shortest 703 | 704 | text = "babad" 705 | shortest_palindrome = shortest_palindromic_substring(text) 706 | print(shortest_palindrome) # Output: bab or aba 707 | ``` 708 | --- 709 | 710 | ### 46. How do you convert a string to lowercase in Python? 711 | **Answer:** 712 | You can convert a string to lowercase using the `lower()` method. 713 | ```python 714 | def to_lowercase(text): 715 | return text.lower() 716 | 717 | text = "HELLO WORLD" 718 | lowercase_text = to_lowercase(text) 719 | print(lowercase_text) # Output: hello world 720 | ``` 721 | --- 722 | 723 | ### 47. How do you convert a string to uppercase in Python? 724 | **Answer:** 725 | You can convert a string to uppercase using the `upper()` method. 726 | ```python 727 | def to_uppercase(text): 728 | return text.upper() 729 | 730 | text = "hello world" 731 | uppercase_text = to_uppercase(text) 732 | print(uppercase_text) # Output: HELLO WORLD 733 | ``` 734 | --- 735 | 736 | ### 48. How do you count the number of lowercase letters in a string in Python? 737 | **Answer:** 738 | You can count the number of lowercase letters by iterating through each character and checking if it is a lowercase letter. 739 | ```python 740 | def count_lowercase(text): 741 | return sum(1 for char in text if char.islower()) 742 | 743 | text = "Hello World" 744 | lowercase_count = count_lowercase(text) 745 | print(lowercase_count) # Output: 8 746 | ``` 747 | --- 748 | 749 | ### 49. How do you count the number of uppercase letters in a string in Python? 750 | **Answer:** 751 | You can count the number of uppercase letters by iterating through each character and checking if it is an uppercase letter. 752 | ```python 753 | def count_uppercase(text): 754 | return sum(1 for char in text if char.isupper()) 755 | 756 | text = "Hello World" 757 | uppercase_count = count_uppercase(text) 758 | print(uppercase_count) # Output: 2 759 | ``` 760 | --- 761 | 762 | ### 50. How do you check if a string contains only alphanumeric characters in Python? 763 | **Answer:** 764 | You can check if a string contains only alphanumeric characters using the `isalnum()` method. 765 | ```python 766 | def is_alphanumeric(text): 767 | return text.isalnum() 768 | 769 | text = "Hello123" 770 | print(is_alphanumeric(text)) # Output: True 771 | ``` 772 | --- 773 | 774 | If you found this repository helpful, please give it a star! 775 | 776 | Follow me on: 777 | - [LinkedIn](https://www.linkedin.com/in/ashish-jangra/) 778 | - [GitHub](https://github.com/AshishJangra27) 779 | - [Kaggle](https://www.kaggle.com/ashishjangra27) 780 | 781 | Stay updated with my latest content and projects! 782 | --------------------------------------------------------------------------------