Search This Blog

Conditional Statement in Python:-


It is used to solve condition-based problems using it to provide a separate block of if statement, else statement, and elif statement.

Type of Conditional Statement:-

1) Simple if:-


It will be executed when the condition will be true.

if(condition):       

       statements;



Q) WAP to increase the salary of employees from 500 if entered salary will be less then 10000 otherwise the same salaries will be displayed.

x = int(input("enter salary"))
if x<10000:
    x=x+500
print(x)  





Q) WAP to display the mark of the student which will be entered by the user's, if the mark is eligible for grace then display mark including grace mark otherwise actual mark will display.

x = int(input("enter mark"))
if x>=28 and x<33:
    g=33-x
    print('grace mark is ',g)
    x=x+g

print(x)  

2) If...Else:-

'else' is the dependent statement of if statement.
It will be implemented when the condition will be true and false both.



if(condition):       

  Statements

else:   

  Statements


Q) WAP to check that entered number is a one-digit number or above one-digit number, the number can be positive or negative?


num = int(input("enter number"))

if num>=-9 and num<=9:
    print("one digit number")
else:
    print("above one digit")




3)  Nested If--else:-

 It is used to implement multiple condition-based problems which can not be solved by simple if-else .it will use more than one if-else statement using nested sequence.
means it contains collections of outer if-else and inner if-else.

if the program has multiple conditions and we not use the logical operator(and) then we prefer nested if-else.

we should manage the nested sequence should be managed by Indent to create the outer block and inner block.

if(condition):  

  if(condition):        

      statement    

  else:        

      statement
else:  

    if(condition):           

      statement   

    else:          

      statement

Q)WAP to check that entered number is one digit positive number, negative or two-digit positive number or negative, the number can be positive and negative?

num = int(input("enter number"))

if num>=-9 and num<=9:
    if num>0:
        print("one digit positive number")
    else:
        print("one digit negative number")
else:
    if num>=10 and num<100:
        print("two digit positive number")
    else:
        if num>=-99 and num<=-10:
            print("two digit negative number")
        else:
            print("other number")



 Example of Nested If--else Statement:-

a = int(input("enter first number"))
b = int(input("enter second number"))
c = int(input("enter third number"))

if a>b:
    if a>c:
        print("a is greatest")
    else:
        print("c is greatest")
else:
    if b>c:
        print("b is greatest")
    else:
        print("c is greatest")


   

 Q WAP to check the greatest number?

a = int(input("enter first number"))
b = int(input("enter second number"))
c = int(input("enter third number"))
if a>b:
    if a>c:
        print("a is greatest")
    else:
        print("c is greatest")
else:
    if b>c:
        print("b is greatest")
    else:
        print("c is greatest")



4)  Ladder if-else using elif statement:-

It provides a simple syntax structure as compare to nested if-else .ladder if-else will work step by step means if the first condition will be true then it will execute it, if it will be false then execute elif block at last if all condition will be false then else statement will execute.

if condition: 

    statement

elif condition:    

   statement

elif condition:
   statement

else:  

 statement

 ................................................................................................

a = int(input("enter first number"))
b = int(input("enter second number"))
c = int(input("enter third number"))

if a>b and a>c:
        print("a is greatest")
elif b>c:
        print("b is greatest")

else:
        print("c is greatest") 


Another Program of Ladder if--else:-


num = int(input("enter number"))
if num>=-9 and num<0:
    print("one digit negative number")
elif num>0 and num<=9:
    print("one digit postive number")
elif num>=-99 and num<-9:
    print("two digit negative number")
else:
    print("two digit positive number")
           
       
 

5) Multiple If:-

We can create more then one if statement to check multiple conditions and it will provide multiple results.

Q WAP to check divisibility of number from 3,5 and 9 with all combination:-

 num = int(input("enter number"))
if num%3==0:
    print("divisible by 3")
if num%5==0:
    print("divisible by 5")
if num%9==0:
    print("divisble by 9")



ASSIGNMENT OF IF__ELSE:-

1)  WAP to increase Salary of employee from 500 if entered salary is less then 10000 otherwise increase by 800?
2) WAP to Check Leap Year? 
3) WAP to check that entered input is numeric char or alphabet char?

ch = input("enter char")

if ord(ch)>=48 and ord(ch)<=57:

    print("Numeric")

elif (ord(ch)>=65 and ord(ch)<=90) or (ord(ch)>=97 and ord(ch)<=122):

    print("Alphabet")

else:

    print("Other")


4) WAP to display "YES" and "NO" when user press 'y' and 'n'?
5) WAP to check middle number in three digit number ?Solution of this program:-


num = int(input("enter number to check middle number"))
a= num%10  #3
num=num//10 #12
b=num%10   #2
c=num//10   #1

if a>b and a<c or a>c and a<b:
     print(str(a)+" is middle number")
if b>a and b<c or b>c and b<a:
     print(str(b)+" is middle number")
if c>a and c<b or c>b and c<a:
    print(str(c)+" is middle number")

 


6) WAP to check number is positive or negative?
7) WAP to calculate gross Salary and net salary of employee where basic,ta,da,comm,pf,numberofleave will be entered by the user?
8) WAP to calculate greatest using four different number using nested and ladder both?

