Video 6 : Relational Operators and If construct
Operator | Name | Operation | Remark |
---|---|---|---|
< | Less than | a < b |
Returns True if a is less than b. Else returns False . |
<= | Less than or Equal to | a <= b |
Returns True if a is less than or equal to b. Else returns False . |
> | Greater than | a > b |
Returns True if a is greater than b. Else returns False . |
>= | Greater than or equal to | a >= b |
Returns True if a is greater than or equal to b. Else returns False . |
== | Equal to | a == b |
Returns True if a is equal to b. Else returns False . |
!= | Not Equal to | a != b |
Returns True if a is not equal to b. Else returns False . |
Operator | Name | Operation | Remark |
---|---|---|---|
or | Boolean Or | con1 or con2 |
Returns True if either con1 or con2 is True . Else returns False . |
and | Boolean And | con1 and con2 |
Returns True if both con1 and con2 are True . Else returns False . |
not | Boolean Not | not con1 |
Returns True if con1 is False |
if (condition1):
# statements to execute if the condition1 is true
elif (condition2):
# statements to execute if the condition2 is true
elif (condition3):
# statements to execute if the condition3 is true
...elif (conditionN):
# statements to execute if the conditionN is true
else:
# statements to execute if all the N conditions fail
The
Check if the inputs actually make a valid triangle.
'''
Program to calculate the area of a triangle using Heron's formulae
'''
# Get inputs
= float(input("Enter the side 'a' of the triangle:"))
a = float(input("Enter the side 'b' of the triangle:"))
b = float(input("Enter the side 'c' of the triangle:"))
c
if (a <= 0.0 or b <= 0.0 or c <= 0.0):
print("Sides of a triangle can't be zero or negative!")
elif ( c >= a+b or b >= a+c or a >= b+c):
print("The values violate triangle inequality!")
else:
print("All conditions are valid!")
# Calculate the semi-perimeter
= (a + b + c)*0.5
s
# Find the Area
= (s*(s-a)*(s-b)*(s-c))**0.5
Area
# Print the result
print("The area of the triangle with sides a = {}, b = {}, and c = {} is {}".format(a, b, c, Area))