PYTHON PROGRAMS
PYTHON PROGRAMS
EXPERIMENT-01:
TITLE : Write a simple program to display “ Hello world”.
SOURCE CODE:
Print(“Hello world ”)
OUTPUT :
EXPERIMENT-02:
TITLE : Write a program to implement basic calculator operations.
SOURCE CODE:
OUTPUT:
EXPERIMENT -03:
TITLE : Write a program to implement simple interest calculator.
SOURCE CODE:
principal = float(input(“enter the principal amount:”))
rate = float(input(“enter the annual rate of interest(%):”))
Time = float(input(“enter the time period:”))
Time = float(input(“enter the time period:”))
Simple_interest=(principal*rate*time)/100
Print(“simple_interest:”,simple_interest)
OUTPUT:
EXPERIMENT-04:
TITLE : Write a program to implement Fibonacci series.
SOURCE CODE:
n=int(input("enter the number of terms:"))
n1,n2=0,1
If n<=0:
print("please enter a positive number")
elif n==1:
print("fibonacci sequence up to given number",n,"is:",n1)
else:
print("Fibonacci sequence is:")
print(n1,end="")
print(n1,end="")
for i in range(2,n):
next_term =n1+n2
print(next_term,end="")
n1,n2 = n2,next_term
OUTPUT:
EXPERIMENT-05:
TITLE : Write a program to implement Recursive Factorial in python.
SOURCE CODE:
def factorial(n):
if n==0:
return 1
else:
return n*factorial(n-1)
num = int(input("enter a number to find its factorial:"))
print ("factorial of", num,"is:",factorial(num))
OUTPUT:
EXPERIMENT-06:
TITLE : Write a program to implement basic relational operators.
SOURCE CODE:
num1 = float(input("enter the number"))
op = input("enter operator(<,>,<=,>=,==,!=): ")
num2=float(input("enter the number"))
if op =="<":
print(num1<num2)
elif op == ">":
print(num1>num2)
elif op == "<=":
print(num1<=num2)
elif op == "==":
print(num1==num2)
elif op == "!=":
print(num1!=num2)
elif op == ">=":
if num2!=0:
print(num1>=num2)
else:
print("error")
else:
print("invalid operator")
OUTPUT:
EXPERIMENT-07:
TITLE : Write a program to implement sum of digits of a given number.
SOURCE CODE:
def sum_of_digits(n):
sum=0
while n>0:
rem=n%10
sum=sum+rem
n=n//10
return sum
num=int(input("enter the number:"))
print("sum of digits:",sum_of_digits(num))
OUTPUT:
EXPERIMENT-08:
TITLE : Write a program to check whether the given number is leap year or not.
SOURCE CODE:
def is_leap_year(year):
if(year%4==0 and year%100!=0)or(year%400==0):
return True
else:
return False
year=int(input("enter a year:"))
if is_leap_year(year):
print(year,"is a leap year.")
else:
print(year,"is not a leap year.")
OUTPUT:
EXPERIMENT-09:
TITLE : Write a program to check the given number is palindrome or not.
SOURCE CODE:
def is_palindrome(num):
t=num
rev=0
while t>0:
digit=t%10
rev=rev*10+digit
t=t//10
if rev==num:
return True
else:
return False
num=int(input("enter a number:"))
if is_palindrome(num):
print(num,"is a palindrome")
else:
print(num,"is not a palindrome")
OUTPUT:
EXPERIMENT-10
TITLE : Write a program to check whether a given number is even or odd.
SOURCE CODE:
num=int(input("enter a number:"))
if num%2==0:
else:
OUTPUT:
EXPERIMENT-11
TITLE : Write a program to find the sum of natural numbers upto a given number
number.
SOURCE CODE:
def sum_of_natural_numbers(n):
return n*(n+1)//2
num=int(input("enter a number:"))
if num<0:
else:
OUTPUT:
EXPERIMENT-12
TITLE : Write a program to implement for loop to print sequential count.
SOURCE CODE:
for count in range(1,6):
Print(count)
Output:
EXPERIMENT-13
TITLE : Write a program to implement While loop to print sequential count.
SOURCE CODE:
number = 1
while number<=5:
print(number)
number=number + 1
Output:
EXPERIMENT-14
TITLE : Write a program to perform addition of numbers using functions.
SOURCE CODE:
def add(n1,n2):
return n1+n2
num1=int(input("enter the first number:"))
num2=int(input("enter the second number"))
print("Addition of ", num1, "and", num2, "is:",add(num1,num2))
Output:
EXPERIMENT-15
TITLE : Write a program to implement Arithmetic operators using functions in while
loop using functions.
SOURCE CODE:
def add(x,y):
return x+y
def subtract(x,y):
return x-y
def multiply(x,y):
return x*y
def divide(x,y):
if y==0:
return "cannot divisible by zero!!!"
else:
return x/y
print("select the operation:")
print("1.ADDITION")
print("2.SUBSTRACTION")
print("3.MULTIPLICATION")
print("4.DIVISION")
choice=input("enter choice(1/2/3/4):")
if choice in('1','2','3','4'):#the in operation checks whether the value of choice matches any of
the elements in the tuple('1','2','3','4')
num1=int(input("enter first number:"))
num2=int(input("enter second number:"))
if choice=='1':
print("Addition of given numbers is:",add(num1,num2))
elif choice=='2':
print("Substraction of given numbers is:",subtract(num1,num2))
elif choice=='3':
print("Multiplication of given numbers is:",multiply(num1,num2))
elif choice=='4':
print("Division of given numbers is:",divide(num1,num2))
else:
print("please enter a valid input!!")
again=input("do you want to repeat the operations again?(yes/no):")
if again.lower( )!='yes':
Break
OUTPUT:
EXPERIMENT-16
TITLE : Write a program to find the biggest of the numbers.
SOURCE CODE:
num1 = float (input ("Enter the first number: "))
num2 = float (input ("Enter the second number: "))
num3 = float (input ("Enter the third number: "))
if num1 >= num2 and num1 >= num3:
greatest = num1
elif num2 >= num1 and num2 >= num3:
greatest num2
else:
greatest num3
print ("The greatest number is:", greatest)
OUTPUT:
EXPERIMENT-17
TITLE : Write a program to implement Logical Operators.
SOURCE CODE:
x = int(input("Enter the value of x: "))
y = int(input("Enter the value of y: "))
print(x, "> 0 and", x, "< 10 is:" x>0 and x<10)
print(x, "> 0 and", y, "> 10 is:", x>0 and y>10)
print(x, "> 10 or", y, "> 10 is:", x>10 or y>10)
print(x, "% 2 == 0 and", y, "% 2 == 0 is:", x% 2 == 0 and y % 2 == 0)
print("not (", x, "+", y, "> 15) is:", not (x+y)>15
OUTPUT:
EXPERIMENT-18
TITLE : Write a program to implement Assignment Operators.
SOURCE CODE:
x= int(input("Enter the initial value of x"))
x+=5
x-=3
x*=2
x//=4
x%=3
x**=2
x/=2
OUTPUT:
EXPERIMENT-19
TITLE : Write a program to implement Bitwise Operators in python.
SOURCE CODE:
x = int(input("Enter the value of x: ")
result_xor=x^y
result_not_x=~x
result_left_shift=x<<shift_bits
OUTPUT:
EXPERIMENT-20
TITLE : Write a program to implement perfect square between 1 and 50.
SOURCE CODE:
def is_perfect_square(num):
if is_perfect_square(i):
OUTPUT:
EXPERIMENT-21
TITLE : Write a program to implement type conversions in python.
SOURCE CODE:
int_num = input((“enter the integer value:”)
float_num= float(int_num)
int_num=int(float_num)
int_from_str=int(str_num)
float_from_str = float(str_float)
str_from_int=str(int_str)
str_from_float =str(float_str)
OUTPUT:
EXPERIMENT-22
TITLE : Write a program in python to implement Mathematical functions.
SOURCE CODE:
import math
#Square root of a number
num_sqrt = float(input("\nEnter a number to find its square root:"))
print("Square root of", num_sqrt, "is: ", math.sqrt(num_sqrt))
#Factorial of a number
num_factorial = int(input("\nEnter a number to find its factorial: "))
print("Factorial of", num_factorial, 'is:", math.factorial(num_factorial))
#Absolute value of a number, Modulus|
num_abs = float(input("\nEnter a floating point number to find its absolute value: "))
print(“Absolute value of", num_abs, "is:", math.fabs(num_abs))
# Power of a number
base = float(input("\nEnter the base to find exponential: "))
exponent = float(input("Enter the exponent of an exponential: "))
print(base, “raised to the power of", exponent, “is: “, math.pow(base, exponent))
#Logarithm of a number - log basenum
num_log = float(input("\nEnter a number to find its logarithm: "))
base_log = float(input("Enter the base of the logarithm: "))
print("Logarithm of", num_log, "to the base", base_log, "is:", math.log(num_log, base_log))
#Greatest Common Divisor (GCD) of two numbers
num1_gcd = int(input("\nEnter the first number: "))
num2_gcd = int(input("Enter the second number: "))
print("GCD of", num1_gcd, "and", num2_gcd, "is:", math.gcd(num1_gcd, num2_gcd))
#Value of pi (π)
print("\nValue of pi:”math.pi)
#Value of Euler's number (e)
print("\nValue of Euler's number (e):", math.e)
OUTPUT:
EXPERIMENT-23
TITLE : Write a program to implement break,continue,and pass statements in python.
SOURCE CODE:
my_list = [1, 2, 3, 4, 5]
print("Demonstrating 'break statement:")
for num in my_list:
if num == 4:
break
print(num)
print("Outside loop")
print("\nDemonstrating 'continue statement’:")
for num in my_list:
if num %2 == 0:
continue
print(num)
print("Outside loop")
print("\nDemonstrating 'pass statement’:")
for num in my_list:
if num == 3:
pass # (Placeholder, does nothing)
else:
print(num)
print("Outside loop")
OUTPUT:
EXPERIMENT-24
TITLE : Write a program to implemented formatted strings.
SOURCE CODE:
name=input("Enter your name:")
age=input("Enter your age:")
print(f"MY name is {name} and I am {age} years old")
OUTPUT:
EXPERIMENT-25
TITLE : Write a program to implement enumerate function in python.
SOURCE CODE:
list=['Apples','29.4','Bananas',0.8,'Cherries',8.0,'Dates',11.091999]
for index, value in enumerate(list):
print(f'Index:{index},value:{value}')
OUTPUT:
EXPERIMENT-26
TITLE : Write a program to achieve simple TO-DO list Manager.
SOURCE CODE:
todo_list=[]
def add_task(task):
todo_list.append(task)
print(f"Task '{task}' added successfully to the to-do list")
def view_tasks():
if todo_list:
print("To-Do List:")
for idx,task in enumerate(todo_list,start=1):
print(f"{idx}.{task}")
else:
print("To-Do Lidt is empty")
while True:
print("\n1.Add task \n2.View task \n3.Quit")
choice=input("Enter the choice:")
if choice=="1":
task=input("Enter the task:")
add_task(task)
elif choice=="2":
view_tasks()
elif choice=="3":
print("Exiting...")
break
else:
print("Invalid choice. PLease try again")
OUTPUT:
EXPERIMENT-27
TITLE : Write a program in python to search for an element in an array.
SOURCE CODE:
from array import array
arr=array('i',[])
num_elements=int(input("Enter the length of an array:"))
for i in range(num_elements):
x=int(input("Enter the value:"))
arr.append(x)
print("\n Elements in an array:",arr)
Ele_search=int(input("\nEnter the search element:"))
index=0
found=False
for e in arr:
if e==Ele_search:
print("Element found at index number:")
found=True
break
index+=1
else:
print("Element not found in the array using the traditional approach")
try:
index=arr.index(Ele_search)
print("Using built-in function, elements found at index:",index)
except ValueError:
print("Element not found using built-in function")
OUTPUT:
EXPERIMENT-28
TITLE : Write a program to implement ternary operator.
SOURCE CODE:
num=int(input("Enter a number:"))
result="even" if num%2==0 else "odd"
print(f"The number {num} is {result}.")
OUTPUT: