top of page
  • Writer's picturePIYUSH LIMKAR

Python if else



 

import pandas as pd import numpy as np


 

1. if Statement


x = int(input('Please enter the number: ')) display(x) if x == 5: display('Hooo')

 

2. Single Line if Statement


x = int(input('Please enter the number: ')) display(x) if x == 5 : display('Hooo')

 

3. if elif Statement


a.

x = int(input('Please enter the number: ')) display(x) if x == 5: display('Hooo') elif x > 5: display('Greater')

 

b.

Below syntax will throw an Error

display('Hooo') if x == 5 elif x > 5 display('Greater') else display('lol')

 

4. if else Statement


x = int(input('Please enter the number: ')) display(x) if x == np.random.randint(1,5): display('Hooo') else: display('Lol')

 

5. Single Line if else Statement


x = int(input('Please enter the number: ')) display(x)


display('Hooo') if x == np.random.randint(1,5) else display('Lol')

 

6. if elif else Statement / if elif elif .... else


a.

x = int(input('Please enter the number: ')) display(x) if x == 5: display('Hooo') elif x > 5: display('Greater') else: display('Lesser')

 

b.

x = int(input('Please enter the number: ')) if (x == 10): print("x is 10") elif (x == 15): print("x is 15") elif (x == 20): print("x is 20") else: print("x is not present")

 

7. Smart if elif else / if elif elif .... else


a.

x = int(input('Please enter the number: ')) if (x == 10) : print("x is 10") elif (x == 15) : print("x is 15") else : print("x is not present")

 

b.

x = int(input('Please enter the number: ')) if (x == 10) : print("x is 10") elif (x == 15) : print("x is 15") elif (x == 20) : print("x is 20") else : print("x is not present")

 

8. Single Line if else if else


a.

x = int(input('Please enter 1st number: ')) y = int(input('Please enter 2nd number: ')) display(x, y) print("Greater") if x > y else print("=") if x == y else print("Lesser")

 

b.

x = int(input('Please enter 1st number: ')) y = int(input('Please enter 2nd number: ')) z = int(input('Please enter 3nd number: ')) display(x, y, z) 'x is {}'.format(x) if x == 10 else 'y is {}'.format(y) if y == 20 else 'z is {}'.format(z) if z == 30 else 'Stop'

 

9. Smart if else using {}


a.

x = int(input('Please enter the number: '))


{x > 190 : "First condition satisfied!", x == 87 : "Second condition satisfied!" }.get(True, "Third condition satisfied")

 

b.

x = int(input('Please enter the number: '))


result = {x > 190 : "First condition satisfied!", x == 87 : "Second condition satisfied!" }.get(True, "Third condition satisfied") print(result)

 

c.

x = int(input('Please enter the number: '))


{x > 190 : print("First condition satisfied!"), x == 87 : print("Second condition satisfied!") }.get(True, print("Third condition satisfied!"))

 

d.

x = int(input('Please enter the number: ')) result = {x < 35 : 'Fail', x == 35 : 'Pass At Edge', x > 35 and x <= 50 : 'D', x > 50 and x <= 60 : 'C', x > 60 and x <= 70 : 'B', x > 70 and x <= 80 : 'A', }.get(True, 'A+') print(result)

 

10. and / or


a.

x = int(input('Please enter the number: ')) y = int(input('Please enter the number: ')) z = int(input('Please enter the number: ')) display(x, y, z) if x > y and z > x: display("Both conditions are True") else: display('Either one of them or Both conditions are False')

 

b.

x = int(input('Please enter the number: ')) y = int(input('Please enter the number: ')) z = int(input('Please enter the number: ')) display(x, y, z) if x > y or x > z: display("At least one of the conditions is True") else: display("Both conditions are False")

 

11. Nested if else


a.

x = int(input('Please enter the number: ')) display(x) if x > 10: display("Above 10!") if x > 20: display("and also above 20!") else: display("but not above 20!")

 

b.

x = int(input("Enter x number: ")) display(x) if x >= 0: if x == 0: print("Zero") else: print("Positive number") else: print("Negative number")

 

12. Lambda Function


a.

Lambda with if but without else in Python Throws an Error


x = int(input('Please enter the number: ')) square = lambdx x : x*x if x > 0 display(square(x))

SyntaxError: invalid syntax

 

b.


Lambda with if but with else


x = int(input('Please enter the number: ')) square = lambda x : x*x if x > 0 else None display('{0} is square of {1}'.format(square(x),x))

 

x = int(input('Please enter the number: ')) display(x) output = lambda x : 'Fail' if x < 35 else 'pass' display(output(x))

 

x = int(input('Please enter the number: ')) mod = lambda x : x if(x >= 0) else -x display('{0} is mod of {1}'.format(mod(x),x))

 

x = int(input('Please enter the number: ')) temp = lambda x : 'A' if x < 100 and x >= 80 else 'B' if x < 80 and x >= 70 else 'C' if x < 70 and x >= 60 else 'D' if x < 60 and x >= 50 else 'E' if x < 50 and x >= 35 else 'F' if x < 35 else 'Absent' print(temp(x))

 

max = lambda x, y : f'{x} is greater' if(x > y) else f'{y} is greater' display(max(45,7812)) display(max(121334,12))

 

result = lambda x : f"{x} is even" if x % 2 == 0 else f"{x} is odd" display(result(2)) display(result(3))

 

result = lambda x,y : f"{x} is smaller than {y}" if x < y else f"{x} is greater than {y}" if x > y else f"{x} is equal to {y}" display(result(12,123)) display(result(321,21)) display(result(1,1))

 

x = int(input('Please enter the number: ')) display(x) output = lambda x : 'Fail' if x < 35 else 'D' if x < 50 and x >= 35 else 'C' if x < 70 and x >= 51 else 'B' if x < 80 and x >= 71 else 'A' display(output(x))

 

13. Apply if else on Column of Data Frame


a.

df = pd.DataFrame({'Name': ['John', 'Jack', 'Shri', 'Krishna', 'Smith', 'Tessa'], 'Maths': [5, 3, 9, 10, 6, 3]}) display(df) def condition(x): if x > 8 : return "No need" elif x >= 5 and x <= 7 : return "Hold decision" else: return 'Need' df['Maths_Spl Class'] = df['Maths'].apply(condition) display(df)

 

b.

df = pd.DataFrame({'Name': ['John', 'Jack', 'Shri', 'Krishna', 'Smith', 'Tessa'], 'Maths': [5, 3, 9, 10, 6, 3]}) display(df) df['Maths_Spl Class'] = df['Maths'].apply(lambda x : 'No Need' if x > 8 else 'Hold Decision' if x >= 5 and x <= 7 else 'Need') display(df)

 

 


Recent Posts

See All
bottom of page