Control Statements in Python

We know that loops are used to execute a set of statements repeatedly sometimes we need to control execution of the program based on the values and the conditions, so to accomplish this task Control Statements are using in python ,python support three types of Control Statements Continue Statement,Break Statement and the Pass Statement. Lets see in details one by one

Continue Control Statement in Python

Sometimes we need to exclude particular iteration inside the loop in that case “Continue” Control Statements is used to accomplish control to the beginning of the loop.if the condition is true it will return to the beginning of the loop.

Example


# Continue Control Statement in Python

#for loop to print a number between 1 to 4 without 2

l = [1, 2, 3, 4] 
for i in l: 
    if i==2:
       continue
    print(i) 



Out put
1
3
4

Break Control Statement in Python

Sometimes we need to stop the iteration based on particular logic or condition Break Statement is used to accomplish this task .Break statement break stop the execution of the loops

Example

# Break Control Statement in Python


l = range(1, 100)
for i in l: 
    if i==10:
       break   #stop the execution here if i equal to 10
    print(i) 

Output

1
2
3
4
5
6
7
8
9

Pass Control Statement in Python

If we want to execute nothing pass statement can be used. Sometimes we need to implement functionality code after beta delivery of the project we use “pass” to keep a loop without anything


l = [1, 2, 3, 4] 
for i in l :  
    pass

how to define a function in python

As per the latest trends set python is the most exploring programming language,expert says in the middle of 21st century 70% of the day life application will be depend on python programming language, python and artificial intelligence definitely will brings miracle in the middle of 21st century.So being a programmer without python programming language you cant survive in 21st century.Like other programming language function in python is also so important.so basically our question is how to define a function in python.To define a function in python programming language, python has its on syntax. lets see the syntax

def FUNCTION-NAME( PARAMETER):
    FUNCTION_BLOCK
   return [EXPRESSION]

def            => Function blocks begin with this keyword def 
FUNCTION-NAME  => Name of the function you can define your own name

PARAMETER      => It the Parameter passing to the function when function was called

FUNCTION_BLOCK => Function code block

EXPRESSION     =>  return [EXPRESSION] passing back an expression to the caller

See example

def sum(a,b):
 resultvalue = a+b
 return resultvalue

x =10
y = 40
print(sum(x,y));
x =60
y = 40
print(sum(x,y));

how to define a function in python

def printPrime(lower,upper):
 print( "prime numbers between "+str(lower)+ ", "+str(upper))
 for num in range(lower,upper + 1):
    if num > 1:
       for i in range(2,num):
           if (num % i) == 0:
               break
       else:
           print(num)

x =10
y = 20
printPrime(x,y);
x =1180
y = 1200
printPrime(x,y);

how to define a prime number display function in python

python print function syntax

Most of the beginners in python always confuse with print function and start searching for print function in google.Like other programming language python print out put function also easy. In this tutorial we are going to explore python print ,the way to print the out put in python. print() is the function used in python to print out put.

Let see example

python print syntax

print(object(s), separator=separator, end=end, file=file, flush=flush)

object = any object, it will convert in to string and print

sep=’separator’ = Optional default ‘ ‘

end=’end’ = Default is line feed, will tell what print at the end

file = Default is sys.stdout , object with a write method.

1) python print a string “Hello World”



print("Hello World")
Output:

Hello World

2) python print a variable numberstart =10

 
numberstart =10
print(numberstart)
Output:

10


3) python print : managing tuble


tupleA = (2, 4, 8, 12) 
tupleB = ('red', 'blue')  
print(tupleA + tupleB ) 

Output:

(2, 4, 8, 12, 'red', 'blue')

python print function