Saturday, 7 December 2024

Day 15 : Python Program to find all perfect squares in a given range

 


def perfect_squares(start, end):

    squares = []

    for num in range(start, end + 1):

        if (num ** 0.5).is_integer():  

            squares.append(num)

    return squares

start = int(input("Enter the start of the range: "))

end = int(input("Enter the end of the range: "))

print(perfect_squares(start, end))

Code Explanation:

1. Defining the Function

def perfect_squares(start, end):

def: This keyword is used to define a new function in Python.

perfect_squares: The name of the function, which suggests its purpose (to find perfect squares).

start, end: These are the input arguments (parameters). start is the lower bound of the range, and end is the upper bound.

2. Initializing an Empty List

    squares = []

squares: This is an empty list that will store all the perfect squares found within the given range.

3. Iterating Through the Range

    for num in range(start, end + 1):

for num in range(start, end + 1):

A for loop that iterates over each number from start to end (inclusive).

range(start, end + 1): Generates a sequence of numbers starting at start and ending at end.

4. Checking for Perfect Squares

        if (num ** 0.5).is_integer():

num ** 0.5: Calculates the square root of the current number (num).

.is_integer(): Checks if the square root is an integer.

If the square root is a whole number, it means num is a perfect square.


5. Adding Perfect Squares to the List


            squares.append(num)

squares.append(num): Adds the current number (num) to the squares list if it passes the perfect square check.

6. Returning the List

    return squares

return squares: After the loop finishes, the function returns the list of all perfect squares found within the range.

7. Taking User Input for the Range

start = int(input("Enter the start of the range: "))

end = int(input("Enter the end of the range: "))

input(): Displays a prompt to the user and reads their input as a string.

int(): Converts the user input from a string to an integer.

The user is asked to provide the starting (start) and ending (end) numbers for the range.

8. Calling the Function and Printing the Result

print(perfect_squares(start, end))

perfect_squares(start, end): Calls the function with the user-provided range.

print(): Displays the resulting list of perfect squares.


#source code --> clcoding.com

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (28) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (223) Cybersecurity (24) data management (11) Data Science (127) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (4) PHP (20) Projects (29) Python (923) Python Coding Challenge (318) Python Quiz (4) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses