Course Content
Testing Tools
Testing Tools
0/1
Sample Python Programs
Sample Python Programs
0/1
Selenium Python First Script
Selenium Python First Script
0/2
HTML Form elements
HTML Form elements
0/1
Handle HTML Frames
Handle HTML Frames
0/2
Handling Dropdown Lists
Handling Dropdown Lists
0/2
Selenium Action class
Selenium Action class
0/1
Python Database Programming
Python Database Programming
0/1
Selenium Python for Beginners [ 2024 ]
About Lesson

Sample Python Program

In this lesson, let’s understand a sample Python program. A
Python Program to calculate the area of a rectangle.

# Sample Python Program
# Python Tutorials - www.TestingDocs.com

# Python Function to calculate area
def calculate_area(length, width):
return length * width

# Main function
def main():
# Get input from the user
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))

# Calculate the area
area = calculate_area(length, width)

# Display the result
print(f"The area of the rectangle is: {area}")

# Call the main function
if __name__ == "__main__":
main()

Explanation

Function Definition

calculate_area(length, width):

This function takes the length and width of the rectangle as arguments and returns the area.

Main Function
The main() function handles user input and output. The input() is used to obtain the user’s length and width.
The inputs are converted to float to handle decimal values.
The calculate_area() function is called with the user-provided length and width to compute the area.
The result is printed using an f-string for formatted output.

Program Execution

The if __name__ == “__main__”: block ensures that main() is called only when the script is executed directly, not when it is imported as a module in another script.

You can run this program in any Python IDE environment to practice it




Join the conversation