Python Lab Programs
Python Lab Programs
AIM
To sway two variables using a temporary variable
PROGRAM
num1=int(input(“Enter num1”))
num2=int(input(“Enter num2”))
print(“Before Swapping”)
print(“num1=”,num1)
print(“num2=”,num2)
temp=num1
num1=num2
num2=temp
print(“After Swapping”)
print(“num1=”,num1)
print(“num2=”,num2)
RESULT
AIM
To find whether a given year is leap year or not
PROGRAM
year=int(input("Enter year"))
if(year%4==0):
if(year%100==0):
if(year%400==0):
print("The given year is a leap year")
else:
print("The given year is not a leap year ")
else:
print("The given year is a leap year ")
else:
print("The given year is not a leap year ")
RESULT
LAB PROGRAMS
PROGRAM
no1=int(input('Enter first no:'))
no2=int(input('Enter second no:'))
def computeGCD(x,y):
if x>y:
smaller=y
else:
smaller=x
for i in range(1,smaller+1):
if((x%i==0)and(y%i==0)):
gcd=i
return gcd
print"The gcd of",no1,"and",no2,"is",computeGCD(no1,no2)
OUTPUT
[root@cselab14 Python-3.6.1]# python gcdofnumbers.py
Enter first no: 8
Enter second no: 12
The gcd of 8 and 12 is 4
RESULT
PROGRAM
num=float(input('Enter a no:\t'))
approx=0.5*num
better=0.5*(approx+num/approx)
while (better!=approx):
approx=better
better=0.5*(approx+num/approx)
print (approx)
OUTPUT
[root@cselab14 Python-3.6.1]# python squareroot.py
Enter a no: 3
1.73205080757
RESULT
EX. NO: 3 EXPONENTIATION (POWER OF A NUMBER)
PROGRAM
base=int(input("Enter base:"))
exp=int(input("Enter exponent:"))
ans=1
for i in range(1,exp+1):
ans=ans*base
print "Result of exponentiation:", ans
OUTPUT
[root@cselab1-46 Python-3.6.1]#python exponentiation.py
Enter base:2
Enter exponent:5
Result of exponentiation:32
RESULT