Skip to main content

Python | Control Flow Tools

Control Flow Tools

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:-

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

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 i in range(5):
         print(i)

0
1
2
3

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

Popular posts from this blog

Basic Knowledge of Python part 1

Basic Mathematics equations using Python- As we know python is a simple and easy to learn programming language. In this section we learn how to done mathematics equations using python. To perform this we use our basic operators +,-,/,* .  To perform python we have to open python interpreter. In this interpreter you can type simple equation like 5+5, 4/2 etc. For example: The integer number (e.g. 1,2,3....) have type int and the number with fractional part (e.g. 2.5,2.1,4.5...) have type float. Note:-  The division type always returns float. To get only an integer result of division you can use the // operator and to get remainder from division result you can use % operator. In python you can easily calculate power of the number by using ** operator. Like this you can easily calculate mathematics equation. Now we see how to find area of rectangle. Area = length*width is the formula to find area of rectangle. Here '=' sign is used to store value to the variable. You can als...

Python | Data Structure or Data Types (String)

Data Type Of Python I n the last session we discus about what is the data type of python. And we see List data type. In this session we s ee the next data type String.     2) String- String is a ordered collection of immutable data type. String are iterable and hashable. Strings are always declared in single or double quotes.  Single line string:- S = "Python" Multi line string:- S = """ Line 1             Line 2             Line 3 """ Iterable:- S= "Program" for i in S:       print(i) Output:-  P  r  o  g  r  a  m Membership:-  print( 'p' in S) => True print( 'P' not in S) => False Methods of string:- capitalize() :-  It return the capitalize version of the string. More specifically it make the first character have uppercase and rest lowercase. For example:- S = "hello World" print(S.capitalize()) => "Hello world" count(substring, start,en...

Python | Data Structure Or Data Type (Tuple)

Data Type Of Python  In this session we will see last data type of python which is Tuple. 5) Tuple:- Tuple is a ordered collection of immutable datatype, tuples are quite similar to lists but difference is list are mutable where as tuple are immutable. Empty tuple:-  t = () or tuple() Tuple with single element:- t = (10,) Tuple with multiple element:- t1 = ( 10, 20, 30, 'Hello' ) Nested tuple:-  t1 = ( 10, 20, 30, 'Hello' ) p = t2, 30, 40, 50 print(p) => ((10, 20, 30, 'Hello'), 30, 40, 50) Methods of Tuple:-   count(value):- It return the count of value if the value is present in the tuple else it return 0. For example:- t = (10, 20, 30, 'Hello') t.count(10) => 1  t.count(50) => 0 index(value,start,end):- It return the index number if the value is present in the tuple, otherwise it raised value error. For example:- t = (10, 20, 30, 'Hello') t.index(10) => 0 It return the index number of value 10. Prev Topic   Next Topic