FLOW CONTROL STATEMENTS/CONDITIONAL STATEMENTS AND FUNCTIONS-PYTHON

 

                               FLOW CONTROL STATEMENTS/CONDITIONAL STATEMENTS

 

  • IF STATEMENT :

If condition is true if following statements will be executed. If condition is false if following statements will not be executed.

IF SYNTAX:

if condition :
   statement-1
   statement-2
   statement-3

 

  • Write a program to read 3 subjects marks of a student. Calculate total ,average, ( if avg>60) then print “Qualified” along with total average marks.
a,b,c=[eval(x) for x in input(“Enter 3”).split()]
t=a+b+c
avg=t/3
if avg>60:
   print(“qualified”)
   print(“total={} /n average={}”.format(t,avg))
   print(“program ended”)

 

  • Write a program to read age of a candidate in years if age is >=18 then only read candidate name, mobile number and city and confirmation message.
n=int((input(“Enter age”))
if n>=18 :
   name=input(“Enter name”)
   mobile=int(input(“Enter mobile no”)
   city=input(“Enter city”)
   print(“ name:”,name)
   print(“mobile no:”,mobile)
   print(“city:”,city)
   print(“your are successfully registered”)
  • IF ELSE STATEMENT :

If condition is true if following statements will be executed. If condition is false else following statements will be executed.

IF ELSE SYNTAX:

if condition :
   statement-1
   statement-2
else :
   statement-3
   statement-4

 

  1. Write a program to read a name from the keyboard if name is geetha then print good morning, otherwise print good evening
name=input(“Enter your name”)
if name==”geetha” :
  print(“good morning”, name)
else :
  print(“good evening”, name)

 

  • Write a program to find given number is even number (or) odd number.
n=int(input(“Enter number”)
if(n%2==0) :
   print(“even number =”,n)
else :
    print(“odd number=”,n)

 

  • EL-IF STATEMENT :

To check condition from more than 2 expressions then we use el-if statement.

   EL-IF SYNTAX:

if condition:
   statement-1
   statement-2
elif condition :
   statement-3
   statement-4
else :
   statement-5
   statement-6
  • Write a program to find biggest number among 3 numbers.

a,b,c =[eval(x) for x in input(“Enter 3 numbers”).split()]
if a>b :

if a>c :
   print(“{} is big “.format(a))
elif b>c :
   print(“{} is big”.format(b))
else
   print(“{} is big” .format(c))
  • Write a program to read 5 subjects marks of a student calculate total, avg and print the result with following details:
t=a+b+c+d+e
avg=t/5
if  avg>=75 :
   print(“Distinction”)
elif  avg>=60 :
   print(“1st class)
elif  avg>=45 :
   print(“2nd class”)
else :
   print(”3rd class”)

 

WHILE LOOP :

  • Write a program to read list of elements from keyboards and print even numbers to console the given list.
l=eval(input(“Enter a list”))
for x in l:
   if  x%2==0
     print(x)
print(“program ends”)

  • Write a program to find the sum of digits of a given number.
n=int(input(“Enter a number”))
sum=0
while n!=0 :
 r=n%10
 sum=sum+r
 n=n/10
print(“Sum of digits”, sum)

  • Write a program to find given number is Armstrong number or not
n=int(input("enter n value")
sum=0
while n!=0 :
   r=n%10
   sum=sum+r*r*r
   n=n/10
if sum==x:
   print(“Armstrong”)

  • Write a program to print the nth table
n=int((input(“Enter a number”))
i=1
while i<=10 :
   m=n*i
   print(“{}*{}”.format(n,i,m))
   i=i+1

  • Write a program to print the following pattern
*

*  *

*  *  *

*  *  *  *
n=int(input(”Enter no. of rows”))
for x in range (1, n+1) :
  for y in range(x) :
    print(“*”, end= ‘ ‘)
print( )

     Write a program to print the following pattern.
*

*  *

* * *

* * * *

* * * * *
n=int(input(“Enter no of rows”))
for x in range(1,n+1)
 for y in range (1, n-x+1):
   print( “ “,end=’ ‘)
 for z in range(x):
   print(“*”,end=’ ‘)
print()

        • Write a program to print the following pattern :

* * * * *

* * * * *

* * * * *

* * * * *

n=int(input(“Enter no of rows”))
for x in range (1,n+1) :
 for z in range (1,n+1) :
  print(“*”, end=’ ‘)
print()

        • Write a program to print the following pattern
1

2 2

3 3 3

4 4 4 4


n=int(input(“Enter no. of rows “))

for x in range (1,n+1):

  for y in range(1, n-x+1):

    print(“ “,end=’ ‘)

  for z in range(x):

     print(x, end=” “)

print()


        • Write a program to print the following pattern:

1

1 2

1 2  3

1 2 3 4

n=int(input(“Enter no. of rows”))
for x in range (1,n+1):
 for y in range(1, n-x+1):
  print(“ “,end=’ ‘)
 for z in range(1,x+1) :
  print(z, end=” “)
print()

        • Write a program to print the following format
1

2   3

4  5  6

7  8  9  10

n=int(input(“Enter no. of rows)
p=1
for x in range (1, n+1)
  for y in range(1, n-x+1) :
     print(“ “, end=” “)
   for z in range(1,x+1) :
   print(p,end=’ ‘)
p=p+1
print()




USING CONTINUE:
  • Write a program to print 5 positive numbers:
i=1

while i<=5 :
n=int(input(‘enter a number’))
if n<=0:
 continue
else :
 print(n)
i=i+1

        • Write a program to find the sum of n integers
sum=0
n=int(input(“Enter a number”))
for x in range(n)
y=int(input(“Enter a number”))
sum=sum+y

 

        • Write a program to read list of cities in India and print welcome with city name If city name is Vijayawada then loop terminates.
while(True):
n=input(“Enter city”)
if city= “Vijayawada” :
break
else:
print(“Welcome”,n)
print(“program ended “)
        • Write a program to print Fibonacci series of first n terms
t1=0
t2=1
print(t1, t2)
n=int(input(“Enter n value”)
i=3
while i<=n :
t3=t1+t2
print(t3)
t1=t2
t2=t3
i=i+1
        • Write a program to print the “ fibonacci series upto n”:
t1=0
t2=1
print(t1, t2)
n=int(input(“Enter n value”))
t3=t1+t2
while t3<=n:
print(t3)
t1=t2
t2=t3
t3=t1+t2
        • Write a program to count the Fibonacci series upto n
x=2
t1=0
t2=1
print(t1, t2)
n=int(input(“Enter n value “))
t3=t1+t2
while t3<n :
x=x+1
t1=t2
t2=t3
t3=t1+t2
print(x)

 

FUNCTIONS :

Function is sub-routine, which is design to perform a specific task. Functions ccontains 3 parts and they are as follows:

 

        1. FUNCTION DECLARATION :

Simply declaring a function.

Ex-1:

def add(arguments):

 statements-1

 statements-2

 statements-3

add(arguments)

 

 

        • POSITIONAL ARGUMENT :

In positional arguments order is important.

Ex-1:

def add(a,b):

  print(a+b)

def sub(a,b)

  print(a-b)
 
add(2,3)      #5

sub(10,10)    #0

 

Ex-2:

def add ():

  a=10

  b=20

  print(a+b)

def sub() :

  print(a-b)

add()

sub()


        • Write a program to find add, sub,mul of 2 integers. Using function.
def calc(a,b) :
print(a+b)
print(a-b)
print(a*b)
calc(2,3)   #6, -1,6
calc(10,20)   # 30,-10,200
2)  KEYWORD ARGUMENT :

In this keyword argument order is not important. Here, positional argument is followed by keyword argument.

Ex-1:
def add(a,b) :
 print(a+b)
def sub(a,b)
 print(a-b)
add(a=10,b=20)  #30
sub(b=20,a=10)  #-10
add(b=1,a=3)   #4
sub(a=10, b=25)  #-15

 

Ex-2 :

def add(a,b):
 print(a+b)
def sub(a,b):
 print(a-b)
add(10,a=20)  #ERROR
sub(b=20,15)  #ERROR
sub(10,b=15)   #-5

 

3)  DEFAULT ARGUMENT :
Ex-1:
def add(a,b=20)
 print(a+b)
add(10,50)  #60 [It overrides]
add(10)    #30
4) VAR-ARG PARAMTER :

We can pass multiple values for a single variable. Var-Arg variable is tuple object.
(*t).

Ex-1 :
def add(*t)
 c=a+b
 return c
print(add(10,20))

 

Ex-2:

def add(a,b)
 c=a+b
 print( c)
print(add(10,20)

 

Ex-3:

def add(*t)
 s=0
 for x in t :
   s=s+x
   print(s)
add(10)   #10
add(10,20)   #30
add(10,20,30)  #60
add(10,20,30,40)  #100

Related Posts