0% found this document useful (0 votes)
58 views8 pages

Exp 2

The document describes experiments conducted to understand Python control structures like if/else statements, while and for loops, and functions. Programs are provided to find the maximum of 3 numbers, calculate a factorial using a while loop, generate a Fibonacci series, create a multiplication table with a for loop, and build a basic calculator using functions. The experiments help learn Python programming concepts like control flow, loops, and defining reusable functions.

Uploaded by

Shaizad Sayyed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views8 pages

Exp 2

The document describes experiments conducted to understand Python control structures like if/else statements, while and for loops, and functions. Programs are provided to find the maximum of 3 numbers, calculate a factorial using a while loop, generate a Fibonacci series, create a multiplication table with a for loop, and build a basic calculator using functions. The experiments help learn Python programming concepts like control flow, loops, and defining reusable functions.

Uploaded by

Shaizad Sayyed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPERIMENT NO 2

Name of the Student :-__________________________________________________

Roll No.____________ Subject:-_______________________

Date of Practical Performed :-___________ Staff Signature with Date


& Marks

2.1 Write python programs to understand the Control Structures


#Control Structures remember the indentations
print "--Finding MAX BETWEEN 3 Nos using IF-Else-Elif STATEMENT--"
a=input("Enter value of First element:")
b=input("Enter value of Second
element:") c=input("Enter value of Third
element:") if a>b and a>c:
print a,"is Greater"
elif b>c:
print b,"is Greater"
else:
print c,"is Greater"
print ""

print "-----Calculate Factorial using WHILE LOOP-----------"


num=int(input("Enter the number : ")) fact=1

i=1
while i <= num:
fact=fact*i
i=i+1
print"Factorial of",num," is :",fact

print "-----Generate Fibonacci series using If statement & WHILE


LOOP-- ---------"
n=int(input("How many numbers??? : "))
f1=0
f2=1
c=2
if n==1:
print f1
elif n==2:
print f1,'\n',f2
else:
print f1,'\n',f2

while c<n:
f=f1+f2
print f
f1,f2=f2,f
c+=1

-------------------------OUTPUT----------------------------------------
Name of the Student :-__________________________________________________

Roll No.____________ Subject:-_______________________

Date of Practical Performed :-___________ Staff Signature with Date


& Marks

2.2 Write python programs to understand the For Loop

#FOR LOOP
print "--Multiplication table using FOR loop--"
n=int(input("Enter the number:"))
for i in range(1,11):
print n,"X",i,"=",n*i
print ""
print "--Pascal triangles--"

n=int(input("Enter the rows:"))


trow=[1]
y=[0]
for x in range(max(n,0)):
print(trow)
trow=[l+r for l,r in zip(trow+y, y+trow)]
n>=1

-------------------------------------------OUTPUT------------------------------------------------------
Name of the Student :-__________________________________________________

Roll No.____________ Subject:-_______________________

Date of Practical Performed :-___________ Staff Signature with Date


& Marks

2.3 Write python programs to understand Functions

#Functions to run basic calculator


def menu(): #Defining function Menu
#print what options you have
print ""
print "Welcome to calculator in Python"
print "your options are:"
print " "
print "1) Addition"
print "2) Subtraction"
print "3) Multiplication"
print "4) Division"
print "5) Quit calculator"
print " "
return input ("Choose your option: ")

# this adds two numbers given


def add(a,b): #Defining function add
print a, "+", b, "=", a + b

# this subtracts two numbers given


def sub(a,b): #Defining function sub
print a, "-", b, "=", a - b

# this multiplies two numbers given


def mul(a,b): #Defining function mul
print a, "*", b, "=", a * b

# this divides two numbers given def


div(a,b): #Defining function div
print a, "/", b, "=", a / b

# NOW THE PROGRAM STARTS, AS CODE IS


RUN loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
num1=int(input("Enter num 1 --: "))
num2=int(input("Enter num 2 --: "))
add(num1,num2)
elif choice == 2:
num1=int(input("Enter num 1 --: "))
num2=int(input("Enter num 2 --: "))
sub(num1,num2)
elif choice == 3:
num1=int(input("Enter num 1 --: "))
num2=int(input("Enter num 2 --: "))
mul(num1,num2)
elif choice == 4:
num1=int(input("Enter num 1 --: "))
num2=int(input("Enter num 2 --: "))
div(num1,num2)
elif choice == 5:
loop = 0

# End of the program


--------------------------------------------------------------------------------------------------------------
OUTPUT OF PROGRAM 2.3
----------------------------------------------------------------------------------------------------------------
Q1) What is Function in python?

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

Q2) Explain try block ?

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

Q3) Explain various control statement in python?

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

Q4) Explain break statement command?

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

Q5) How to define Functions Inside of Functions


.
--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

You might also like