Control Flow Tools
1)if Statement:- In the programming language if statement is important statement. Now we see how to write if statement in the python program.
For Example:-
Here is the program of if statement. You can add one or more elif part in the program. else part is the optional.
In the program we take the value of x is 50. In the if statement we write x<0 condition which is false because 50 is not less than 0 so it will go to the next part of the code which is first elif part, here we give condition x==0 this condition is also false so it will go to the second elif part which is x==1 this condition is also false so it go to the else part of the program. If all conditions were false then else condition is true. So the output of the above program is More.
You must have to complete if statement with the colon. The part inside the if statement must be proper intended.
5) break statement:- The break statement terminates the loop.
For example:-
In this session we will see about control flow tools of python.
1)if Statement:- In the programming language if statement is important statement. Now we see how to write if statement in the python program.
For Example:-
>>> x = int(input("Please Enter an integer"))
Please Enter an integer 50
>>> if x<0:
x=0
print("Negative change to zero")
elif x==0:
print("Zero")
elif x==1:
print("One")
else:
print("More")
More
>>>
Here is the program of if statement. You can add one or more elif part in the program. else part is the optional.
In the program we take the value of x is 50. In the if statement we write x<0 condition which is false because 50 is not less than 0 so it will go to the next part of the code which is first elif part, here we give condition x==0 this condition is also false so it will go to the second elif part which is x==1 this condition is also false so it go to the else part of the program. If all conditions were false then else condition is true. So the output of the above program is More.
You must have to complete if statement with the colon. The part inside the if statement must be proper intended.
2) for Statements:- We use for statement in the programming for the loop. Now we see how to write for statement in python program.
For Example:-
For Example:-
>>> x = ['Python','Java','Php']
>>> for i in x:
print(i)
Python
Java
Php
In python for statement is very easy to write. We write 'for i in x' in our program it means that it collect all the elements from the list x, separate the elements of the list and store it into i variable. When you print i inside the for loop it return the all elements of list separately.
In this program for loop iterate three times first time 'Python' is store in i and print it then 'Java' and then 'Php'. If the list has n elements then for loop iterates n times.
In this program for loop iterate three times first time 'Python' is store in i and print it then 'Java' and then 'Php'. If the list has n elements then for loop iterates n times.
3) range() Function:- If you want to iterate a number then there is a built-in-function range() to iterate over a sequence.
For example:-
For example:-
>>> for i in range(5):
print(i)
0
1
2
3
4 In this program we use range() function. In the range function we insert value 5 that means it start from 0 to 4. If you write there 11 then it will start from 0 to 10.
In the range() function you can use start, stop and step attribute.
In the range() function you can use start, stop and step attribute.
For example:-
>>> for i in range(5,10):
print(i)
5
6
7
8
9
In this program value of start attribute is 5 and end attribute is 10 so it will return the numbers from 5 to 9. When you give the end value it will always take the value upto one number before it. For example if you give the end value 10 it will take value till 9. If you take end value 100 it will take 99.
4) enumerate(iterable,start=0) :- It returns an enumerate object, iterable must be sequence or some other object which support iteration. Enumerate returns the list of two tuple containing the index number and the values of that number.
For example:-
>>> l=['a','b','c']
>>> print(list(enumerate(l)))
[(0, 'a'), (1, 'b'), (2, 'c')]
5) break statement:- The break statement terminates the loop.
For example:-
for val in "String":
if val == "i":
break
print(val)
S
t
r
In this example we iterate the string using for loop. The each element of string is store in val attribute. Inside the for loop we give the condition if val == "i" that means when the value of val is become i the break the loop.
6) continue statement:- The continue statement is used to skip the code inside the loop for the current iteration only.
For example:-
For example:-
for val in "String":
if val == "i":
continue
print(val)
S
t
r
n
g
This is the same example of break statement. The difference is that instead of break statement we use continue statement. So you can see how the output change. In the output of continue statement i will be skip.
We see another example of continue statement.
for val in "Python":
if val == "t":
continue
print(val)
P
y
h
o
n
Same thing happen in this example also.
7) pass Statement:- When the pass statement is executed nothing will happen. By using pass statement you can avoid getting error when empty code is not allowed in loops.
For example:-
For example:-
for x in "String":
pass
In this example, if you don't written any thing in the for loop then it gives error. But when you write pass inside the loop it not affect your code but it doesn't give any error.
Comments
Post a Comment