Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Conditional Statements In Python

 

In this lecture, we will see that What is Conditional statement ?, Which are different conditional statements ?, and How to implement Conditional Statements with Examples ?

 

Generally if we do any kind of for we have certain conditions, like if you want to drive you must be 18 or above 18. Similarly there are certain conditions in programming, if that conditions are true then only the code will execute.

 

Conditional expressions and Conditional constructs are features of a programming language, which perform different computations or actions depending on whether a program was specified Boolean condition evaluates to true or false.

 

We have four type of conditional statements,

  1. If
  2. If-else
  3. If-else if-else
  4. Nested if-else

 

If Statements

 

Syntax :

if expression:

statement(s)

Example :

x=15

if x>0:

print('Positive Value')

 

Now we will see practically there how this if statement works,

x=15

if x>0:

print('Positive Number')

 

Now we will run it and we'll see our output as Positive Number.

 

If we write x is equal to any negative number then and the condition will be false, and therefore console will show no output.

 

If-else Statements

 

We have seen that if the condition is true then a statement will be printed in 'if' statement, but now we will see that if the condition is false then and also a what statement will be printed with the help of 'if else' statement.

 

Syntax :

if expression :

statement(s)

else :

statement(s)

 

Example :

age=13

if age>18 :

print('You can drive')

else :

print('You can't drive')

 

If-else if-else Statements

 

Syntax :

if expression :

statement(s)

elif expression :

statement(s)

else :

statement(s)

 

Example :

x=15

if x>0 :

print('Positive Value')

elif x<0 :

else :

print('value is zero')

 

Another Example :

name='123'

if name=='ABC':

print('Hello ABC')

elif name=='123':

print('Hello Numbers')

elif name=='+/*':

print('Hello Operators')

else :

print('Good bye all of You')

 

Nested if Statements

 

Syntax :

if expression :

statement(s)

if expression :

statement(s)

else :

statement(s)

else :

statement(s)

 

Example :

x=15

if x>0:

print('Positive Value')

if x<10:

print('Value less than 10')

else:

print('Value more than 10')

elif x<0:

print('Negative Value')

else:

print('Value is ZERO')

 

FAQs

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere