Python Nakul

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Name: Saksham Negi Course: BSc IT

University roll no: 2223074 Class Roll no:60


Subject Name: Data Analytics & Python Programming lab

1. Python program to print "Hello Python"

print("Hello Python")
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

2. Python program to do arithmetical operations

num1=22
num2=11

Sum=num1+num2
Sub=num1-num2
Div=num1/num2
Mul=num1*num2
Module=num1%num2

print("Sum is: ",Sum)


print("Subraction is: ",Sub)
print("Divison is: ",Div)
print("Multiplication is: ",Mul)
print("Modulus is: ",Module)
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

3. Python program to find the area of a triangle

base=22
height=11
Area=1/2*base*height
print("Area of a Triangle: ",Area)
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

4. Python program to solve quadratic equation


import cmath
a=int(input("enter number: "))
b=int(input("enter number: "))
c=int(input("enter number: "))
d=(b**2) -(4*a*c)
root1=(-b-cmath.sqrt(d))/2*a
root2=(-b+cmath.sqrt(d))/2*a
print("discriminant",d )
print("Roots are: ",root1,"and",root2)
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

5. Python program to swap two variables


a=int(input("enter a: "))
b=int(input("enter b: "))
t=a
a=b
b=t
print("a is ",a)
print("b is ",b)
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

6. Python Program to Check if a Number is Positive, Negative or


Zero
x=int(input("enter a number: "))
if(x<0):
print("Negative")
elif(x>0) :
print("Positive")
else:
print("Zero")
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

7. Python Program to Check if a Number is Odd or Even

x=int(input("enter a number: "))


if(x%2==0):
print("Even")
else:
print("Odd")
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

8. Python Program to Check Leap Year


y=int(input("Enter Year : "))
if(y%400==0 or y%100!=0 and y%4==0 ):
print("Given year is a leap year")
else:
print("Not a leap year")
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

9. Python Program to Check Prime Number


num=int(input("Enter a number : "))
if(num==1):
print("Not a prime number")
if(num>1) :
for i in range(2,num) :
if(num%i==0):
print("Not a Prime number")
break;
else:
print("Prime number")
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

10. Write a python program to check if a value entered by a user is


Palindrome or not
list=*+
l1=input("enter 1st value: ")
l2=input("enter 2nd value: ")
l3=input("enter 3rd value: ")
list.append(l1)
list.append(l2)
list.append(l3)
copy_list=list.copy()
copy_list.reverse()

if(copy_list==list) :
print("palindrome")
else:
print("Not palindrome")
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

11. Write a python program to check if a value entered by a user is


Armstrong or not.

Code:-
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

Output:-
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

12.Consider a number entered by a user. Now calculate the Factorial of this


number.

Code:-
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)

def calculate_factorial():
try:
number = int(input("Enter a non-negative integer: "))
if number < 0:
print("Invalid input. Please enter a non-negative integer.")
else:
result = factorial(number)
print(f"The factorial of {number} is {result}.")
except ValueError:
print("Invalid input. Please enter a non-negative integer.")

calculate_factorial()
Output:-
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

13.Write a python program to print the Fibonacci sequence upto the range

entered by a user.

Code:-

nterms = int(input("How many terms? "))


n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
Output:-
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

14.Write a python program to check whether the number entered by the user

is a Perfect number or not.

Code:-
num=int(input("Enter the number: "))
sum_v=0
for i in range(1,num):
if (num%i==0):
sum_v=sum_v+i
if(sum_v==num):
print("The entered number is a perfect number")
else:
print("The entered number is not a perfect number")

Output:-
Name: Saksham Negi Course: BSc IT
University roll no: 2223074 Class Roll no:60
Subject Name: Data Analytics & Python Programming lab

15.Consider any long string. Now replace each space between two words
with the tab (i.e. the space created when you press the key 'Tab' from your
keyboard).

Code:-
long_string = "This is a long string with many spaces"
long_string = long_string.replace(" ", "\t")
print(long_string)

Output:-

You might also like