Connection of if and bool

 Connection of if and bool :

The If Statement and True :

if statement and the bool data type. we can control which parts of our code run based on a condition. For example

  • if the condition is True, then the block of code run.
  • if its False, then the block of code skip

The If Statement and True 

Code :

bool_condition = True
#check condition using if 
if bool_condition:
    print("Code within if is running!")

where 

  • variable--bool_condition
  • Boolean condition set to true--True
  • if statement--if
  • block of code run if condition is true--print("Code within if is running!")

Output :

Code within if is running!

Try these conditions in your compiler

The If Statement and False

Code :

bool_condition = False
#check condition using if 
if bool_condition:
    print("Code within if is running!")

Output :


print some statements using if statements

Code :

is_sunny=True

if is_sunny:

    print("lets wear a t shirt!")

output:

lets wear a t shirt!

Code Explanation :

is_sunny : True sets the weather as sunny

if statement evaluate the value of is_sunny

Since is_sunny is true 

print the print("lets wear a t shirt!")

 note:

Error : indentation error when you add print statement without giving 4 typically spaces. we face the indentation error

<syntaxError: bad input on line 3 or indentation error expected line 3 


  • You have to must try in your python compiler use idle avoid online compilers to Errors

Post a Comment

0 Comments