You are on page 1of 4

Python Conditional Statements

Branching is an all-important concept to learn when programming in Python. If, Else and Elif
statements enable your program to be smart and make decisions.

Conditional Statement in Python performs different computations or actions depending on


whether a specific Boolean constraint evaluates to true or false. Conditional statements are
handled by IF statements.

If Statement is used for decision making. It will run the body of code only when IF statement is
true.

When you want to justify one condition while the other condition is not true, then you use "if
statement".

Syntax:

if expression
Statement
else
Statement

Example 1

x,y =2,19

if(x < y):


st= "x is less than y"
print(st)
Example 2

x,y =18,4

if(x < y):


st= "x is less than y"
else:
st= "x is greater than y"
print (st)
Example 3

x,y =28,28

if(x < y):


st= "x is less than y"
elif (x == y):
st= "x is same as y"

else:
st="x is greater than y"
print(st)
Example 4

Shorter Code

x,y = 10,8
st = "x is less than y" if (x < y) else "x is greater than or equal to y"
print(st)

Nested IF

Example 5

total = 100
#country = "US"
country = "AU"
if country == "US":
if total <= 50:
print("Shipping Cost is $50")
elif total <= 100:
print("Shipping Cost is $25")
elif total <= 150:
print("Shipping Costs $5")
else:
print("FREE")
if country == "AU":
if total <= 50:
print("Shipping Cost is $100")
else:
print("FREE")

Try-Except Statements
Try-except statements are another selection structure in Python.

Like if, elif and else statements, a try-except statements select a particular block of code to run
based on a condition. Unlike if, elif and else clauses, try-except blocks are not based on logical
conditions. Try-except blocks are based upon whether a line or section of code returns an error.

There are two types of errors in Python: syntax errors and exception errors.
Syntax Errors

A syntax error is a type of error in Python that occur when the syntax in a line of code is not
valid Python code. Syntax errors include quotes that are not closed and variable names that do
not start with a letter.

Exception Errors

Exception errors result when a valid line of Python code cannot run. Lines of code with
exception errors contain valid Python code, but the line of code still cannot be executed.

Try except statements can be used to try to run sections of Python code that may return an
exception error. The general syntax of a try except statement is below:
try:
<code to try>
except:
<code to run instead>

Example 6

a)
try:
a = 5
print(a[0])
except:
print('variable a is not a list')

b)
try:
a = 'Solution'
print(a[0])
except:
print('variable a is not a list')

Loop

A loop is a piece of code in a program that automatically repeats. One complete execution
within a loop is called and iteration or pass. The length of the loop is controlled by a conditional
test made within the loop.

While loop

Example 7 – Fibonacci Sequence of numbers

a, b = 0, 1
while b< 100 :
print (b)
a,b = b, a + b
For loop

Example 8
chars = [‘A’, ‘B’, ‘C’]
fruit = (‘Apple’, ‘Banana’, ‘Cherry’)
dict = {‘name’: ‘Mike’,’ref’ : ‘Python’, ‘sys’ : ‘Win’}
print(‘\nElements:\t’, end = ‘ ‘)
for item in chars :
print(item, end = ‘ ‘)
print(‘\nEnumerated:\t’, end = ‘ ‘)
for item in enumerate (chars) :
print(item, end = ‘ ‘)
print(‘\nZipped:\t’, end = ‘ ‘)
for item in zip (chars, fruit) :
print(item, end = ‘ ‘)
print(‘\nPaired:\t’, end = ‘ ‘)
for key, value in dict.items() :
print(key, ‘=’ ,value)

The break keyword can be used to prematurely terminate a loop when a specified condition is
met.

You might also like