Control Statement in python
Control Statement in python
Control Statement in python
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
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".
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
Program 7
i=1
while i < 6:
print(i)
if (i == 3):
break
i += 1
Program 8
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
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
Example
Loop through the letters in the word "banana":
Program 10
for x in "banana":
print(x)
Example
Exit the loop when x is "banana":
Program 11
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):
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"
When the pass statement is executed, nothing happens, but you avoid getting
an error when empty code is not allowed.
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())