Python Program to Find Prime Numbers in a Range

Aim: Python program to print all prime numbers in a given interval (use break)

Program:

# take input from the user
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: ")) 
for num in range(lower,upper + 1):
   # prime numbers are greater than 1
   if num > 1:
       for i in range(2,num):
           if (num % i) == 0:
               break
       else:
           print(num)
Output:

Enter lower range: 1
Enter upper range: 50
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

Sample Viva Questions and Answers

  1. What is the purpose of the program?
    The program identifies and prints all prime numbers within a specified range provided by the user.

  2. What inputs does the prime number program require?
    The program requires two integer inputs: the lower and upper range for the search of prime numbers.

  3. What is a prime number?
    A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

  4. How does the program determine if a number is prime?
    The program checks if the number is divisible by any integer from 2 up to the number itself (exclusive). If it is not divisible by any of these, it is considered prime.

  5. What will the output be if the user inputs a range of 1 to 50?
    The output will be all prime numbers between 1 and 50, which are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, and 47.

  6. What happens if the user inputs a lower range of 0?
    The program will still function correctly, but it will only print prime numbers starting from 2, as prime numbers are defined to be greater than 1.

  7. How does the program handle the number 1?
    The program does not print 1 because it is not considered a prime number.

  8. What is the time complexity of the prime number checking logic?
    The time complexity is O(n^2) in the worst case, as for each number in the range, it checks divisibility against all smaller numbers.

  9. What improvements could be made to the program?
    The program could be optimized to check for divisibility only up to the square root of the number, reducing the number of checks needed.

  10. Can the program handle negative inputs?
    The program does not explicitly handle negative inputs, but since it starts checking from the lower range, negative numbers will not produce any output.

๐Ÿ“Œ Python Programming Lab – Complete Guide

Welcome to the Python Programming Lab! This comprehensive lab series is designed to provide a solid foundation in Python programming through practical exercises and hands-on learning. From basic calculations to advanced concepts like file handling, modules, and GUI programming, this lab covers it all. Letโ€™s dive into what youโ€™ll be learning throughout this lab course.


๐Ÿ” Week 1: Introduction to Python Basics

In the first week, you will explore fundamental Python concepts aimed at building a strong foundation for your programming journey.

  • Getting Started with Python: Visit the Python Official Website to explore documentation and use the help() function in the interpreter.
  • Python as a Calculator: Learn how to perform basic arithmetic operations like addition, subtraction, multiplication, and division.
  • Calculating Compound Interest: Write a program to calculate compound interest using the formula with given principal, rate, and time.
  • Distance Calculation: Compute the distance between two points using the distance formula.
  • Reading User Details: Write a program to collect and print user details like name, address, email, and phone number.

๐Ÿ” Week 2: Python Loops and Conditional Statements

This week focuses on using loops and conditional statements effectively.

  • Pattern Generation: Print a triangle pattern using nested loops.
  • Character Identification: Write a program to identify whether the input is a digit, lowercase, uppercase, or special character.
  • Fibonacci Sequence Generation: Use a while loop to generate the Fibonacci series.
  • Prime Numbers Identification: Find all prime numbers within a specified range using the break statement.

๐Ÿ” Week 3: Data Structures and Functions

Dive into data structures and creating functions to enhance your programming skills.

  • Converting Lists and Tuples to Arrays: Understand how to convert different data structures.
  • Finding Common Values: Compare two arrays to find common values.
  • Calculating GCD: Create a function to calculate the greatest common divisor (GCD) of two numbers.
  • Palindrome Checker: Write a function that checks if a given string is a palindrome.

๐Ÿ” Week 4: Advanced Functions and String Manipulation

This week emphasizes sorting, handling duplicate elements, and manipulating strings effectively.

  • Checking Sorted Lists: Write a function to check if a list is sorted.
  • Handling Duplicates: Create functions to detect and remove duplicates from lists.
  • Dictionary Inversion: Write a program to swap keys and values in a dictionary.
  • String Manipulation: Add commas between characters, remove words from strings, and convert sentences to title case without using built-in functions.
  • Binary String Generation: Use recursion to generate all binary strings of a specified length.

๐Ÿ” Week 5: Working with Matrices and Modules

Learn to work with matrices and create custom modules for various applications.

  • Matrix Operations: Define, print, add, and multiply square matrices using Python.
  • Creating Modules: Build modules using geometrical shapes and their operations.
  • Exception Handling: Implement exception handling for robust error management.

๐Ÿ” Week 6: Object-Oriented Programming and Validation

This week focuses on using classes, inheritance, and validation techniques.

  • Drawing Shapes with Classes:
    • Create classes for rectangles, points, and circles, and draw them on a canvas.
    • Add colors and attributes to these shapes to enhance visualization.
  • Method Resolution Order (MRO): Demonstrate MRO in multiple inheritance scenarios.
  • Data Validation: Write programs to validate phone numbers and email addresses.

๐Ÿ” Week 7: File Handling and Text Processing

Master file handling techniques and analyzing text data.

  • File Merging: Combine the contents of two files into a new file.
  • Word Search: Create a function to find specific words in a file.
  • Word Frequency Analysis: Identify the most frequent words in a text file.
  • Text Statistics: Count words, vowels, blank spaces, lowercase, and uppercase letters in a file.

๐Ÿ” Week 8: Python Libraries and GUI Programming

Explore advanced Python libraries and build simple graphical user interfaces.

  • NumPy, Plotly, and Scipy: Learn how to install and use these powerful libraries for data analysis and visualization.
  • Digital Logic Gates: Implement logic gate operations such as AND, OR, NOT, and EX-OR.
  • Adder Circuits: Create programs for Half Adders, Full Adders, and Parallel Adders.
  • GUI Programming: Build a simple window wizard with text labels, input fields, and buttons using Python’s GUI tools.

๐ŸŒŸ Conclusion:

The Python Programming Lab R23 provides you with a strong foundation in Python programming through a wide range of exercises, from basic concepts to advanced topics. By the end of this lab, you will have gained valuable skills in data processing, file handling, OOP, modules, and GUI development. Start coding and enhance your Python skills! ๐Ÿš€


;