#PYTHON(DAY32)
Example programs
1) Python Program to Print Hello world!
print('Hello, world!')
And the output is:
Hello, world!
2) Python Program to Add Two Numbers
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
And the output is:
The sum of 1.5 and 6.3 is 7.8
3) Python Program to Find the Square Root
num = 8
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
And the output is:
The square root of 8.000 is 2.828
4) Python Program to Calculate the Area of a Triangle
a = 5
b = 6
c = 7
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
And the output is:
The area of the triangle is 14.70
No comments:
Post a Comment