Python

champak
₹2,000.00 ₹300.00
The Python Programming Language at Champak's World
  • 22 students
  • 41 lessons
  • 0 quizzes
  • 10 week duration

Python

If then else in Python

if then else in Python has the following syntax in python.

PS. I am using — for indent.

if condition:
—s1

else:

—s2

 

There are no curly braces in Python. Statement blocks are created by indent.

#Finding maximum of two numbers
x=1
y=2
if x>=y:
—max=x
else:
—max=y
print(max)

ans 2

Finding the maximum of 3 numbers

This is an if else ladder. The last optio will be selected by default. Else if statements are called elif

#Finding maximum of three numbers
x=11
y=2
z=59
if x>=y and x>=z:
—max=x
elif y>=z:
—max=y
else:
—max=z
print(max)

Ans 59

Nested if else. Finding the max of three numbers.

#Finding maximum of three numbers
x=1109
y=278
z=59
if x>=y:
—if x>=z:
——max=x
—else:
——max=z
else:
—if y>=z:
——max=y
—else:
——max=z
print(max)

ans 1109

Finding max of three numbers using or

#Finding maximum of three numbers
x=1109
y=2780
z=5909
if x<y or x<z:
—if y>=z:
——max=y
—else:
——max=z
else:
—max=x
print(max)

ans 5909

Here is another method of solving the same problem

#Finding maximum of three numbers
x=1109
y=2780
z=5909
max=x
if max>y:
—max=y
if max>z:
—max=z
print(max)

 

Problems.

  1. Enter the three sides of a triangle and check if it is equilateral, isosceles or scalene
  2. Check whether a number is even or odd. The remainder operator is %
  3. Check if  year is a leap year. A leap year is divisible by 400 or it is divisible by 4 and not divisible by 100
  4. Enter selling price and cost price. Check whether we have profit or loss. Find the profit or loss. Also find the profit or loss percent.

end

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *