Control Statement in python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Control Statement

The If statement
An "if statement" is written by using the if keyword.
Program1

a = 33
b = 200
if b > a:
print("b is greater than a")
Explaination of above Program

In this example we use two variables, a and b, which are used as part of the
if statement to test whether b is greater than a. As a is 33, and b is 200, we
know that 200 is greater than 33, and so we print to screen that "b is greater
than a".

Indentation

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.

Elif
The elif keyword is Python's way of saying "if the previous conditions were not
true, then try this condition".
Program2

a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Else
The else keyword catches anything which isn't caught by the preceding
conditions.

Program3

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 this example a is greater than b, so the first condition is not true, also
the elif condition is not true, so we go to the else condition and print to screen
that "a is greater than b".

You can also have an else without the elif:


Program4

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

Short Hand If
If you have only one statement to execute, you can put it on the same line as
the if statement.
Program 5
a = 200
b = 33
if a > b: print("a is greater than b")

Python Loops
Python has two primitive loop commands:

 while loops
 for loops

The while Loop


With the while loop we can execute a set of statements as long as a condition is
true.
Program 6
i=1
while i < 6:
print(i)
i += 1

The break Statement


With the break statement we can stop the loop even if the while condition is
true:

Program 7

i=1
while i < 6:
print(i)
if (i == 3):
break
i += 1

The continue Statement


With the continue statement we can stop the current iteration, and continue
with the next:

Program 8

i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)

# Note that number 3 is missing in the result


Output:
1
2
4
5
6

Python For Loops


A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).

This is less like the for keyword in other programming languages, and works
more like an iterator method as found in other object-orientated programming
languages.With the for loop we can execute a set of statements, once for each
item in a list, tuple, set etc.

Program 9
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

Output
apple
banana
cherry

Looping Through a String


Even strings are iterable objects, they contain a sequence of characters:

Example
Loop through the letters in the word "banana":

Program 10

for x in "banana":

print(x)

The break Statement


With the break statement we can stop the loop before it has looped through all
the items:

Example
Exit the loop when x is "banana":

Program 11

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)
if x == "banana":
break

The infinite loop


We can create an infinite loop using while statement. If the condition of while
loop is always True , we get an infinite loop.

The infinite loop


We can create an infinite loop using while statement. If the condition of while
loop is always True , we get an infinite loop.

Program 12: Infinite loop using while


# An example of infinite loop
# press Ctrl + c to exit from the loop

while True:
num = int(input("Enter an integer: "))
print("The double of",num,"is",2 * num)

Output

Enter an integer: 3
The double of 3 is 6
Enter an integer: 5
The double of 5 is 10
Enter an integer: 6
The double of 6 is 12
Enter an integer:
Traceback (most recent call last):

Program 13: Infinite loop using while

# An example of infinite loop


# press Ctrl + c to exit from the loop

while True:
num = int(input("Enter an integer: "))
print("The double of",num,"is",2 * num)

Output

Enter an integer: 3
The double of 3 is 6
Enter an integer: 5
The double of 5 is 10
Enter an integer: 6
The double of 6 is 12
Enter an integer:
Traceback (most recent call last):
Python assert Keyword

Program 14
x = "hello"

#if condition returns True, then nothing happens:


assert x == "hello"

#if condition returns False, AssertionError is raised:


assert x == "goodbye"

Python pass Statement


Definition and Usage
The pass statement is used as a placeholder for future code.

When the pass statement is executed, nothing happens, but you avoid getting
an error when empty code is not allowed.

Empty code is not allowed in loops, function definitions, class definitions, or in if


statements.

Program 15
for x in [0, 1, 2]:
pass
# having an empty for loop like this, would raise an error without the pass
statement
Python return Keyword
Definition and Usage
The return keyword is to exit a function and return a value.

Program 16

def myfunction():
return 3+3

print(myfunction())

You might also like