DEV Community

Cover image for Python Foundation with Data Structures & Algorithms - Part 04
NirmaniWarakaulla
NirmaniWarakaulla

Posted on

Python Foundation with Data Structures & Algorithms - Part 04

Python Decision Making

Python supports the usual logical conditions from mathematics:

Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.

Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.

Using if Statement

1a

using if-else Statement

2b

Multiple if Statement

Here all the true conditions will be executed.

3c

if-elif Statement

Even if all the conditions are true, only first True condition will be executed rest all will not be executed.

4d

Nested if-else Statement

5e

Short Hand If

If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:

6f

if-else using Logical AND Operator

7g

if-else using Logical OR Operator

15.08.2021_09.26.50_REC

The pass Statement

if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.

8h

Find No is +ve/-ve

9i

Find the No is Even/Odd

10j

Find the No is Even/Odd ( Without using any Arithmetic Operator )

11k

Find the Greatest Between 2 No's

12l

Find the Greatest Between 3 No's

13m

14n

Calculate Ticket Price:

15o

Top comments (0)