Conditional and Looping Statements in Python with Examples

Jagadeshwaran_D
3 min readMay 22, 2021

For more programs https://github.com/Jagadeshwaran-D/Python_Basic_programs

Photo by James Harrison on Unsplash

Conditional Statement

A Program makes decisions based on conditions. If/Else statement, a common form of conditional statements in programming. It tells the computer that if the condition is true, do this. Else, if the condition is false, do another thing

IF Statement

In If conditional statement, If the condition is true it will execute the statement.

Syntax

if test-expression:

statement

Example:

IF-ELSE Statement

In If-else conditional statement, If the condition is true it will execute the statement or execute the else statement.

Syntax

if test-expression:

statement

else:

statement

Example:

IF-ELIF Statement

In If-elif conditional statement, If the condition is true it will execute the statement or execute the elif statement.

Syntax

if test-expression:

statement

elif test-expression:

statement

else:

statement

Example:

Looping Statement

In Programming Language, a loop is a programming structure that repeats a sequence of instructions until a specific condition is met. Programmers use loops to cycle through values, add sums of numbers, repeat functions, and many other things.

Loops are supported by all modern Programming Language, though their implementations and syntax may differ. Two of the most common types of loops are the while loop and the for loop

While Loop

The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true. In the while loop, the test expression is checked first. The body of the loop is entered only if the test expression evaluates to True. After one iteration, the test expression is checked again. This process continues until the test expression evaluates False.

Syntax

while test_expression:

statement

Example

python program for reverse a given number

In this Program, we can reverse a given number by using the while loop. After getting the input from the user, we passing the test expression (a>0) to the while loop. It will execute until the test expression fail. In the body of the loop, we use some mathematical operations to reverse a given number.

For Loop

A for loop is used for iterating over a sequence. For example: traversing a list or string or array etc. The for loop does not require an indexing variable to set beforehand. With the help of for loop, we can execute a set of statements, once for each item in a list, tuple and etc...

Syntax

for var in sequence :

body of for loop

Range Function

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

Example:

Output

1

2

Looping Through a String

Even strings are iterable objects, they contain a sequence of character

Example:

Output:

r
a
m

my other work https://jagadeshwaran-d.medium.com/basic-programs-in-python-ca01ef9dfe6d

keep support me on https://jagadeshwaran-d.medium.com

--

--