• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Python if-else

Learn Python if elif else

  • Categories Python if-else, Python, Python

In this post we will cover  if-elif-else in Python. You can watch our videos on if-elif-else in Python – Click Here

Q. 1 What is an if statement? 
Ans.

  • The if statement contains a logical expression using which a decision is made based on the result of the comparison.
  • Syntax:
    1. if expression:
         statement(s)
    2. if expression:
            statement(s)
      else:
            statement(s)

    3. if expression1:
            statement(s)
      elif expression2:
            statement(s)
      else:
            statement(s)

    • If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed.
    • If boolean expression evaluates to FALSE,
      • The first set of code after the end of the if statement(s) is executed
      • Or the else block is executed
      • Or the next elif block is executed.
  • If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed.
  • If boolean expression evaluates to FALSE,
    • The first set of code after the end of the if statement(s) is executed
    • Or the else block is executed
    • Or the next elif block is executed.

Q. 2 Which are two types of else clauses in Python?
Ans. The two types of python else clauses are:
else in an if statement– the else clause of an if statement is executed when the condition of the if statement results into false
else in a loop statement-The else clause of a loop is executed when the loop is terminating normally. i.e. when its test- condition has gone false for a while loop or when the for loop has executed for the last value in sequence

Q. 3 Is there any limit of statement that can appear under an if block.
Ans. No

Q.4 What is pass in Python?

Ans. In python pass statement does nothing. It is just an indication to interpreter to move to next executable statement.

Q. 5 What is the output of following code ?

if(4+5==10):
    print(“True”)
else:
    print(“False”)
print(“True”)

Ans. False
         True

Q. 6 What is the output of the following code if a=1 and a =4

if(a==0):
    print(“zero”)
if(a==1):
    print(“one”)
else :
   print(“a > 1”)

Ans.

  1. one
  2. a > 1

Q. 7 Under what conditions will this code fragment will print ”water”

if temp < 32:
    print("ice")
elif temp < 212:
   print("water")
else:
   print("steam")

Ans. if temp greater than > 32 and less than < 212 it will print water                                                                     

Q. 8 What is the output produced by the following code?

x=1
if x>3 :
    if x>4:
        print(“a”, end=”)
    else:
        print(‘b’,end=’ ‘)
elif x < 2:
    if(x != 0):
        print(“c”, end=’ ‘)
print(“d”)

Ans.  c d                            

Q.9 What is the error in following code ? Correct the code?

weather='raining'
if weather = "sunny":
   print("wear sunblock")
elif weather="snow":
   print("going skiing")
else:
    print(weather)

Ans.

weather='raining'
if weather == "sunny":
   print("wear sunblock")
elif weather=="snow":
   print("going skiing")
else:
   print(weather)
>>>raining

Q. 10 What is the error in following code ? Correct the code?

n=2
if n == 0
   print("zero")
elif : n==1
   print("one")
elif n==2:
   print("two")
else   n==3:
    print("three")

Ans.

n=2
if n == 0:
   print("zero")
elif  n==1:
   print("one")
elif n==2:
   print("two")
else:
    print("three")

Q. 11 Write a program to check if the number is buzz nos or not .
            (BUZZ nos are numbers which are divisible by 7 or end with 7)

     Ans.   

n=int(input("Enter a number:"))
if(n % 7 == 0 or n % 10 == 7):
   print (("It’s a BUZZ no"))
else:
   print ("Not a BUZZ no")

Q. 12 Write a program to check if the character is a vowel or not?
Ans.

ch=input("Enter a character:")
if ch in "aeiouAEIOU":
   print("It’s a vowel")
else:
     print ("Not a vowel")

Q. 13 Predict the output of the program

a = 10
if 5 < a < 15 :
  print (a / 2)
else:
  print (a / 5)

Ans.    >>>5.0

Q. 14 Write a program to accept percentage from the user and display the grade according to the following criteria:

         Marks                                    Grade

         > 90                                         A

         > 80 and <= 90                        B

         >= 60 and <= 80                      C

         below 60                                  D

Ans.

per = int(input("Enter marks"))
if per > 90:
   print("Grade is A")
if per > 80 and per <=90:
   print("Grade is B")
if per >=60 and per <= 80:
   print("Grade is C")
if per < 60:
    print("Grade is D")

Q.15 Write a program to check whether an years is leap year or not.
Ans.

yr=int(input("Enter the year"))
if yr%100==0:
   if yr%400==0:
         print("Entered year is leap year")
   else:
         print("Entered year is not a leap year")
else:
   if yr%4==0:
        print("Entered year is leap year") 
   else:
        print("Entered year is not a leap year")

Q. 16 Accept three numbers from the user and display the second largest number.
Ans.

num1=int(input("Enter first number"))
num2=int(input("Enter second number"))
num3=int(input("Enter third number"))
if (num1 > num2 and num1 < num3) or (num1 < num2 and num1 > num3):
    print("Middle number is " , num1)
if (num2 > num1 and num2 < num3) or (num2 < num1 and num2 > num3):
    print("Middle number is" , num2)
if (num3 > num2 and num3 < num1) or (num3 < num2 and num3 > num1):
     print("Middle number is" , num3)

 

Q.17 Write a Python program to test if a variable is a list or tuple or a set.
Ans.

#x = ['a', 'b', 'c', 'd']
#x = {'a', 'b', 'c', 'd'}
x = ('tuple', False, 3.2, 1)
if type(x) is list:
   print('x is a list')
elif type(x) is set:
   print('x is a set')
elif type(x) is tuple:
   print('x is a tuple')   
else:
    print('Neither a list or a set or a tuple.')

 

Q. 18 Consider the following code: If y has the value 2 after executing the above program fragment, then what do you know about the initial value of x?

if x > 3:
   if x <= 5:
       y = 1
   elif x != 6:
       y = 2
   else:
       y = 3
else:
        y = 4

Ans.    x has to be >= 7 for y =2

 

  • Share:
author avatar
Simply Coding

Previous post

Java Theory - Java Basics
June 15, 2021

Next post

Python Dictionary
June 15, 2021

You may also like

Python function
Python function
3 July, 2021
for & while
Learn Python for & while
19 June, 2021
Queue
Python Queue
17 June, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now