DEV Community

Arokya Naresh
Arokya Naresh

Posted on

Python Operators and Conditional 15.07.2024

Operators
Operator and Conditions

Add +
Sub -
Mul *
Div // floor division
Mod % to get remainder
Exponent **

eg:
a=int(input('Enter value for a '))
b=int(input('Enter value for b '))

add=a+b
print(add)

sub=a-b
print(sub)

mul=a*b
print(mul)

div=a//b
print(div)

mod=a%b
print(mod)

exp=2 ** 3 #2 power 3
print(exp)

Condition
if
else
elif

eg1:Calculator prgm

num1=float(input('Enter value for num1 '))
num2=float(input('Enter value for num2 '))
choice=input('Operation to be done:add/sub/mul/div/mod ')

if choice=='add':
add=num1+num2
print(add)

elif choice=='sub':
sub=num1-num2
print(sub)

elif choice=='mul':
mul=num1*num2
print(mul)

elif choice=='div':
div=num1//num2
print(div)

elif choice=='mod':
mod=num1%num2
print(mod)

elif choice=='exp':
exp=num1**num2
print(exp)

else:
print('option not available')

eg2:Pass or Fail
Using OR operator

mark=int(input('Enter your Mark: '))

if mark >= 91 or mark==100:
print('Grade A')
elif mark > 81 or mark==90:
print('Grade B')
elif mark >= 71 or mark==80:
print('Grade C')
elif mark > 61 or mark==70:
print('Grade D')
elif mark >= 46 or mark==60:
print('Grade E')
elif mark <= 45:
print('Fail')
else:
print('Mark value is invalid')

Using AND operator
mark=int(input('Enter your Mark: '))

if mark >= 91 mark <=100:
print('Grade A')
elif mark > 81 mark <=90:
print('Grade B')
elif mark >= 71 mark <=80:
print('Grade C')
elif mark > 61 mark<=70:
print('Grade D')
elif mark >= 46 mark<=60:
print('Grade E')
elif mark <= 45:
print('Fail')
else:
print('Mark value is invalid')

Conditionals:

For Condition Check in Python,we can use 0 or 1
0 means false -1/1 means True

eg:
Passmark=int(input('Enter your mark: '))

if Passmark >45 :
print('Pass')
else:
print('Fail')

Global Variable-accesed with anywhere in the program
Local Variable-can be accessed only with function of the program
eg:Global Variable

Tiffan='IdlySambar' #global variable

def Morningmenu1(): #def function
Tiffan='Kitchadi' #local variable
print(Tiffan)

def Morningmenu2():
print(Tiffan)

Morningmenu1() #methodcalling
Morningmenu2()

Top comments (0)