Python: Print "30+20=50"
Sum Expression Printer
Given variables x=30 and y=20, write a Python program to print "30+20=50".
Pictorial Presentation:
Sample Solution-1:
Python Code:
# Define two variables, x and y, and assign values 30 and 20, respectively.
x = 30
y = 20
# Print the sum of x and y using string formatting.
print("\n%d+%d=%d" % (x, y, x+y))
Sample Output:
30+20=50
Sample Solution-2:
Python Code:
# Define two variables, x and y, and assign values 30 and 20, respectively.
x = 30
y = 20
# Print the sum of x and y using the format method to insert the variables.
print("{0}+{1}={2}".format(x, y, x + y))
Sample Output:
30+20=50
Sample Solution-3:
Python Code:
# Define two variables, x and y, and assign values 30 and 20, respectively.
x = 30
y = 20
# Print the sum of x and y using the format method to insert the variables.
print("{}+{}={}".format(x, y, x + y))
Sample Output:
30+20=50
Python Code Editor:
Previous: Write a Python program to get the size of a file.
Next: Write a Python program to perform an action if a condition is true.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics