KEMBAR78
Conditional Statements | PDF | Python (Programming Language) | Computing
0% found this document useful (0 votes)
6 views11 pages

Conditional Statements

Conditional statements
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Conditional Statements

Conditional statements
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CONDITIONAL STATEMENTS

127
• Python Conditions and If statements
Python supports the usual logical conditions from mathematics:
1.Equals: a == b

2.Not Equals: a != b

3.Less than: a < b

4.Less than or equal to: a <= b

5.Greater than: a > b

6.Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in "if statements" and loops.

128
An "if statement" is written by using the if keyword.

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

In the above eg we use two variable and an operator. We use if statement to check whether
it is true or not.

We say like if that is true then , print “b is greater than a”

Python runs the code and check if that is true then it will print .

129
Indentation
Indentation is the white space at the beginning of the line to define scope in the
code. Other programming languages often use curly-brackets for this purpose.

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

If we type the code like this, then we get the error message because we miss the
indentation.

130
Elif
Elif says if the above condition is not true try these.
Which is it is same as if but it says 1st if condition is false then try elif to the system.

Eg: a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")

If we run the above program then, python check if statement first then if that's true
then it will print that statement, in this scene we get the elif statement , if is wrong
and python will run elif statement.

131
Else
If all the conditions fails to meet ,then the python execute the else condition
statement.
Eg :
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

In the above code , python will check 1st two condition and that will be false and it
will execute else statement.

132
If we have only one statement to execute, we can put it on the same line as the if
statement , which is called shorthand if.
Example of one line if statement.
if a > b: print("a is greater than b")
Eg :
a=2
b = 330
print("A") if a > b else print("B")
We can also have multiple else statements on the same line:
Eg:
a = 330
b = 330
print("A") if a > b else print("=")

We can use logical operators in if statement which is and , or. 133


And
The and keyword is a logical operator, and is used to combine
conditional statements.
Eg :
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")

The above code will execute when both the statements are true.
134
Or
The or keyword is a logical operator, and is used to combine conditional
statements.

Eg:
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")

In the above code, “or” is used, which is, python will execute when one
condition true.

135
Nested If
We can have if statements inside the if statement, that is called nested if.
Eg :
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")

1st python will check x>10 if that is true then it will print “above ten”.then it will
come to next command, which is x > 20 , if that is true then we get “and also
above 20!”.
If that both statements are false we get else statement.

136
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.

Eg: a = 33
b = 200
if b > a:
pass

137

You might also like