Python Lab File
Python Lab File
Source Code:
n = int(input("Enter a number:\t"))
sqrt_n = n**0.5
print("Square root of given Number = ", sqrt_n)
OUTPUT
OUTPUT
Program 5: Write a program to solve Quadratic equation.
Source Code:
a = eval(input("Enter coefficient of x^2: "))
b = eval(input("Enter coefficient of x: "))
c = eval(input("Enter the Constant term: "))
d = b**2 - 4*a*c
sqrt_d = (abs(d))**0.5
if d==0:
print("Roots are real & equal")
print("root 1 = root 2 = ", -b/(2*a))
elif d>0:
print("root_1 = ", (-b + sqrt_d)/(2*a))
print("root_2 = ", (-b - sqrt_d)/(2*a))
elif d<0:
print("root 1 = ", -b/(2*a), " + i (%0.2f)" %(sqrt_d))
print("root 2 = ", -b/(2*a), " - i (%0.2f)" %(sqrt_d))
OUTPUT
Program 6: Write a program to Swap two Variables.
Source Code:
a = eval(input("Enter value of a = "))
b = eval(input("Enter value of b = "))
a, b = b,a
print("a = ", a, "b = ", b)
OUTPUT
Program 7: Write a program to generate a random number.
Source Code:
import random
print(random.randint(0,2303))
OUTPUT
Source Code:
def convert_km_to_miles(Km):
miles = km/1.60934
return miles
km = eval(input("Enter the distance in km:\t"))
print(km, "km in miles = %0.2f" %convert_km_to_miles(km))
OUTPUT
Source Code:
n = eval(input("Enter any number:\t"))
if n==0:
print("Number is equal to 0")
elif n>0:
print("Number",n ,"is Positive")
elif n<0:
print("Number",n ,"is Negative")
OUTPUT
Program 11: Write a program to Check a number is Odd
or Even.
Source Code:
n = int(input("Enter a number:\t"))
if n%2==0:
print(n, "is Even")
else:
print(n, "is Odd")
OUTPUT
Program 12: Write a program to Check Leap Year.
Source Code:
year = int(input("Enter any Year:\t"))
if year%4==0 and year%400==0:
print(year, "is leap year!!")
elif year%4==0 and year%100 != 0:
print(year, "is leap year!!")
else:
print(year, "is not leap year!!")
OUTPUT
Program 13: Write a program to find the largest among three
numbers.
Source Code:
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
c = int(input("Enter the third number: "))
if a>b:
if a>c:
print("Largest among the three numbers:",a)
else:
print("Largest among the three numbers:",c)
else:
if b>c:
print("Largest among the three numbers:",b)
else:
print("Largest among the three numbers:",c)
OUTPUT
Program 14: Write a program to Check Prime Number.
Source Code:
n = int(input("Enter a number to check it is prime or not:\t"))
c=0
for i in range(2,n):
if n%i == 0:
c+=1
break
if c==0:
print(n,"is a Prime number.")
else:
print(n,"is not a Prime number.")
OUTPUT
Program 15: Write a program to Print Prime Nos. in an Interval.
Source Code:
def print_prime(a,b):
for i in range(a,b+1):
c=0
for j in range(2,i):
if i%j == 0:
c+=1
break
if c==0:
print(i, end=" ")
print("Printing prime b/w interval")
m = int(input("Entrer the lowewr range:\t"))
n = int(input("Entrer the upper range:\t"))
print("Prime number in interval [",m,",",n,"]:")
print_prime(m,n)
OUTPUT
Program 16: Write a python program to Find the Factorial of a
Number.
Source Code:
def factorial(n):
fact = 1
for i in range(n, 0, -1):
fact = fact*i
return fact
num = int(input("Enter a number to find its factorial:\t"))
print("Factorial of",num,"=",factorial(num))
OUTPUT
Program 17: Write a program to Display the multiplication Table.
Source Code:
def table(n):
for i in range(1,11):
print(n,"x",i,"=",n*i)
num = int(input("Enter a number to print its Multiplication
Table:\t"))
print("Multiplication Table of",num,":")
table(num)
OUTPUT
Program 18: Write a program to take input as ‘Narendra Modi’
using Command Prompt and Print as ‘Namo’.
Source Code:
import sys
m = (sys.argv[1])
n = (sys.argv[2])
print(m[0:2]+n[0:2].lower())
OUTPUT
Program 19: Write a program to take input two numbers using
Command Prompt and Print its Sum and Product.
Source Code:
import sys
m = int(sys.argv[1])
n = int(sys.argv[2])
print("Sum =",m+n)
print("Product =",m*n)
OUTPUT
Program 20: Write a program to take input a list of 5 integers and
print its sum.
Source Code:
print("Enter 5 Integers:")
integers = []
for i in range(0,5):
j = int(input("Enter " +str(i+1)+" = "))
integers.append(j)
print("Integers = ",integers)
sum = 0
for i in integers:
sum+=i
print("Sum of all 5 integers = ",sum)
OUTPUT
Program 21: Write a program to print a list in reverse order.
Source Code:
list = [3, 23, 13, 6, 22, 8, 29]
print(list)
print("list in reverse order:")
for i in range(-1, -(len(list)+1), -1):
print(list[i], end=" ")
OUTPUT
Program 23:
Source Code:
album="Aashiqui 2", 2013,"Arijit Singh",((1,"Tum hi ho"),
(2,"Chahun Mai Ya Na"),(3,"Meri Aashiqui"),(4,"Aasan Nahin
Yahaan"))
title, year, singer, song = album
print("Title:", title)
print("Year:",year)
print("Singer:",singer)
for i in song:
print("Song Number:{},Song Name:{}".format(i[0], i[1]))
OUTPUT
Program 24: Write a program to justify List are mutable but Tuple
are immutable.
Source Code:
list = [1, 2, 3, 4]
tuple = (1, 2, 3, 4)
print("List address = ",id(list))
print("Tuple address = ",id(tuple))
list += [5, 6]
tuple += (5, 6)
print(list)
print(tuple)
print("List address after modification = ",id(list))
print("Tuple address after modification = ",id(tuple))
print("Hence, List are mutable and Tuple are immutable.")
OUTPUT
Program 25: Write a function called find_largest() which accept
Multiple strings as arguments and return the largest string and its
length also.
Source Code:
def find_largest(*P):
max = 0
for i in P:
if len(i)>max:
largest = i
max = len(i)
return largest, max
print("Largest String and its length:",find_largest("Ram", "Shyam",
"Mahendra", "Rahul"))
OUTPUT