28.10.2021 Views

Python Tutorial ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

if statements

if...else statements

nested if statements

An if statement consists of a boolean expression followed by one or more

statements.

An if statement can be followed by an optional else statement, which

executes when the boolean expression is false.

You can use one if or else if statement inside another if or else

if statement(s).

If statements

The if statement of Python is similar to that of other languages. The if statement contains a logical expression

using which data is compared and a decision is made based on the result of the comparison.

Syntax:

The syntax of an if statement in Python programming language is:

if expression:

statement(s)

If the boolean expression evaluates to true, then the block of statement(s) inside the if statement will be

executed. If boolean expression evaluates to false, then the first set of code after the end of the if statement(s)

will be executed.

Python programming language assumes any non-zero and non-null values as true, and if it is

eitherzero or null, then it is assumed as false value.

Flow Diagram:

Example:

#!/usr/bin/python

TUTORIALS POINT

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!