a = int(input("enter first number"))

b = int(input("enter second number"))

c = int(input("enter third number"))

d = int(input("enter fourth number"))

if a>b:

    if a>c:

        if a>d:

         print("a is greatest")

        else:

         print("d is greatest")

    else:

        if c>d:

         print("c is greatest")

        else:

         print("d is greatest")

else:

    if b>c:

        if b>d:

         print("b is greatest")

        else:

         print("d is greatest")

    else:

        if c>d:

         print("c is greatest")

        else:

         print("d is greatest")



9) WAP to check that entered char is vowel and consonent without using or oeprator?


  ch = input("enter char")

if ch=='a':
   print("Vowel")
else:
   if ch=='e':
     print("Vowel")
   else:
     if ch=='i':
        print("vowel")
     else:
        if ch=='o':
          print("vowel")
        else:
         if ch=='u': 
           print("vowel")
         else:
           print("Consonent")
           


10) WAP to Calculate Marksheet using five different subjects with the following condition.
1) all subject marks should be 0 to 100.
2) if only one subject mark is <33 then the student will be suppl.
3) if all subject marks are>33 then percentage and division should be calculated.
4) if a student is suppl then five bonus marks can be applied to be pass and the result will be "pass by grace".
5) Display Grace Subject name, distinction subject name,suppl subject name and failed subject name.


Solution of this program:-

s1= input("enter first subject name")
m1 = int(input("enter marks"))
s2= input("enter second subject name")
m2 = int(input("enter marks"))
s3= input("enter third subject name")
m3 = int(input("enter marks"))
s4= input("enter fourth subject name")
m4 = int(input("enter marks"))
s5= input("enter fifth subject name")
m5 = int(input("enter marks"))

if((m1>=0 and m1<=100) and (m2>=0 and m2<=100) and (m3>=0 and m3<=100) and (m4>=0 and m4<=100) and (m5>=0 and m5<=100)):
   c=0
   g=0
   sub=""
   dist=""
   if m1>=75:
       dist=dist+s1+" "
   if m2>=75:
       dist=dist+s2+" "
   if m3>=75:
       dist=dist+s3+" "
   if m4>=75:
       dist=dist+s4+" "
   if m5>=75:
       dist=dist+s5+" "    
   if m1<33:
       c=c+1
       g=m1
       sub=sub+s1
   if m2<33:
       c=c+1
       g=m2
       sub=sub+s2
   if m3<33:
       c=c+1
       g=m3
       sub=sub+s3
   if m4<33:
       c=c+1
       g=m4
       sub=sub+s4
   if m5<33:
       c=c+1
       g=m5
       sub=sub+s5
   if c==0 or (c==1 and g>=28):
      per= (m1+m2+m3+m4+m5)/5
      if per>33 and per<45:
          print("pass with third division")
      elif per<60:
          print("pass with second division")
      else:
          print("pass with first division")
      if(dist!=""):
       print("distinction subject name is "+dist)
          
      if c==1: 
       print("pass by grace and grace mark is "+str(33-g)+" subject is "+sub)
     
   elif c==1:
      print("suppl")
   else:
      print("fail")
else:
   print("entered subject marks should be 0 to 100")

11)WAP to check that salary is in income tax criteria or not .if in income tax then display income tax slab.




5 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. # increase salary of employee from 500 if entered salary will be less then 10000 otherwise same salary will be displayed.

    salary=int(input(" Enter the employee salary :"))
    if(salary<10000):
    salary=salary+500;
    print("\n Total salary of employee is :",salary)

    ReplyDelete
  2. #display mark of student which will be entered by the user's ,if mark is eligible for grace then display mark including grace mark otherwise actual mark will display.
    marks=int(input("Enter the marks : "))
    if marks>=28 and marks<33:
    grace=33-marks
    print("\nEligibal for grace ",grace)
    marks=marks+grace

    print("\n Total marks after grace ",marks )

    ReplyDelete
  3. #check that entered number is one digit number or above one digit number ,number can be positive or negative?
    number=int(input("Enter the number :"))
    if(number>=9 or number<=-9):
    #if(number<-9):
    print("\n Above one digit numner")
    else:
    print("\n It's one digit number")

    ReplyDelete
  4. # check that entered number is one digit positive number,negative or two digit positive number or negative ,number can be positive and negative?
    number=int(input("Enter the number : "))
    if(number>=9 or number<=-9):
    print("\n Above one digit numner")
    else:
    print("\n It's one digit number")
    if(number>=0):
    print(" \n Number is positive")
    else:
    print("\n Number is negative")

    ReplyDelete
  5. #check divisibility of number from 3,5 and 9 with all combination:-
    number=int(input(" Enter the number : "))
    '''if(number%3==0 or number%5==0 or number%9==0):
    print("\n Number can be devide with given number : ")
    else:
    print("\n Number can't be devide with given number : ")'''
    if number%3==0:
    print("\n Number can be devide with number 3 ")
    if number%5==0:
    print("\n Number can be devide with number 5 ")
    if number%9==0:
    print("\n Number can be devide with number 9 ")
    else:
    print("\n Number can't be devide with number")

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post