Tuesday, May 9, 2023

#PYTHON(DAY13)Conditional statements

 

#PYTHON(DAY13)

Conditional statements

Python’s conditional statements carry out various calculations or operations according to whether a particular Boolean condition is evaluated as true or false.

Indentation:

If statement, without indentation (will raise an error):

a = 33
b = 200
if b > a:
print("b is greater than a")
And the output is:

File "demo_if_error.py", line 4
print("b is greater than a")
^
IndentationError: expected an indented block

if statement

  • syntax of ‘if’
  • ‘if’ conditional_expression :
  • code_block

flowchart:

IF STATEMENT

syntax:

if <conditional expression>  
Statement

Example:

a, b = 6, 5  

# Initializing the if condition
if a > b:
code = "a is greater than b"
print(code)

And the output is:

a is greater than b

if-else:

  • else the c.e is false, then the code_block under ‘else’ will be executed
  • ‘else’ does not have a c.e

flow chart of if-else

if-else

syntax:

if <conditional expression>  
Statement
else
Statement

Example:

a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")

And the output is:

a is greater than b

No comments:

Post a Comment

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...