Conditional statements in python

Conditional statements in python are compound statements, a compound statement represents a group of statements executed as a unit, here we learn conditional statements with some simple programs.

Conditional statement:

Conditional statements are based on some conditions.

Here are some conditions:

If, if else, if elif and nested if Let us discuss these all conditions

The if condition

If statement tests a particular condition, if the statement is true then it will print the result otherwise it gives nothing

Example:

a=2

if a==2:

    print(“Hello readers”)

it gives the result/output:

Hello readers

if condition, example of if condition, double equality operator

Explanation of the above program: first we take a variable (a), we assigned value to (a)= 2 then we apply the <if> condition which said if (a) is equal to 2 then print hello readers

In the output or result, you can see our message is printed which is hello readers because our (if) condition was true

if you want to learn about all type of expression or arithmetic expression in python here is link for you readers

Now let us suppose that the condition is false

a=2

if a==4:

print(“Hello World”)

explanation:

here we did some changes in the program to understand what happens (if) condition was false

first, we take a variable (a), assigned value to (a)= 2 then apply the <if> condition which said if (a) is equal to 4 then print hello readers

(a) is equal to 2 but in (if) condition (a) is equal to 4.

so here our output or result prints nothing because our condition is false,

example of if condition

We can make a flowchart/raptor to explain this program If you want to learn about flowcharts/raptors here is link for you

flowchart, flowchart of if condition

The if else condition

If the else statement works as the same (if) statement, the (If else) statement tests a particular condition, if the statement is true then it will print the result, if the statement was false then it moves to the else statement and print the result of else

Example:

x=2

y=3

if x<y:

    print(“welcome readers”)

else:

    print(“hello readers”)

output/result:

Welcome readers

if else condition, relational operator

Explanation of the above program:

First, we take two variables named x and y, x assigned 2 and y assigned 3 then we apply the condition if  x is smaller than y it will print welcome reader and then we apply the else statement which is print hello readers

Explanation of the output/result: In this program, our (if) condition is true because x is smaller than y as we apply the condition, here compiler found true so the program will end here.

Flowchart/raptor:

flowchart of if else

Note: python is case sensitive, so make sure to write correct order or in a correct block to avoid indentation errors, to understand the basics of python go to the link

The if elif condition

In this statement, you can add multiple conditions by using the keyword elif

Example:

x=4

y=3

if x<y:

    print(“welcome readers”)

elif x>y:

    print(“learn with me”)

else:

    print(“hello readers”)

output/result:

learn with me

elif condition

Explanation of the above program

In this program, we take two variables x and y, x=4, and y=3 then apply the (if) condition if x is smaller than y it will print a welcome reader, then we use the elif condition which said x is greater than y, then we apply else condition which said hello readers  in this program else condition works when all the condition were false

Explanation of the output/result

The output of the above program is learn with me because our elif condition were true for this program

X is greater than y

The nested if statement

The nested if statement allows multiple conditions with the use of (if), in this, we can use it multiple times (if) to create or add new conditions or you can say there is if statement inside if statement

Example:

x=int(input(“enter 1st number:”))

y=int(input(“enter 2nd number:”))

if x<y:

    print(“i love python”)

else:

    if x>y:

        print(“learn with me”)

if x==y:

     print(“welcome readers”)

output/result:

enter 1st number: 4

enter 2nd number: 4

Welcome readers

nestid if

Explanation of the above program:

In this program, we take two variables a and b but we didn’t give value, here we take values from the user

by using <int(input(“whatever you want to print”))> here int is for numbers and input is for alphabets or characters, we use int or input both so that the user can enter both numbers as well as characters, if you didn’t use int here you can only able to use characters it will not take numbers

we apply nested if conditions in the above program and you can see in the above picture after the execution of the program it will ask for the 1st and 2nd numbers as we write in the program, we enter the 1st and 2nd numbers 4 and 4

according to conditions both the numbers 1st and 2nd are equal (x==y) so it will print welcome readers

note: here we use else in-between program but you can do with if only

which type of conditional statement is it?

it is a compound statement which represent a group of statement executed as a unit

how if condition different from elif?

in the if condition you can give only one condition the program will be true or it prints nothing because of 1 condition and in the elif condition you can give multiple conditions for multiple results

what is a flowchart?

it is a symbol representation of program, it contain different types of symbols which have different meaning or uses, from this you can easily understand the program