w3resource

Python: Python program to solve (x + y) * (x + y)

Python Basic: Exercise-38 with Solution

Write a Python program to solve (x + y) * (x + y).

Test Data : x = 4, y = 3
Expected Output : (4 + 3) ^ 2) = 49

Sample Solution:

Python Code:

# Define variables 'x' and 'y' and assign values to them.
x, y = 4, 3
# Calculate the result by squaring the sum of 'x' and 'y'.
result = x * x + 2 * x * y + y * y
# Print the result with a formatted message.
print("({} + {}) ^ 2) = {}".format(x, y, result))

Sample Output:

(4 + 3) ^ 2) = 49 

Explanation:

The said Python code assigns the value 4 to the variable x and 3 to the variable y. Then it defines the variable "result" as the result of the mathematical expression (x^2 + 2xy + y^2).

The print statement then uses string formatting to output the final result of the expression and the values of x and y in the string "(x + y) ^ 2 = result".

The final output will be (4 + 3) ^ 2 = 49.

Flowchart:

Flowchart: Python program to solve (x + y) * (x + y).

Python Code Editor:

 

Previous: Write a Python program to display your details like name, age, address in three different lines.
Next: Write a Python program to compute the future value of a specified principal amount, rate of interest, and a number of years.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/python-basic-exercise-38.php